






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 10
This page cannot be seen from the preview
Don't miss anything!







import javax.swing.JFrame;
static final int FRAME_WIDTH = 300; static final int FRAME_HEIGHT = 400;
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
public void paintComponent(Graphics g) {
g.drawRect(20, 20,75, 100);
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); }
g.setColor(Color. GREEN ); g.fillRect(120, 20, 50, 50); Run it again.
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);
// Move rectangle 15 units to the right and 25 units down box.translate(15, 25); // Draw moved rectangle g2.draw(box);
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; /*
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.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 ); }