java swing one of the most important features of java is its ability to draw graphics

16
Java Swing One of the most important features of Java is its ability to draw graphics.

Upload: demitrius-gryphon

Post on 31-Dec-2015

24 views

Category:

Documents


1 download

DESCRIPTION

Java Swing One of the most important features of Java is its ability to draw graphics. AWT to Swing. AWT: Abstract Window Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* extends AWT Depends less on underlying platform Many more predefined GUI components - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Java Swing One of the most important features of Java is its ability to draw graphics

Java Swing

One of the most important features of Java is its ability to draw

graphics.

Page 2: Java Swing One of the most important features of Java is its ability to draw graphics

AWT to Swing

• AWT: Abstract Window Toolkit• import java.awt.*

• Swing: new with Java2• import javax.swing.* extends AWT

• Depends less on underlying platform

• Many more predefined GUI components• Event listeners

• API: • http://docs.oracle.com/javase/8/docs/api/

Page 3: Java Swing One of the most important features of Java is its ability to draw graphics

GUI Component API

• Java: GUI component = class

• Properties

• Methods

• Events

JButton

Page 4: Java Swing One of the most important features of Java is its ability to draw graphics

Using a GUI Component

1. Create it• Instantiate object: b = new JButton(“press me”);

2. Configure it• Properties: b.text

• Methods: b.setText(“press me”); //set this way

3. Add button b to panel• panel.add(b);

4. Listen to it• Events: Listeners

JButton

Page 5: Java Swing One of the most important features of Java is its ability to draw graphics

Anatomy of an Application GUI

JPanel

JButton

JFrame

JLabel

GUI Internal structure

JFrame

JPanel

JButton JLabel

containers

Page 6: Java Swing One of the most important features of Java is its ability to draw graphics

Using a GUI Component 2

1. Create it

2. Configure it

3. Add children (if container)

4. Add to parent (if not JFrame)

5. Listen to it

orderimportant

Page 7: Java Swing One of the most important features of Java is its ability to draw graphics

JPanel

JButton

Listener

JFrame

JLabel

Add from bottom up

• Create:• Frame• Panel• Components• Listeners

• Add: (bottom up)• listeners into components• components into panel• panel into frame

Page 8: Java Swing One of the most important features of Java is its ability to draw graphics

Frames

• Swing version JFrame (extends AWT’s frame)

• Import javax.swing (‘x’ – extension package)

• Default size is 0x0 pixels

DEMO FrameTest.java

Page 9: Java Swing One of the most important features of Java is its ability to draw graphics

Frames

• Need to extend class

• Override constructor for size desired.

• Select behavior on Close (for example, exit)

• Starts invisible, must show

DEMO SimpleFrameTest.java

Page 10: Java Swing One of the most important features of Java is its ability to draw graphics

Frames

• Only a few methods to change look• Inherits methods from superclasses

– dispose – closes window and reclaims resources– setIconImage - used when window is minimized– setTitle – for menu bar of window– setResizable – takes boolean to enable/disable user’s

ability to resize window– setLocation – position component– setBounds – size and position component

DEMO SimpleFrameTest2.java

Page 11: Java Swing One of the most important features of Java is its ability to draw graphics

Inheritance Chain

JFrameFrameWindow

Container

JPanelJComponent

Component

Object

Page 12: Java Swing One of the most important features of Java is its ability to draw graphics

Panels

• Draw on a panel, which is added to frame– Specifically to the content pane of the frame

• Displays information – Draw strings– Draw graphical elements

• Add components into content pane (they are containers)

SomeComponent c = new …Container contentPane = frame.getContentPane();contentPane.add (c);

Page 13: Java Swing One of the most important features of Java is its ability to draw graphics

Application Code

import javax.swing.*;

class hello {

public static void main(String[ ] args) {

JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”);

p.add(b); // add button to panel f.setContentPane(p); // add panel to frame

f.show();}

}

press me

Page 14: Java Swing One of the most important features of Java is its ability to draw graphics

Subclasses

• Extend JPanel• Override paintComponent method in that class to do

graphics

class MyPanel extends Jpanel { public void paintComponent (Graphics

g) { //code for drawing goes here }

DEMO NotHelloWorld.java

Page 15: Java Swing One of the most important features of Java is its ability to draw graphics

Painting…

• Never call paintComponent – Event handler notifies component to paint itself

whenever needed• Resizing, minimizing, overlapping windows, etc.

– If you need to force repainting, call repaint instead

Page 16: Java Swing One of the most important features of Java is its ability to draw graphics

Subclasses cont.

class MyPanel extends Jpanel { public void paintComponent (Graphics g) { super.paintComponent (g); //superclass

does job

//code for drawing goes here g.drawString (“Java is COOL!”, 75, 100); }

DEMO NotHelloWorld.java