Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are required, but not limited, to turn in the following source files: Assign

ID: 3551793 • Letter: Y

Question

You are required, but not limited, to turn in the following source files:

Assignment7 (The Assignment7 class extends JApplet- It is completed)
Rect.java
WhilePanel (It extends JPanel) -- to be completed.

You may add more classes or more methods as needed.

Swing/AWT

Some of the classes needed:  
JApplet, JButton, Container, JPanel, JSplitPane, JComboBox, Color, Font, Graphics, ActionListener, KeyListener, and MouseListener. You may use other classes too.

-Create an html file, say "hw7.html" with the following content:
--------------------------------------------------------

<HTML>

<HEAD>

<TITLE>Assignment 7 Applet</TITLE>

</HEAD>

<BODY>

<applet code="Assignment7.class" width=550 height=250>

</applet>

</BODY>

</HTML>

------------------------------------------------------
-Compile your java program as usual.

-In a console, type:

appletviewer hw7.html

(instead of typing "java Assignment7").

-In the TextPad, choose Tool->Run Java Applet or
press Ctrl-3 (press control key and 3 at the same time).

-In the Eclipse
choose Run->Run as Applet.

Suggested Class Diagram:

Class Diagram (download the .ppt file of the figure):

Write a Java program that constructs an Applet.

Components for assignment 7: two buttons "Undo", ?Erase?, ?a JRadioButton , and ButtonGroup where a user can select a color to draw a rectangle on a panel.

(The size of the applet here is approximately 400 X 400).

A user can move a mouse into the drawing area and drag the mouse to draw a rectangle. The default color is black, so the first time, the rectangle will appear in the drawing area with black color.

A user can continue drawing more rectangles. Note that the rectangles remain on the panel.

A user can select another color from the radio Buttons located on the left corner. After selecting a color, a user can press somewhere in the drawing panel again, and draw another rectangle on the screen with the chosen color.

When a user pushes the "Undo" button, the last drawn rectangle will be erased from the drawing area. Thus if a user keeps pushing the "Undo" button, eventually all rectangles will be erased.

Rect class

The Rect class represents a rectangle to be drawn in the panel. It contains at least the following instance variable:

Attribute name

Attribute type

Description

X

int

x-coordinate of the rectangle to be drawn.

Y

int

y-coordinate of the rectangle to be drawn.

Color

Color

color of the rectangle

Width

int

width of the rectangle

Height

Int

height of the rectangle

This class has the following constructor:

public Rect(int x1, int y1, int width, int height, Color color)

where the parameters x and y are (x,y) coordinate of where the rectangle is drawn, and color is the color of the rectangle.

This class also contains the method:

public void draw (Graphics page)

In this method the fillRect is called by passing the x, y coordinates, the width and the height.

CanvasPanel class (this is a private class inside WholePanel class)

The CanvasPanel class extends JPanel defined in javax.swing package. This is where rectangles are drawn. The Background of this panel is white. It must contain the following method.

public void paintComponent(Graphics page)

Using the parameter page you can draw rectangle with a selected color. This can be done by calling the draw method. Remember that this method need to call paintComponent method defined in its parent class.

WholePanel class

The WholePanel class organizes all components in the applet. It extends JPanel defined in javax.swing package. It contains at least the following instance variable:

Attribute name

Attribute type

Description

rectList

ArrayList

A list of rectangles drawn by the user.

This class should have a constructor:

public WholePanel()

This is where all components are arranged. Add as many instance variables as you need, and instantiate them in this constructor.? One way is to instantiate a CanvasPanel and canvas panel is where rectangles will be drawn, thus it will be listening to mouse.

ButtonListener class

The ButtonListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, based on the "Undo" button or ?Erase? the last drawn rectangle or all the rectangles should be erased. Note in case there is no rectangle drawn in the drawing panel, nothing will happen even when the "Undo" button is pushed. You should make sure that your program does not crash in this case. The ?Erase? button clears the list and repaint the canvas.

ColorListener class

The ColorListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, the color chosen by a user using JRadioButton is assigned as a color to be used to draw the rectangle.

PointListner class

The PointListener class implements MouseListener and MouseMotionListener interface. It must implement the following method:

public void mousePressed (MouseEvent event)

public void mouseReleased (MouseEvent event)???? ???

//? mouseReleased method takes the point where a mouse is released

//using the point and the pressed point to create a rectangle, add it to the ArrayList, and call paintComponent method.

public void mouseDragged(MouseEvent event) //mouseDragged method takes the point where a mouse is dragged,

//and call paintComponent nethod

Other methods from MouseListener can be left as blank.

Note that these listener classes and CanvasPanel class are defined as nested classes inside of the WholePanel class.

Download the following files and use them as a base of your program:
Assignment7

WholePanel


Step 1: Create PointListener class that implements MouseListener to get the point that a mouse is pointing. Use the point where the mouse is pushed instead of the fixed point (200,200) so that a rectangle can appear in different locations.

Step 2: Create Rect class so that each rectangle at a certain location can be stored as an object. Then create an ArrayList and store each Rect object in it. Rewrite paintComponent method so that it goes through each object in the ArrayList and draw them one by one.

Step 3: Add the "Undo" button, and create ButtonListener class to implement actionPerformed method. Also CanvasPanel needs to be created and move all functionality described so far for the WholePanel toCanvasPanel. If  the"Undo" button is pushed, delete the last element of the ArrayList and re-draw.

