Question
Lab 7 The purpose of this assignment is GUI program Class Implements Class Extends Inner Classes Drawing Paint Component Work on the following steps in
Lab 7
The purpose of this assignment is
GUI program
Class Implements
Class Extends
Inner Classes
Drawing Paint Component
Work on the following steps in order. An example of the final product is shown at the end of the document.
Create a folder call Lab7yourlastname
STEP 1. Create a class called paintLabMain that contains the following:
Put your comments at the top of VoteCounter.java
Source Code
/**
* @param args
* @author
* @course CPSC 220
* @section Section 1 or 2
* @description fill in
*/
(a). The imports:
import java.awt.*; import javax.swing.*;
(b). A main method that creates a frame object containing an instance of PaintLab, sets the frames title to Paint Lab, sets the default close operation to exit, and makes the frame object visible.
STEP 2. Create a new file called PaintLab that extends JPanel contains the following:
Put your comments at the top of VoteCounterPanel.java
Source Code
/**
* @param args
* @author
* @course CPSC 220
* @section Section 1 or 2
* @description fill in
(a). The imports:
import java.awt.*; import java.awt.event.*; import javax.swing.*;
(b). The class data members
// declares a private global class variable JButton named circle
// declares a private global class variable JButton named square
// declares a private global class variable DrawingPanel named drawingPanel
(c). A default constructor that does the following:
public PaintLab() {
// news a JButton named "Draw Circle"
// news a JButton named "Draw Square"
// creates a buttonPanel (JPanel) using BoxLayout, X_AXIS
// adds the two buttons to buttonPanel
// sets the current panel to BoxLayout, Y_AXIS
// adds the buttonPanel
}
(d). Make sure you can compile and run PaintLab. It will display a small window with two buttons that do nothing when clicked
STEP 3. Create a private inner class of PaintLab called DrawingPanel of type JPanel. Define the following for DrawingPanel:
(a). Private instance variables:
private boolean paintFlag = false; private int theMode = -1;//0 is for circle 1 is Square private int width; private int height;
(b). A constructor:
private DrawingPanel(int width, int height) { this.width = width; this.height = height; // set the preferred size to width by height }
(c). A public method setPaintFlag that sets the paintFlag variable to true and theMode to the given int currentMode argument. The method does not return anything.
(d). Override the paintComponent method with the following.
public void paintComponent(Graphics g) {
// return if paint flag is false super.paintComponent(g); // clears screen
// get the fancier 2D graphics object Graphics2D g2 = (Graphics2D)g;
// create a random color
int red = (int)(Math.random() * 255) + 1; int green = (int)(Math.random() * 255) + 1; int blue = (int)(Math.random() * 255) + 1;
// set the color
g2.setColor(new Color(red, green, blue));
// create a random width
int w = (int)(Math.random() * (width/2));
// create a random location
int x = (int)(Math.random() * width) + 1; int y = (int)(Math.random() * height) + 1;
// draw the shape:
// if the mode is set to CIRCLE, call: g2.fillOval(x, y, w, w)
// else if mode is set to SQUARE, call: g2.fillRect(x, y, w, w)
// turn off the paint flag g.dispose();
(e). Go back to the constructor PaintLab
drawingPanel = new DrawingPanel(400,400);
add(drawingPanel)
Make sure you can compile and run PaintLab (although it wont look any different from before at this point).
STEP 4. Create a private inner class of PaintLab called buttonListener that implements ActionListener.
Define the following for buttonListener:
(a). A constructor:
public void actionPerformed(ActionEvent event) { if(event.getSource()== circle){ Each listener should call the setPaintFlag method on the
drawingPanel (0 FOR DRAW CIRCLE), and then call
drawingPanel.repaint(). } else if (event.getSource()== square){ Each listener should call the setPaintFlag method on the
drawingPanel (1 FOR DRAW CIRCL), and then call
drawingPanel.repaint(). } }
STEP 5. Go back to the PaintLab constructor and fill in the remaining parts.
(a). New drawingPanel variable of type DrawingPanel. You should set the width and height to a reasonable value.
(b). Add the two button listeners to circle and square. Have them new the buttonListener.
(c). Add the drawingPanel to the PaintLab panel. The buttons should be below the drawingPanel.
Compile and run PaintLab. It should work now.
Sample Output
Below is my code for this program. I know I have made a lot of mistake. Please help me and correct me. Thank you so much!
import java.awt.*; import java.awt.event.*; import javax.swing.*; class paintLab extends JPanel { private JButton circle; private JButton square; private JPanel drawingPanel; public paintLab() { JPanel buttonPanel=new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS)); add(square); //setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(buttonPanel); drawingPanel=new JPanel(); drawingPanel.setLayout(new BoxLayout(drawingPanel, BoxLayout.Y_AXIS)); circle=new JButton("DrawCircle"); circle.addActionListener(new CircleListener()); add(circle); //circleButtonListener listener=new circleButtonListener(); //circleButton.addActionListener(listener); //add(circleButton); square=new JButton("Draw Square"); square.addActionListener(new squareListener()); add(drawingPanel); } } class DrawingPanel extends JPanel { private boolean paintFlag=false; private int theMode=-1; private int width; private int height; private DrawingPanel(int width, int height) { this.width=width; this.height=height; setPreferredSize(new Dimension(width,height)); } public void setPaintFlag(int current) { theMode=current; paintFlag=true; } public void paintComponent(Graphics g) { if(paintFlag==false) super.paintComponent(g); else { Graphics2D g2=(Graphics2D)g; int red=(int)(Math.random()*255)+1; int green=(int)(Math.random()*255)+1; int blue=(int)(Math.random()*255)+1; g2.setColor(newColor(red,green,blue)); int w=(int)(Math.random()*(width/2)); int x=(int)(Math.random()*width)+1; int y=(int)(Math.random()*height)+1; if(theMode==0) g2.fillOval( x, y, w, w); else if(theMode==1) g2.fillRect( x, y, w, w); g.dispose(); } } private class buttonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==circle) { b1.setPaintFlag(0); b2.setPaintFlag(0); drawingPanel.repaint(); } else if(event.getSource==square) {b1.setPaintFlag(1); b2.setPaintFlag(1); drawingPanel.repaint(); } } } }
public class paintLabMain { public static void main(String[] args) { JFrame frame=new JFrame("Paint Lab"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new paintLab()); frame.pack(); frame.setVisible(true); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started