Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this assignment is GUI program Class Implements Class Extends Inner Classes Drawing Paint Component STEP 1. (a). Create a class called paintLabMain.

The purpose of this assignment is

GUI program

Class Implements

Class Extends

Inner Classes

Drawing Paint Component

STEP 1. (a). Create a class called paintLabMain.

(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:

(a). 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

(b). 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

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.

Can someone please help with this Lab. Thank you so much.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ how might this lead to faster growth in productivity?

Answered: 1 week ago