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

I wanna add the shape creator like rectangular , square , circle etc shape creat

ID: 3774582 • Letter: I

Question

I wanna add the shape creator like rectangular , square , circle etc shape creator in this program. I tried many ways to do it but couldnt figure out the right way. Please help me do this. this is a painter program and I want to add shape creator for an extra point.. Thanks..

//Painter.java

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class Painter extends JFrame

{

private JButton blueButton, redButton, greenButton,eraserButton,clearButton;

private Container myCP;

public Paint panel;

private JPanel mainContainer;

public Painter()

{

mainContainer = new JPanel(new BorderLayout());

mainContainer.setBackground(Color.WHITE);

JPanel panel2 = new JPanel();

panel2.setLayout(new FlowLayout());

blueButton = new JButton("Blue");

blueButton.setPreferredSize(new Dimension(75,50));

panel2.add(blueButton);

blueButton.addActionListener(new

ButtonListener());

redButton = new JButton("Red");

redButton.setPreferredSize(new Dimension(75,50));

panel2.add(redButton);

redButton.addActionListener(new ButtonListener());

greenButton = new JButton("Green");

greenButton.setPreferredSize(new Dimension(75,50));

panel2.add(greenButton);

greenButton.addActionListener(new ButtonListener());

eraserButton = new JButton("Eraser");

eraserButton.setPreferredSize(new Dimension(75,50));

panel2.add(eraserButton);

eraserButton.addActionListener(new ButtonListener());

clearButton = new JButton("Clear");

clearButton.setPreferredSize(new Dimension(75,50));

panel2.add(clearButton);

clearButton.addActionListener(new ButtonListener());


panel = new Paint();

mainContainer.add(panel2, BorderLayout.NORTH);

mainContainer.add(panel, BorderLayout.CENTER);

add(mainContainer);

setVisible(true);

}


private class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{
if (e.getSource() == blueButton)

{

panel.addCoordinateList(new ColorPoints(Color.BLUE,

new Point()));

}
if (e.getSource() == redButton)

{

panel.addCoordinateList(new ColorPoints(Color.RED,

new Point()));

}


if (e.getSource() == greenButton)

{

panel.addCoordinateList(new

ColorPoints(Color.GREEN,

new Point()));

}


if (e.getSource() == eraserButton)

{

panel.addCoordinateList(new ColorPoints(panel.getBackground(),

new Point()));

}

if (e.getSource() == clearButton)

{

panel.setCoordinateList(new ArrayList<ColorPoints>());

repaint();

}

}

}

public static void main(String args[])

{

Painter myAppF = new Painter();

myAppF.setTitle("Painter");

myAppF.setVisible(true);

myAppF.setSize(500, 500);

myAppF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

System.out.println("Programmed By Aashutosh Bajgain and Sujan Gautam!");

}

}

//Paint.java

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class Paint extends JPanel

{


private boolean lineBeingDrawn;

private Point startingPoint;

private ArrayList<ColorPoints> coordinateList;

private Color selectedColor;

private ColorPoints pwc;

public Paint()

{

lineBeingDrawn = false;

startingPoint = new Point(0, 0);

selectedColor = Color.BLACK;

coordinateList = new ArrayList<ColorPoints>();

this.addMouseListener(new DrawingListener());

this.addMouseMotionListener(new DrawingListener());

}

@Override

public void paintComponent(Graphics pen)

{

super.paintComponent(pen);

for (ColorPoints c : coordinateList)

{

selectedColor = c.getColor();

pen.setColor(selectedColor);

pen.fillOval(c.getCoordinate().x, c.getCoordinate().y, 5, 5);

}

}


private class DrawingListener extends MouseAdapter

{

@Override

public void mouseClicked(MouseEvent ev)

{

Point clickedPoint = ev.getPoint();

if (!lineBeingDrawn)

{

startingPoint = clickedPoint;

repaint();

}

lineBeingDrawn = !lineBeingDrawn;

repaint();

}


@Override

public void mouseMoved(MouseEvent ev)

{

if (lineBeingDrawn)

{

Point p = ev.getPoint();

ColorPoints newPoint = new ColorPoints(selectedColor, p);

coordinateList.add(newPoint);

}

repaint();

}

}

public void setColor(Color color)

{

this.selectedColor = color;

}

public boolean isLineBeingDrawn()

{

return lineBeingDrawn;

}

public void setLineBeingDrawn(boolean lineBeingDrawn)

{

this.lineBeingDrawn = lineBeingDrawn;

}

public Point getStartingPoint()

{

return startingPoint;

}

public void setStartingPoint(Point startingPoint)

{

this.startingPoint = startingPoint;

}


public ArrayList<ColorPoints> getCoordinateList()

{

return coordinateList;

}

public void setCoordinateList(ArrayList<ColorPoints>coordinateList)

{

this.coordinateList = coordinateList;

}

public Color getSelectedColor()

{

return selectedColor;

}

public void setSelectedColor(Color selectedColor)

{

this.selectedColor = selectedColor;

}

public void addCoordinateList(ColorPoints pwc)

{

this.coordinateList.add(pwc);

}

}

