220 likes | 333 Views
Lecture 11. Writing Graphic Applications. 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.
E N D
Lecture 11 Writing Graphic Applications
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
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
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.
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?
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.
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.
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
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..
Additional Class • Candy Machine Representation -restocking and selling • CandyMachine (class for candy machine object) • UseMachine (class with main) • CPanel (custom panel class)
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
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?
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.
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) • This shows the use of the MouseListener Interfaces and the methods that must be included. Some are empty but still must be included.
Dialog Boxes • Dialog Boxes Program • DialogBoxMain.java (dialog boxes in main method) • Using Dialog box to do input and output
Graphics with arrays and additional class • Draw a box of crayons • CrayonMain11.java (class with main) • CrayonPanel.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.
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 accounts • BankAccount.java (class representing a bank account) contains name, address (of Address class) and balance • Address.java (class has all address fields) • Note the use of nested classes, Address class object is a variable of the BankAccount class
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 ActionListener • 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.
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)
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) • 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.
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?
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.