1 / 23

Java Graphics

Java Graphics. java.awt.*;. Graphics in Applications. Must include a main() method Must extend the AWT Frame class. awt Frame Inheritance. Object Component Container Window Frame Class MyNewApp extends Frame. HellowWorldWindow. import java.awt.*;

psyche
Download Presentation

Java Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Graphics java.awt.*; CS423 (cotter)

  2. Graphics in Applications • Must include a main() method • Must extend the AWT Frame class CS423 (cotter)

  3. awt Frame Inheritance • Object • Component • Container • Window • Frame • Class MyNewApp extends Frame CS423 (cotter)

  4. HellowWorldWindow import java.awt.*; public class HelloWorldWindow extends Frame { public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent (evt); } public void paint(Graphics g) { g.drawString(“A Windows version of Hello, World”,25,75); } public static void main(String[] args) { Frame f = new HelloWorldWindow(); f.setSize(300, 200); f.show(); } } CS423 (cotter)

  5. HelloWorldWindow Output CS423 (cotter)

  6. public void paint (Graphics g) • Provides an initial presentation of Graphics • Works on Graphics object • (like device context). • Place to store settings for screen output (text, images. etc.) • Must be regenerated following changes. CS423 (cotter)

  7. Event Driven Programming • Operating System recognizes an event • Sends a signal to appropriate object • Object receives event notification and call appropriate function public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent (evt); CS423 (cotter)

  8. Component.handleEvent( ) action gotFocus lostFocus keyDown keyUp mouseEnter mouseExit mouseMove mouseDrag mouseDown mouseUp CS423 (cotter)

  9. Accessing super class methods • Object will execute method version that is closest to it in the class hierarchy. • If a method is extended, only the extended behaviour is given in the extension. • To access previous versions, use: super.method ( ); super.handleEvent (evt); CS423 (cotter)

  10. Add Font Control /* modify paint method to specify font */ public void paint (Graphics g) { Font f = new Font(“Helvetica”, Font.BOLD, 14); g.setFont (f); g.drawString(“A Windows version of Hello, World”, 75, 100); } CS423 (cotter)

  11. HelloWorldWindow Output 2 CS423 (cotter)

  12. Swing Components • Defined in package javax.swing • Pure Java components • AWT components tied to local platform GUI • UNIX java windows look like X windows • Windows java windows look like windows windows (??) • etc. • Swing defines a common look and feel for Java. CS423 (cotter)

  13. Swing Components • Derive from awt classes java.lang.Object -> java.awt.Component -> jav.awt.Container -> javax.swing.Jcomponent • Methods overloaded in swing to allow different behaviour CS423 (cotter)

  14. Java 1.2 Graphics // From: Java: How to Program - Deitel & Deitel // Fig. 11.5: ShowColors.java Demonstrating Colors import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowColors extends JFrame { public ShowColors() { super( "Using colors" ); setSize( 400, 130 ); show(); } CS423 (cotter)

  15. ShowColors.java public void paint( Graphics g ) { // set new drawing color using integers g.setColor( new Color( 255, 0, 0 ) ); g.fillRect( 25, 25, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,40); // set new drawing color using floats g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); g.fillRect( 25, 50, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,65); // set new drawing color using static Color objects g.setColor( Color.blue ); g.fillRect( 25, 75, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(),130,90); CS423 (cotter)

  16. ShowColors.java // display individual RGB values Color c = Color.magenta; g.setColor( c ); g.fillRect( 25, 100, 100, 20 ); g.drawString( "RGB values: " + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue(), 130, 115 ); } public static void main( String args[] ) { ShowColors app = new ShowColors(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); /*end of main ()*/ } /*end of paint*/ } // end of class CS423 (cotter)

  17. ShowColors.java CS423 (cotter)

  18. Object Layout • BorderLayout (default for Frames) • FlowLayout (default for Panels) • GridLayout • GridbagLayout • CardLayout • Fixed: • object.reshape (x, y, width, height); CS423 (cotter)

  19. Display Objects • Button • Checkbox • CheckboxGroup • TextField • TextArea • etc. CS423 (cotter)

  20. TextTest Example CS423 (cotter)

  21. import java.awt.*;public class MyTextTest extends Frame{ public MyTextTest() { setTitle("MyTextTest"); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Button("Tick")); p.add(new Button("Set time")); hourField = new TextField("12", 3); p.add(hourField); minuteField = new TextField("00", 3); p.add(minuteField); timeField = new TextField("", 12); p.add(timeField); add("North", p); } MyTextTest CS423 (cotter)

  22. MyTextTest public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (arg.equals("Tick")) { int minutes = Integer.parseInt(minuteField.getText()); minutes += 1; String min = String.valueOf(minutes); minuteField.setText(min); } CS423 (cotter)

  23. MyTextTest else if (arg.equals("Set time")) { int hours = Integer.parseInt(hourField.getText()); int minutes = Integer.parseInt(minuteField.getText()); String tim = hourField.getText()+ ":" + minuteField.getText(); timeField.setText(tim); } else return false; return true; } private TextField hourField; private TextField minuteField; private TextField timeField; public static void main(String[] args) { Frame f = new MyTextTest(); f.resize(400, 100); f.show(); } } CS423 (cotter)

More Related