Step 4: Then, create ColorListener class to change selected colors. The next rectangle will be drawn with the newly selected color.



Attribute name

Attribute type

Description

X

int

x-coordinate of the rectangle to be drawn.

Y

int

y-coordinate of the rectangle to be drawn.

Color

Color

color of the rectangle

Width

int

width of the rectangle

Height

Int

height of the rectangle

Explanation / Answer


//wholepanel.java

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.ArrayList;

public class WholePanel extends JPanel

{

private Color currentColor;

private CanvasPanel canvas;

private JPanel primary, buttonPanel, leftPanel;

private JButton erase, undo;

private ArrayList rectList, tempList;

private JRadioButton[] colorRButtons;

private Color[] colors;

private int x1, y1, x2, y2, x3, y3;

private boolean mouseDragged = false;

public WholePanel()

{

rectList = new ArrayList();

//default color to draw rectangles is black

currentColor = Color.black;

//create buttons

undo = new JButton("Undo");

erase = new JButton("Erase");

undo.addActionListener(new ButtonListener());

erase.addActionListener(new ButtonListener());

//create radio buttons for 5 colors

//at the bginning, black will be chosen by default

colorRButtons = new JRadioButton[5];

colorRButtons[0] = new JRadioButton("black", true);

colorRButtons[1] = new JRadioButton("red");

colorRButtons[2] = new JRadioButton("blue");

colorRButtons[3] = new JRadioButton("green");

colorRButtons[4] = new JRadioButton("orange");

//store 5 colors in an array

colors = new Color[5];

colors[0] = Color.black;

colors[1] = Color.red;

colors[2] = Color.blue;

colors[3] = Color.green;

colors[4] = Color.orange;

//group radio buttons so that when one is selected,

//others will be unselected.

ButtonGroup group = new ButtonGroup();

for (int i=0; i<colorRButtons.length; i++)

group.add(colorRButtons[i]);

//add ColorListener to radio buttons

ColorListener listener = new ColorListener();

for (int i=0; i<colorRButtons.length; i++)

colorRButtons[i].addActionListener(listener);

//primary panel contains all radiobuttons

primary = new JPanel(new GridLayout(5,1));

for (int i=0; i<colorRButtons.length; i++)

primary.add(colorRButtons[i]);

//create buttonPanel

buttonPanel = new JPanel(new GridLayout(2,1));

buttonPanel.add(undo);

buttonPanel.add(erase);

//create leftPanel and add buttonPanel as well as the button group to it

leftPanel = new JPanel();

leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

leftPanel.add(primary);

leftPanel.add(buttonPanel);

//canvas panel is where rectangles will be drawn, thus

//it will be listening to a mouse.

canvas = new CanvasPanel();

canvas.setBackground(Color.white);

canvas.addMouseListener(new PointListener());

canvas.addMouseMotionListener(new PointListener());

JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvas);

setLayout(new BorderLayout());

add(sp);

}

//ButtonListener defined actions to take in case "Create",

//"Undo", or "Erase" is chosed.

private class ButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

if (event.getActionCommand().equals("Undo"))

{

// if undo is pressed, remove the last drawn rectangle and repaint the canvas

int size = rectList.size();

if (size >= 1)

{

rectList.remove(size - 1);

canvas.repaint();

}

}

else

{

// if erase is pressed, remove all rectangles and repaint the canvas

if (rectList.size() >= 1)

{

rectList.clear();

canvas.repaint();

}

}

}

} // end of ButtonListener

// listener class to set the color chosen by a user using

// the radio buttons.

private class ColorListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == colorRButtons[0])

currentColor = colors[0];

else if (event.getSource() == colorRButtons[1])

currentColor = colors[1];

else if (event.getSource() == colorRButtons[2])

currentColor = colors[2];

else if (event.getSource() == colorRButtons[3])

currentColor = colors[3];

else if (event.getSource() == colorRButtons[4])

currentColor = colors[4];

}

}

//CanvasPanel is the panel where rectangles will be drawn

private class CanvasPanel extends JPanel

{

//this method draws all rectangles specified by a user

public void paintComponent(Graphics page)

{

super.paintComponent(page);

//draw all rectangles

for (int i=0; i < rectList.size(); i++)

{

((Rect) rectList.get(i)).draw(page);

}

//draw an outline of the rectangle that is currently being drawn.

if (mouseDragged == true)

{

page.setColor(currentColor);

//Assume that a user will move a mouse only to left and down from

//the first point that was pushed.

page.drawRect(x1, y1, x3-x1, y3-y1);

}

}

}

public class PointListener implements MouseListener, MouseMotionListener

{


public void mousePressed (MouseEvent event)

{

//after "create" button is pushed.

mouseDragged = true;

x1 = event.getX();

y1 = event.getY();

}

public void mouseReleased (MouseEvent event)

{

mouseDragged = false;

rectList.add(new Rect(x1,y1,x3-x1,y3-y1,currentColor));

canvas.repaint();

}


public void mouseDragged(MouseEvent event)

{

x3 = event.getX();

y3 = event.getY();

canvas.repaint();

}

public void mouseClicked (MouseEvent event) {}

public void mouseEntered (MouseEvent event) {}

public void mouseExited (MouseEvent event) {}

public void mouseMoved(MouseEvent event) {}

} // end of PointListener

} // end of Whole Panel Class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote