Download presentation
Presentation is loading. Please wait.
Published byEileen Crawford Modified over 8 years ago
1
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
2
CSTP FS99CS423 (cotter)2 Graphics in Applications Must include a main() method Must extend the AWT Frame class
3
CSTP FS99CS423 (cotter)3 awt Frame Inheritance Object Component Container Window Frame Class MyNewApp extends Frame
4
CSTP FS99CS423 (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(); }
5
CSTP FS99CS423 (cotter)5 HelloWorldWindow Output
6
CSTP FS99CS423 (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.
7
CSTP FS99CS423 (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);
8
CSTP FS99CS423 (cotter)8 Component.handleEvent( ) action gotFocus lostFocus keyDown keyUp mouseEnter mouseExit mouseMove mouseDrag mouseDown mouseUp
9
CSTP FS99CS423 (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);
10
CSTP FS99CS423 (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); }
11
CSTP FS99CS423 (cotter)11 HelloWorldWindow Output 2
12
CSTP FS99CS423 (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.
13
CSTP FS99CS423 (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
14
CSTP FS99CS423 (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(); }
15
CSTP FS99CS423 (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);
16
CSTP FS99CS423 (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
17
CSTP FS99CS423 (cotter)17 ShowColors.java
18
CSTP FS99CS423 (cotter)18 Object Layout BorderLayout (default for Frames) FlowLayout (default for Panels) GridLayout GridbagLayout CardLayout Fixed: –object.reshape (x, y, width, height);
19
CSTP FS99CS423 (cotter)19 Display Objects Button Checkbox CheckboxGroup TextField TextArea etc.
20
CSTP FS99CS423 (cotter)20 TextTest Example
21
CSTP FS99CS423 (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
22
CSTP FS99CS423 (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); }
23
CSTP FS99CS423 (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(); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.