//ColorPoints.java

import java.awt.Color;

import java.awt.Point;

public class ColorPoints

{

private Color pointColor;

private Point coordinate;

public ColorPoints(Color color, Point coordinate)

{

this.pointColor = color;

this.coordinate = coordinate;

}

public Color getColor()

{

return pointColor;

}

public void setColor(Color color)

{

this.pointColor = color;

}

public Point getCoordinate()

{

return coordinate;

}

public void setCoordinate(Point coordinate)

{

this.coordinate = coordinate;

}

}

Explanation / Answer

Hello Please check below Changes,

1) When Color Buttons are Clicked the Only set the Color, Don't create any new point here

      panel.setSelectedColor(Color.GREEN);

2)Don't Draw shape during Mouse Move. Draw Shape when Mouse is Dragged(Use Click the mouse and Drag it) . In General any Paint Application will start Drwaing when Mouse is Dragged on Paint Window

3)Draw line line between previous coordinate and current Coordinate. Drawing a line along
   Mouse Dragged positions makes a Shape. Ovel is Different

4)Please check Below Changed Files Pasted

//Paint.java

----------------------------------------------------

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class Paint extends JPanel

{


     private boolean lineBeingDrawn;

     private Point startingPoint;

     private ArrayList<ColorPoints> coordinateList;

     private Color selectedColor;

     private ColorPoints pwc;

     public Paint()

     {

          lineBeingDrawn = false;

          startingPoint = new Point(0, 0);

          selectedColor = Color.BLACK;

          coordinateList = new ArrayList<ColorPoints>();

          this.addMouseListener(new DrawingListener());

          this.addMouseMotionListener(new DrawingListener());

     }

     @Override

     public void paintComponent(Graphics pen)

     {

          super.paintComponent(pen);
          ColorPoints prevPoint = null;
          for (ColorPoints c : coordinateList)

          {
              if(prevPoint == null){
                  prevPoint = c;
                  continue;
              }

              selectedColor = c.getColor();

              pen.setColor(selectedColor);

              //pen.fillOval(c.getCoordinate().x, c.getCoordinate().y, 5, 5);
              /*
               * Draw line line between previous coordinate and current Coordinate. Drawing a line along
               * Mouse Dragged positions makes a Shape. Ovel is Different
               */
              pen.drawLine(prevPoint.getCoordinate().x, prevPoint.getCoordinate().y, c.getCoordinate().x, c.getCoordinate().y);
            
              prevPoint = c;

          }

     }


     private class DrawingListener extends MouseAdapter

     {

          @Override

          public void mouseClicked(MouseEvent ev)

          {
               /*
                * This is not required
                */
              /*Point clickedPoint = ev.getPoint();

              if (!lineBeingDrawn)

              {

                   startingPoint = clickedPoint;

                   repaint();

              }

              lineBeingDrawn = !lineBeingDrawn;

              repaint();*/

          }


          @Override

          public void mouseMoved(MouseEvent ev)

          {

               //Paint is done in Mouse Drag , Use Click the mouse and Drag it

          }


       @Override
       public void mouseDragged(MouseEvent ev) {
           // TODO Auto-generated method stub
           super.mouseDragged(ev);
           Point p = ev.getPoint();

            ColorPoints newPoint = new ColorPoints(selectedColor, p);

             coordinateList.add(newPoint);          

             repaint();
       }
        
        

     }

     public void setColor(Color color)

     {

          this.selectedColor = color;

     }

     public boolean isLineBeingDrawn()

     {

          return lineBeingDrawn;

     }

     public void setLineBeingDrawn(boolean lineBeingDrawn)

     {

          this.lineBeingDrawn = lineBeingDrawn;

     }

     public Point getStartingPoint()

     {

          return startingPoint;

     }

     public void setStartingPoint(Point startingPoint)

     {

          this.startingPoint = startingPoint;

     }


     public ArrayList<ColorPoints> getCoordinateList()

     {

          return coordinateList;

     }

     public void setCoordinateList(ArrayList<ColorPoints>coordinateList)

     {

          this.coordinateList = coordinateList;

     }

     public Color getSelectedColor()

     {

          return selectedColor;

     }

     public void setSelectedColor(Color selectedColor)

     {

          this.selectedColor = selectedColor;

     }

     public void addCoordinateList(ColorPoints pwc)

     {

          this.coordinateList.add(pwc);

     }

}

