Question: public class ButtonButton extends javax.swing.JFrame implements java.awt.event.ActionListener { // the field for the graphic component private cpts132.graphics.BigOval dot; java.awt.Color colors[] = {java.awt.Color.green, java.awt.Color.BLUE, java.awt.Color.RED}; int
public class ButtonButton extends javax.swing.JFrame implements java.awt.event.ActionListener { // the field for the graphic component private cpts132.graphics.BigOval dot; java.awt.Color colors[] = {java.awt.Color.green, java.awt.Color.BLUE, java.awt.Color.RED}; int i = 1; public ButtonButton() { super("ButtonButton"); setLocation(25, 25); setSize(400, 300); setDefaultCloseOperation(DISPOSE_ON_CLOSE); // create the dot dot = new cpts132.graphics.BigOval(); add(dot); // create a toolbar javax.swing.JPanel toolbar = new javax.swing.JPanel(); add(toolbar, java.awt.BorderLayout.NORTH); // create a button javax.swing.JButton button; button = new javax.swing.JButton("Change Color"); toolbar.add(button); button.addActionListener(this); // finally, set the window to be visible setVisible(true); } @Override public void actionPerformed(java.awt.event.ActionEvent e) { dot.setFill(colors[i++ % colors.length]); } public static void main(String[] args) { new ButtonButton(); } }
****second class
public class BigOval extends javax.swing.JComponent { // field to hold the property value java.awt.Color fill; public BigOval() { fill = java.awt.Color.green; } public java.awt.Color getFill() { return fill; } public void setFill(java.awt.Color color) { fill = color; repaint(); } public void paintComponent(java.awt.Graphics g) { super.paintComponent(g); int x = getWidth() / 7; int y = getHeight() / 7; int w = 5 * x; int h = 5 * y; g.setColor(fill); g.fillOval(x, y, w, h); g.setColor(java.awt.Color.black); g.drawOval(x, y, w, h); } }
Change the text on the existing JButton to "Next Color". You can maintain the same handler, cycling through the colors: red, green, and blue, in that order. Add another button to the toolbar. The text on this button shall be "Previous Color". It will cycle through the colors: red, green, and blue, but in reverse order. Leave the actionPerformed method in ButtonButton as it was, just cycling through red-green-blue. Add a second handler, using an inner class.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
