CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
CS 280 Data Structures Professor John Peterson. Quiz 4 Recap Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}. Draw this in tree form and then show.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Field Trip #19 Animations with Java By Keith Lynn.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Concurrent Programming and Threads Threads Blocking a User Interface.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Java Swing One of the most important features of Java is its ability to draw graphics.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Java Programming: From Problem Analysis to Program Design,
A Quick Java Swing Tutorial
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Graphical User Interface
Presentation transcript:

CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications Lecture 11

CS 112 George Mason UniversityLecture 4 Slide 2 Simplest Graphic Application Import the java.swing.*; for the JFrame A public class that has a main method The main method has A JFrame declared, created One content pane is requested A label is added to the contentpane The frame is made visible

CS 112 George Mason UniversityLecture 4 Slide 3 SIMPLE GRAPHICS APPLICATION import javax.swing.*; public class simplegraphics { public static void main(String args[]) { //declaring and creating a frame and label JFrame frame=new JFrame(“I am a frame”); JLabel label = new JLabel(“I am a label”); //getting a pane for the frame and adding the label to the pane. frame.getContentPane().add(label); //make the frame visible when the program executes frame.setVisible(true); } Simple.java -Simple Graphics Program

CS 112 George Mason UniversityLecture 4 Slide 4 adding components Other components can be added directly to the ContentPane of the frame. Jpanel objects and other components can be created in main and added to the content pane, Alternatively an object of a custom panel can be created in main and write the custom panel class. The other components will be created here and added to the panel. For either option, if interactive components, such as buttons are used, an implementation of the ActionListener interface is required. This means an actionPerformed method either: in the class where the buttons and textfield are created or write a new class with this method in it, and declare an object of this class where the buttons and textfield are created.

CS 112 George Mason UniversityLecture 4 Slide 5 The option we have usually used We have usually had a separate class for the panel and components One class with main, in which you declare and create a frame, and declare and create a custom panel (instead of a plain JPanel, it’s a MyJPanel which extends JPanel and is defined in another class), and add the panel to a ContentPane of the frame We then have a second custom panel class. This class extends Jpanel. In this class we create and add components. We also declare and create a custom actionlistener or have the the listener method (e.g. actionPerformed) in this class. If we declare a custome listener, then a third class is written for our custom actionlistener either as an inner class or as a separate class. It implements ActionListener interface and has only one method, the actionPerformed method Why?

CS 112 George Mason UniversityLecture 4 Slide 6 It makes the program cleaner Without a separate panel class, everything is done in the main method. The frame is created, a contentpane is gotten If there is a panel, it is declared and created The buttons and textfields and actionlisteners are declared and created The buttons and textfields are added to the panel or frame The actionlisteners are added to the buttons and textfields The custom actionlistener is declared and created or the actionPerformed is written in the same class. When more components are added, main becomes long and complicated, harder to maintain or modify.

CS 112 George Mason UniversityLecture 4 Slide 7 The programs that follow will have at least two classes One class will have the main method. In this method, a frame will be created, and one or more custom panel objects will be created and added to the frame. Another class will extend JPanel, will have a constructor method, in which components (such as buttons) will be created and added to the panel, custom actionlistener object(s) will be created and added to the components. This class may also override the paintComponent() method (which paints the screen) to provide repainting of the screen at particular. If the program has components, The custom panel class will also ll implement ActionListener or other Listeners and will contain the methods that the listener interfaces require, such as actionPerformed More classes will be added If the programs use objects of other classes, which have to be defined.

CS 112 George Mason UniversityLecture 4 Slide 8 Menu Given this structure how would you use Control Structures Arrays Additional classes representing objects Class concepts, such as inheritance, interfaces Dialog boxes Timer class for animation String methods

CS 112 George Mason UniversityLecture 4 Slide 9 Control Structures Drawing different shapes with radio button options ControlMain11 (class with main) ControlPanel11 (custom JPanel class) Notice the use of the switch structure Notice the set up of radio buttons in groups Notice the use of Random() to get pixel sizes and locations Random Circles fitting into 400x400 panel RandomCirclesMain (class with main) RandomCirclesPanel (custom panel) Note that this is only one simple method of insuring circle is inside panel..

CS 112 George Mason UniversityLecture 4 Slide 10 Additional Class Candy Machine Representation -restocking and selling CandyMachine (class for candy machine object) UseMachine (class with main) CPanel (custom panel class)

CS 112 George Mason UniversityLecture 4 Slide 11 Arrays and Layouts Keypad that displays last number pressed and can be reset OOMain11 (class with main) OOPanel11 (custom panel) Note the use of the grid layout on a panel that is placed in the Center of a Border Layout Note the two statements that are necessary to create a JButton array. The array must be declared and then the Jbutton constructor must be called for each element

CS 112 George Mason UniversityLecture 4 Slide 12 Arrays and Additional Class Skyline at night -Using additional classes ClassMain11 (class with main) ClassPanel11 (custom JPanel class) Notice that the buildings move everytime you resize the frame. This is because the paintComponent method is called to redraw the screen and it resets the x, y locations of the buildings. What would you do to make 50 buildings instead of 20? Suppose you wanted to also have round buildings?

CS 112 George Mason UniversityLecture 4 Slide 13 Control Structures and Arrays Checkerboard with checkers ArrayMain11 (class with main) ArrayPanel11.java ( custom JPanel class) Notice that the array is in the main method and it is a two dimensional array to draw the checkerboard. The checkers areare drawn in paintComponent each time a square(panel )is created. A random number is called to determine whether to draw a checker,another to determine if it should be red or black. Up to 5 red checkers are drawn and up to 8 black checkers. The numbers of checkers drawn must be static, to keep the count the same for each panel.

CS 112 George Mason UniversityLecture 4 Slide 14 Mouse Events and Arrays Draws Lines with Mouse and remembers them RubberLines.java (class with main) RubberPanel.java (panel with mouse listeners which draws line)RubberPanel.java (panel with mouse listeners which draws line) This shows the use of the MouseListener Interfaces and the methods that must be included. Some are empty but still must be included.

CS 112 George Mason UniversityLecture 4 Slide 15 Dialog Boxes Dialog Boxes Program DialogBoxMain.java (dialog boxes in main method) Using Dialog box to do input and output

CS 112 George Mason UniversityLecture 4 Slide 16 Graphics with arrays and additional class Draw a box of crayons CrayonMain11.java (class with main) CrayonPanel.java (custom panel) with crayon and color arraysCrayonPanel.java (custom panel) with crayon and color arrays Crayon.java (class defining a crayon) Notice that paintComponent(Graphics g) is called everytime you resize the screen. That means that you must set up your loops in paintComponent and in the draw method in Crayon so that they repeat. If you used a while loop that kept adding to the index, it would be out of bounds on the first resize.

CS 112 George Mason UniversityLecture 4 Slide 17 Graphics with Other Classes and Arrays Bank Account-open account, deposit and withdraw ManageAccount.java(with main method) AccountPanel.java (setting up textfields and buttons for GUI and creating an array of bank accounts, blank ones to be used as users ask for new accountsAccountPanel.java (setting up textfields and buttons for GUI BankAccount.java (class representing a bank account) contains name, address (of Address class) and balanceBankAccount.java (class representing a bank account) Address.java (class has all address fields) Note the use of nested classes, Address class object is a variable of the BankAccount class

CS 112 George Mason UniversityLecture 4 Slide 18 Images and Timer class for animation The real Turtle moves in a wave motion ImageMain11.java (class with main) ImagePanel11.java (custom panel class) where turtle image is added, implementing ActionListenerImagePanel11.java (custom panel class) Note that image is added by using ImageIcon object Note that if you do not check for x, y and change direction, you may never see the turtle because if x, and y just increase never decrease resizing may put them off the frame.

CS 112 George Mason UniversityLecture 4 Slide 19 Arrays and String methods and the String Tokenizer class Program to translate sentences into piglatin PigLatinMain.java (class with main, frame) PigLatinPanel.java (custom panel class)

CS 112 George Mason UniversityLecture 4 Slide 20 Additional class and complex actionPerformed Lotto Game-guess numbers or let computer pick numbers and win Project3.java (class with main) LottoPanel.java (custom panel class)sets up radio buttons and textfields and implements ActionListener (has actionPerformed method which directs the game activities)LottoPanel.java (custom panel class) Lotto.java (class representing game)- a Lotto object is created when the program is run, the methods set up games, pick numbers and determine winners and jackpots.Lotto.java (class representing game)

CS 112 George Mason UniversityLecture 4 Slide 21 Special Topic: The Event Thread In our previous example: import javax.swing.JFrame; // trust us import javax.swing.JButton; // trust us public class Example { public static void main(String[] args) { JFrame frame = new JFrame("Button Example"); frame.getContentPane().add(new JButton("Press me!")); frame.show(); }... if you run the program, notice that the window stays up with the button. The program doesn't quit. But isn't the program supposed to quit after the last statement in main has been executed?

CS 112 George Mason UniversityLecture 4 Slide 22 Special Topic: The Event Thread Yes and no. When you create a GUI widget (like a JFrame or a JButton), Java fires up the GUI's event thread. Now you have two things "running" in your program: your main thread (created when you started main() ) and the GUI's event thread. A program won't quit until all threads have quit. Even if your main thread has exited, the GUI event thread is still going. So your program will continue to run.