Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Related documents


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. 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."); } } Illustrates the Frame itself as ActionListener // OneButton. Written by Claude Anderson. // A button changes background colors when it is clicked. import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Color; import java.awt.event.*; // We make the Frame be the ActionListener for the Button. public class OneButton extends JFrame implements ActionListener { private JButton aButton; // The only component in the frame. static final int FRAME_WIDTH = 300; static final int FRAME_HEIGHT = 60; public OneButton() { aButton= new JButton("Color Me"); aButton.setBackground(Color.RED); // initial color. this.add(aButton); // Add button to frame aButton.addActionListener(this); // Hello, my name is // <anonymous OneButton object> // and I'll be your ActionListener this evening! } public static void main(String[] args) { JFrame frame = new OneButton(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("OneButton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } // This method is called when the button is clicked. // It toggles the background color between green and red. public void actionPerformed(ActionEvent e) { if (aButton.getBackground().equals(Color.RED)) aButton.setBackground(Color.GREEN); else aButton.setBackground(Color.RED); } 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); }