230 likes | 353 Views
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.*;
E N D
Java Graphics java.awt.*; CS423 (cotter)
Graphics in Applications • Must include a main() method • Must extend the AWT Frame class CS423 (cotter)
awt Frame Inheritance • Object • Component • Container • Window • Frame • Class MyNewApp extends Frame CS423 (cotter)
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)
HelloWorldWindow Output CS423 (cotter)
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)
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)
Component.handleEvent( ) action gotFocus lostFocus keyDown keyUp mouseEnter mouseExit mouseMove mouseDrag mouseDown mouseUp CS423 (cotter)
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)
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)
HelloWorldWindow Output 2 CS423 (cotter)
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)
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)
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)
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)
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)
ShowColors.java CS423 (cotter)
Object Layout • BorderLayout (default for Frames) • FlowLayout (default for Panels) • GridLayout • GridbagLayout • CardLayout • Fixed: • object.reshape (x, y, width, height); CS423 (cotter)
Display Objects • Button • Checkbox • CheckboxGroup • TextField • TextArea • etc. CS423 (cotter)
TextTest Example CS423 (cotter)
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)
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)
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)