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

Need help in JAVA For this project, you will create a GUI drawing application. N

ID: 3857945 • Letter: N

Question

Need help in JAVA

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

Note that because of when final grades are due, there are no late submissions accepted of this project.

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:When the user clicks in the drawing space, the pen is turned on. The user can then move the mouse to draw. The user clicks again to turn the pen off. (40 points)

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

The user does not have to hold down the mouse to draw!

The user can select from three different pen colors. (15 points)

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

The user can also select an "eraser" pen. (10 points)

Hint: Do you really need to "erase" a point? Or is there a shortcut you can use?

There is a clear button that clears the entire drawing space. (10 points)

You must keep track of both the location and color of a point on which the user has drawn. (25 points)

Use an ArrayList to keep track of this.

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?

For full credit, design an object-oriented solution!

Extra Credit (10 points)

Include an additional drawing functionality. Be creative with this!! (Your additional functionality cannot be just choosing another pen color- that will not count for any extra credit.)

Notes and Submission

For full credit, follow good principles of programming, object-oriented design, and inheritance.

Use comments to provide a brief explanations where appropriate.

Part of the purpose of this project is to design what classes you will need and how the classes should communicate with each other.

Because of this, I have not provided a starting file.

You might review the program that changed the color of a smiley face if you're unsure where to get started.

I have provided an executable jar sample file.

You can download this file and then double-click on it to run the program.

To submit your program, zip all .java files together and upload the zip file.

Explanation / Answer

Complete Program:

// File: ColorPensPanel.java
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<Color> 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<Color>();
  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 == blue)
    color = colors.get(2);
   else if(source == green)
    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