//Painter.java

-----------------------------------------------

//Painter.java

import java.awt.*;

import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.*;

public class Painter extends JFrame

{

     private JButton blueButton, redButton, greenButton,eraserButton,clearButton;

     private Container myCP;

     public Paint panel;

     private JPanel mainContainer;

     public Painter()

     {

          mainContainer = new JPanel(new BorderLayout());

          mainContainer.setBackground(Color.WHITE);

          JPanel panel2 = new JPanel();

          panel2.setLayout(new FlowLayout());

          blueButton = new JButton("Blue");

          blueButton.setPreferredSize(new Dimension(75,50));

          panel2.add(blueButton);

          blueButton.addActionListener(new

          ButtonListener());

          redButton = new JButton("Red");

          redButton.setPreferredSize(new Dimension(75,50));

          panel2.add(redButton);

          redButton.addActionListener(new ButtonListener());

          greenButton = new JButton("Green");

          greenButton.setPreferredSize(new Dimension(75,50));

          panel2.add(greenButton);

          greenButton.addActionListener(new ButtonListener());

          eraserButton = new JButton("Eraser");

          eraserButton.setPreferredSize(new Dimension(75,50));

          panel2.add(eraserButton);

          eraserButton.addActionListener(new ButtonListener());

          clearButton = new JButton("Clear");

          clearButton.setPreferredSize(new Dimension(75,50));

          panel2.add(clearButton);

          clearButton.addActionListener(new ButtonListener());


          panel = new Paint();

          mainContainer.add(panel2, BorderLayout.NORTH);

          mainContainer.add(panel, BorderLayout.CENTER);

          add(mainContainer);

          setVisible(true);

     }


     private class ButtonListener implements ActionListener

     {

          public void actionPerformed(ActionEvent e)

          {
              if (e.getSource() == blueButton)

              {
                   /**
                    * Set Only Color when Color Button is pressed (Red/Green/Blue....)
                    */

                   /*panel.addCoordinateList(new ColorPoints(Color.BLUE,

                             new Point()));*/
                  panel.setSelectedColor(Color.BLUE);

              }
              if (e.getSource() == redButton)

              {
                  /**
                      * Set Only Color when Color Button is pressed (Red/Green/Blue....)
                      */
                   /*panel.addCoordinateList(new ColorPoints(Color.RED,

                             new Point()));*/
                  panel.setSelectedColor(Color.RED);

              }


              if (e.getSource() == greenButton)

              {
                  /**
                      * Set Only Color when Color Button is pressed (Red/Green/Blue....)
                      */
                   /*panel.addCoordinateList(new

                    ColorPoints(Color.GREEN,

                             new Point()));*/
                  panel.setSelectedColor(Color.GREEN);

              }


              if (e.getSource() == eraserButton)

              {

                   panel.addCoordinateList(new ColorPoints(panel.getBackground(),

                             new Point()));

              }

              if (e.getSource() == clearButton)

              {

                   panel.setCoordinateList(new ArrayList<ColorPoints>());

                   repaint();

              }

          }

     }

     public static void main(String args[])

     {

          Painter myAppF = new Painter();

          myAppF.setTitle("Painter");

          myAppF.setVisible(true);

          myAppF.setSize(500, 500);

          myAppF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          System.out.println("Programmed By Aashutosh Bajgain and Sujan Gautam!");

     }

}

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