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

In Java Language: For this project, you will create a GUI drawing application. N

ID: 3695543 • Letter: I

Question

In Java Language:

For this project, you will create a GUI drawing application.

Note that there is code for a sample drawing application in the textbook. You might use that as a reference, but the requirements for this assignment are different.

Your drawing application should function as follows:

1.The user can select from three different pen colors.(1st, 2nd, 3rd choice)

2.The user can also select an "eraser" pen to clean the drawing.(4th choice)

3.The user can select another size of pen.(5th choice)

When the user clicks in the drawing space, the pen is activated. The user can then move the mouse to draw. The user clicks again to de-activate the pen.

Note: the user does not click-and-drag to draw. Instead, it's: a) click once to turn on, b) move to draw, c) click once to turn off.

Make sure you test that your program works when you change the color of the pen!

There is a clear button that clears the entire drawing space.

You need to keep track of both the location and color of a point which the user has drawn.To do this, use an ArrayList.

What kind of object should the ArrayList hold? Does Java provide a class that represents a Point with Color? If not, what can you do?

Design an object-oriented solution to keep track of an object with a location and a color.

Do not use double-buffering or the BasicStroke class. Instead, use the approach of keeping track of points instead. (We don't cover double-buffering or BasicStroke in our course, so if you don't know what this is, don't worry about it!)

*I have the code about requirement 1 now, but I feel confused about requirement 2 and requirement 3:

2.The user can also select an "eraser" pen to clean the drawing.(4th choice)

3.The user can also select another size of pen.(5th choice)

This s the code I have now:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
public class ColorPensPanel extends JPanel
{
private ArrayList colors;
private JButton red, green, blue;
private Point first, last;
private Shape shape, lineShape;
private Color color;
private int mouseClicks;
private Path2D path;

public ColorPensPanel()
{
colors = new ArrayList();
colors.add(Color.BLACK);
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
  
red = new JButton("RED");
green = new JButton("GREEN");
blue = new JButton("BLUE");
  
mouseClicks = 0;
  
setBackground(Color.white);
setPreferredSize(new Dimension(500, 500));
addMouseListener(new LineListener());
addMouseMotionListener(new LineListener());
  
add(red);
add(green);
add(blue);
  
red.addActionListener(new ButtonListener());
green.addActionListener(new ButtonListener());
blue.addActionListener(new ButtonListener());
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
  
if(first != null && last != null)
{
BasicStroke bs = new BasicStroke(1);
lineShape = bs.createStrokedShape(shape);
g2.setColor(color);
g2.draw(lineShape);
g2.fill(lineShape);
}
}

private class LineListener extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
mouseClicks++;
if(mouseClicks % 2 == 1)
{
first = me.getPoint();
path = new Path2D.Double();
shape = path;
}
else
{
first = last;
}
}

public void mouseMoved(MouseEvent me)
{
if(mouseClicks % 2 == 1)
{
last = me.getPoint();
path = (Path2D) shape;
path.moveTo(first.x, first.y);
path.lineTo(last.x, last.y);
shape = path;
first = last;
repaint();
}
}

public void mouseReleased(MouseEvent me)
{
path = (Path2D) shape;
try
{
path.closePath();
}
catch(Exception e)
{}

shape = path;
repaint();
}
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();

if(source == red)
color = colors.get(1);
else if(source == green)
   color = colors.get(2);
else if(source == blue)
color = colors.get(3);

else
color = colors.get(0);
}
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ColorPensPanel shapes = new ColorPensPanel();
JFrame frame = new JFrame("Pen Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(shapes);
frame.pack();
frame.setVisible(true);
}
});
}
}

I appreaciate it if some one can help me!

Explanation / Answer

No change will be done !!!!

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
public class ColorPensPanel extends JPanel
{
private ArrayList colors;
private JButton red, green, blue;
private Point first, last;
private Shape shape, lineShape;
private Color color;
private int mouseClicks;
private Path2D path;

public ColorPensPanel()
{
colors = new ArrayList();
colors.add(Color.BLACK);
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
  
red = new JButton("RED");
green = new JButton("GREEN");
blue = new JButton("BLUE");
  
mouseClicks = 0;
  
setBackground(Color.white);
setPreferredSize(new Dimension(500, 500));
addMouseListener(new LineListener());
addMouseMotionListener(new LineListener());
  
add(red);
add(green);
add(blue);
  
red.addActionListener(new ButtonListener());
green.addActionListener(new ButtonListener());
blue.addActionListener(new ButtonListener());
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
  
if(first != null && last != null)
{
BasicStroke bs = new BasicStroke(1);
lineShape = bs.createStrokedShape(shape);
g2.setColor(color);
g2.draw(lineShape);
g2.fill(lineShape);
}
}

private class LineListener extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
mouseClicks++;
if(mouseClicks % 2 == 1)
{
first = me.getPoint();
path = new Path2D.Double();
shape = path;
}
else
{
first = last;
}
}

public void mouseMoved(MouseEvent me)
{
if(mouseClicks % 2 == 1)
{
last = me.getPoint();
path = (Path2D) shape;
path.moveTo(first.x, first.y);
path.lineTo(last.x, last.y);
shape = path;
first = last;
repaint();
}
}

public void mouseReleased(MouseEvent me)
{
path = (Path2D) shape;
try
{
path.closePath();
}
catch(Exception e)
{}

shape = path;
repaint();
}
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();

if(source == red)
color = colors.get(1);
else if(source == green)
   color = colors.get(2);
else if(source == blue)
color = colors.get(3);

else
color = colors.get(0);
}
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ColorPensPanel shapes = new ColorPensPanel();
JFrame frame = new JFrame("Pen Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(shapes);
frame.pack();
frame.setVisible(true);
}
});
}
}

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