Java Graphics

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

description

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.*; - PowerPoint PPT Presentation

Transcript of Java Graphics

Page 1: Java Graphics

CSTP FS99 CS423 (cotter) 1

Java Graphics

java.awt.*;

Page 2: Java Graphics

CSTP FS99 CS423 (cotter) 2

Graphics in Applications

• Must include a main() method• Must extend the AWT Frame class

Page 3: Java Graphics

CSTP FS99 CS423 (cotter) 3

awt Frame Inheritance

• Object• Component• Container• Window• Frame• Class MyNewApp extends Frame

Page 4: Java Graphics

CSTP FS99 CS423 (cotter) 4

HellowWorldWindowimport 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(); }}

Page 5: Java Graphics

CSTP FS99 CS423 (cotter) 5

HelloWorldWindow Output

Page 6: Java Graphics

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

Page 7: Java Graphics

CSTP FS99 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);

Page 8: Java Graphics

CSTP FS99 CS423 (cotter) 8

Component.handleEvent( )

actiongotFocus lostFocuskeyDown keyUp mouseEnter mouseExitmouseMove mouseDragmouseDown mouseUp

Page 9: Java Graphics

CSTP FS99 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);

Page 10: Java Graphics

CSTP FS99 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);}

Page 11: Java Graphics

CSTP FS99 CS423 (cotter) 11

HelloWorldWindow Output 2

Page 12: Java Graphics

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

Page 13: Java Graphics

CSTP FS99 CS423 (cotter) 13

Swing Components

• Derive from awt classesjava.lang.Object -> java.awt.Component ->

jav.awt.Container -> javax.swing.Jcomponent

• Methods overloaded in swing to allow different behaviour

Page 14: Java Graphics

CSTP FS99 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();

}

Page 15: Java Graphics

CSTP FS99 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);

Page 16: Java Graphics

CSTP FS99 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

Page 17: Java Graphics

CSTP FS99 CS423 (cotter) 17

ShowColors.java

Page 18: Java Graphics

CSTP FS99 CS423 (cotter) 18

Object Layout

• BorderLayout (default for Frames)• FlowLayout (default for Panels)• GridLayout• GridbagLayout• CardLayout• Fixed:

– object.reshape (x, y, width, height);

Page 19: Java Graphics

CSTP FS99 CS423 (cotter) 19

Display Objects

• Button• Checkbox• CheckboxGroup• TextField• TextArea• etc.

Page 20: Java Graphics

CSTP FS99 CS423 (cotter) 20

TextTest Example

Page 21: Java Graphics

CSTP FS99 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

Page 22: Java Graphics

CSTP FS99 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);

}

Page 23: Java Graphics

CSTP FS99 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(); }}