Java Swing Programming: Creating Graphics Components and Event Handling, Study Guides, Projects, Research of Computer Science

Java code examples for creating and customizing graphics components using swing, as well as handling user events. Topics include creating rectangles, circles, and squares, using colorviewer for custom colors, and implementing event listeners for buttons and counters.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/18/2009

koofers-user-q1c
koofers-user-q1c 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Script for live demo part of Day 7 and 8 SWING Intro for CSSE 220
Make a new project (FirstGraphics) and a new class (FirstGraphics).
import javax.swing.JFrame;
Why javax? When first introduced, Swing was an extension to Java, not part of the standard distribution.
static final int FRAME_WIDTH = 300;
static final int FRAME_HEIGHT = 400;
We don't want to use magic numbers.
Now add each of the following lines, explaining as we go (mainly explain what happens if we don't do them)
JFrame frame = new JFrame(); Create the window. Unlike in ZelleGraphics, the window is not
automatically displayed.
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); If we leave this out, both height and width will be zero, so we
won't see a window,
frame.setTitle("First Graphics"); If we leave this out, the window will not have a title
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); If we leave this out, clicking the window's Close button will not
kill the program. Note that EXIT_ON_CLOSE is a (static)
constant from the JFrame class.
frame.setVisible(true); If we leave this out, the window will not display
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Run it and see the empty window.
Quiz Question 7
This is a good "boilerplate" for getting a GUI program
up and going.
Question: How could we modify this code to display
two windows?
Do it.
Now we want to display a couple of rectangles
in the window. We need a new class that extends JComponent.
Call it FirstGraphicsComponent Superclass: javax.swing.JComponent.
(Do not type in all of the imports. Let Eclipse add them
as needed (click the red X and choose the import option)
public void paintComponent(Graphics g) {
This is the method we always write to paint a component.
But we don't ever need to call it. It gets called when needed.
Quiz Question 8
Where does the g come from? We don't need to create it.
Add the line to paintComponent:
g.drawRect(20, 20,75, 100);
Then add these lines to the FirstGraphics class:
FirstGraphicsComponent component =
new FirstGraphicsComponent();
frame.add(component);
import javax.swing.JFrame;
public class FirstGraphics{
static final int FRAME_WIDTH = 300;
static final int FRAME_HEIGHT = 400;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("First Graphics");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.JComponent;
/**
A component that draws two rectangles.
*/
public class FirstGraphicsComponent extends JComponent {
public void paintComponent(Graphics g) {
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;
// Construct a rectangle and draw it
Rectangle box = new Rectangle(5, 10, 20, 30);
g2.draw(box);
// Move rectangle 15 units right and 25 units down
box.translate(15, 25);
// Draw moved rectangle
g2.draw(box);
}
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Swing Programming: Creating Graphics Components and Event Handling and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Script for live demo part of Day 7 and 8 SWING Intro for CSSE 220

Make a new project (FirstGraphics) and a new class (FirstGraphics).

import javax.swing.JFrame;

Why javax? When first introduced, Swing was an extension to Java, not part of the standard distribution.

static final int FRAME_WIDTH = 300; static final int FRAME_HEIGHT = 400;

We don't want to use magic numbers.

Now add each of the following lines, explaining as we go (mainly explain what happens if we don't do them)

JFrame frame = new JFrame(); Create the window. Unlike in ZelleGraphics, the window is not

automatically displayed.

frame.setSize( FRAME_WIDTH , FRAME_HEIGHT ); If we leave this out, both height and width will be zero, so we

won't see a window,

frame.setTitle("First Graphics"); If we leave this out, the window will not have a title

frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); If we leave this out, clicking the window's Close button will not

kill the program. Note that EXIT_ON_CLOSE is a (static)

constant from the JFrame class.

frame.setVisible( true ); If we leave this out, the window will not display

frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );

Run it and see the empty window.

Quiz Question 7

This is a good "boilerplate" for getting a GUI program

up and going.

Question: How could we modify this code to display

two windows?

Do it.

Now we want to display a couple of rectangles

in the window. We need a new class that extends JComponent.

Call it FirstGraphicsComponent Superclass: javax.swing.JComponent.

(Do not type in all of the imports. Let Eclipse add them

as needed (click the red X and choose the import option)

public void paintComponent(Graphics g) {

This is the method we always write to paint a component.

But we don't ever need to call it. It gets called when needed.

Quiz Question 8

Where does the g come from? We don't need to create it.

Add the line to paintComponent:

g.drawRect(20, 20,75, 100);

Then add these lines to the FirstGraphics class:

FirstGraphicsComponent component = new FirstGraphicsComponent(); frame.add(component); import javax.swing.JFrame; public class FirstGraphics{ static final int FRAME_WIDTH = 300; static final int FRAME_HEIGHT = 400; public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize( FRAME_WIDTH , FRAME_HEIGHT ); frame.setTitle("First Graphics"); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); frame.setVisible( true ); } } import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JPanel; import javax.swing.JComponent; /** A component that draws two rectangles. */ public class FirstGraphicsComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; // Construct a rectangle and draw it Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); // Move rectangle 15 units right and 25 units down box.translate(15, 25); // Draw moved rectangle g2.draw(box); }

Quiz Question 9

Now add the following code to paintComponent:

g.setColor(Color. GREEN ); g.fillRect(120, 20, 50, 50); Run it again.

Graphics2D

The graphics class is useful, but not very object-oriented. It does not know how to draw objects. Users complained. SUN responded

with the new Graphics2D class.

How to use without breaking old code?

Graphics2D extends Graphics. The object passed to PaintComponent actually is a graphics2D object, but we can use it as a Graphics

object.

Quiz Question 10

Comment out the three drawing commands, and add the following.

Graphics2D g2 = (Graphics2D) g;

Add these lines and run it again:

Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);

Why is this better than the drawRect apoproach?

Because the rectangle is an object that we can now manipulate.

Add this code:

// Move rectangle 15 units to the right and 25 units down box.translate(15, 25); // Draw moved rectangle g2.draw(box);

Run it again.

Then put those last two lines of code in a loop. for (int i=1; i<10; i++)

Now add other shapes: Ellipse2D.Double, Line2D.Double

Construct the line first with four coordinates, then with two

Sometime, look at the API documentation to explore the methods of these classes, along with Rectangle, Arc2D, and Arc2D.Double

Polygon, Point2D and Point2D.Double

Write these on the board and refer to Quiz Question 11

Next use g.drawString(String, int, int) to draw a message in the component.

Let's create a Car class: three lines, a rect, two circles.

Show this picture on a slide.

Do the CarViewer class (can copy and paste from the

FirstGraphics class and change these two lines:

frame.setTitle("Cars"); CarComponent component = new CarComponent(); import javax.swing.JFrame; public class CarViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Cars"); frame.setDefaultCloseOperation( JFrame. EXIT_ON_CLOSE ); CarComponent component = new CarComponent(); frame.add(component); frame.setVisible( true ); } }


/* Adapted from Horstmann Big Java by Claude Anderson / import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; /* This component draws two car shapes. / public class CarComponent extends JComponent { public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D) g; Car car1 = new Car(0, 0); int x = getWidth() - Car. WIDTH ; int y = getHeight() - Car. HEIGHT ; Car car2 = new Car(x, y); car1.draw(g2); car2.draw(g2); } } / Adapted from Cay Horstmann's Big Java 2e by Claude Anderson / import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; /* A car shape that can be positioned anywhere on the screen. / public class Car { public static int WIDTH = 60; public static int HEIGHT = WIDTH /2; private int xLeft; private int yTop; /*

  • Constructs a car with a given top left corner
  • @param x the x coordinate of the top left corner
  • @param y the y coordinate of the top left corner / public Car( int x, int y){ xLeft = x; yTop = y; } /* Draws the car. @param g2 the graphics context / public void draw(Graphics2D g2) { int cH = HEIGHT /3; //componentHeight Rectangle body = new Rectangle(xLeft, yTop + cH, WIDTH , cH); Ellipse2D.Double frontTire = new Ellipse2D.Double( xLeft + cH, yTop + 2cH, cH, cH); Ellipse2D.Double rearTire = new Ellipse2D.Double( xLeft + 4cH, yTop + 2cH, cH, cH); Point2D.Double r1 // Bottom of front windshield = new Point2D.Double(xLeft + cH, yTop + cH); Point2D.Double r2 // The front of the roof = new Point2D.Double(xLeft + 2cH, yTop); Point2D.Double r3 // The rear of the roof = new Point2D.Double(xLeft + 4cH, yTop); Point2D.Double r4 // Bottom of rear windshield = new Point2D.Double(xLeft + 5*cH, yTop + cH); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield);

Some programs to demonstrate event handling.

ButtonTester/ClickListener (from BigJava)

About as simple as you can get and

have a response to a button.

Compile and run first without the

ActionListener stuff.

import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; /** This program demonstrates how to install an action listener. / public class ButtonTester { private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.setSize( FRAME_WIDTH , FRAME_HEIGHT ); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); frame.setVisible( true ); } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* An action listener that prints a message. */ public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System. out .println("I was clicked."); }

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class ClickCounterFrame extends JFrame implements ActionListener {

static final int FRAME_WIDTH = 200;

static final int FRAME_HEIGHT = 60;

private int count = 0;

private JButton button;

private JLabel text;

private JLabel countLabel;

private JPanel panel;

public ClickCounterFrame() {

this .button = new JButton("Click me!");

this .text = new JLabel("Click count");

this .countLabel = new JLabel(count + "");

this .panel = new JPanel();

this .panel.add(button);

this .panel.add(text);

this .panel.add(countLabel);

this .add(panel);

this .button.addActionListener( this );

public static void main(String[] args) {

ClickCounterFrame frame = new ClickCounterFrame();

frame.setSize( FRAME_WIDTH , FRAME_HEIGHT );

frame.setTitle("ClickCount");

frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );

frame.setVisible( true );

@Override

public void actionPerformed(ActionEvent e) {

count++;

countLabel.setText("" + count);

import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Multiplier extends JFrame implements ActionListener { static final int FRAME_WIDTH = 200; static final int FRAME_HEIGHT =300; private int count = 0; private JButton button; private JLabel product; private JTextField multiplicand; private JTextField multiplier; private JPanel panel; public Multiplier() { this .button = new JButton("Multiply"); this .product = new JLabel("0"); this .multiplicand = new JTextField("0"); this .multiplier = new JTextField("0"); this .panel = new JPanel(); this .panel.setLayout( new GridLayout(4, 2)); this .panel.add(button); this .panel.add( new JLabel("Multiplicand")); this .panel.add(multiplicand); this .panel.add( new JLabel("Multiplier")); this .panel.add(multiplier); this .panel.add( new JLabel("Product")); this .panel.add(product); this .panel.add(button); this .add(panel); this .button.addActionListener( this ); } public static void main(String[] args) { Multiplier frame = new Multiplier(); frame.setSize( FRAME_WIDTH , FRAME_HEIGHT ); frame.setTitle("Multiplier"); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); frame.setVisible( true ); } @Override public void actionPerformed(ActionEvent e) { int x = Integer. parseInt (multiplicand.getText()); int y = Integer. parseInt (multiplier.getText()); product.setText(x*y + ""); }

import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPanel; public class TwoButtons extends JFrame{ static final int FRAME_WIDTH = 300; static final int FRAME_HEIGHT = 60; public TwoButtons() { JPanel pan = new JPanel(); ColorToggleButton aButton = new ColorToggleButton("Color Me", Color. red , Color. green ); ColorToggleButton otherButton = new ColorToggleButton("Color Other", Color. blue , Color. yellow ); pan.add(aButton); pan.add(otherButton); this .add(pan); aButton.addActionListener(aButton); otherButton.addActionListener(aButton); } public static void main(String[] args) { JFrame frame = new TwoButtons(); frame.setSize( FRAME_WIDTH , FRAME_HEIGHT ); frame.setTitle("OneButton"); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); frame.setVisible( true ); }