Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications."— Presentation transcript:

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

2 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

3 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

4 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.

5 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?

6 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.

7 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.

8 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

9 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..

10 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)

11 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

12 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?

13 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.

14 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.

15 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

16 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.

17 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

18 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.

19 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)

20 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)

21 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?

22 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.


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

Similar presentations


Ads by Google