SlideShare a Scribd company logo
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());
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 coordinateList;
private Color selectedColor;
private ColorPoints pwc;
public Paint()
{
lineBeingDrawn = false;
startingPoint = new Point(0, 0);
selectedColor = Color.BLACK;
coordinateList = new ArrayList();
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 getCoordinateList()
{
return coordinateList;
}
public void setCoordinateList(ArrayListcoordinateList)
{
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;
}
}
Solution
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 coordinateList;
private Color selectedColor;
private ColorPoints pwc;
public Paint()
{
lineBeingDrawn = false;
startingPoint = new Point(0, 0);
selectedColor = Color.BLACK;
coordinateList = new ArrayList();
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 getCoordinateList()
{
return coordinateList;
}
public void setCoordinateList(ArrayListcoordinateList)
{
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());
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!");
}
}

More Related Content

DOCX
Applications
DOCX
The Noland national flag is a square showing the following pattern. .docx
PDF
package chapter15;import javafx.application.Application;import j.pdf
DOCX
Android Studio (Java)The SimplePaint app (full code given below).docx
PPTX
Data Types and Processing in ES6
PDF
can you add a delete button and a add button to the below program. j.pdf
PPT
C++: Constructor, Copy Constructor and Assignment operator
PDF
Mashup caravan android-talks
Applications
The Noland national flag is a square showing the following pattern. .docx
package chapter15;import javafx.application.Application;import j.pdf
Android Studio (Java)The SimplePaint app (full code given below).docx
Data Types and Processing in ES6
can you add a delete button and a add button to the below program. j.pdf
C++: Constructor, Copy Constructor and Assignment operator
Mashup caravan android-talks

Similar to I wanna add the shape creator like rectangular , square , circle etc.pdf (20)

PDF
I dont know what is wrong with this roulette program I cant seem.pdf
PDF
How do you update an address according to this code- Currently- I fig.pdf
PDF
Creating an Uber Clone - Part IX.pdf
PDF
C++ L11-Polymorphism
PDF
There is something wrong with my program-- (once I do a for view all t.pdf
PDF
Bindings: the zen of montage
PDF
This is a C# project . I am expected to create as this image shows. .pdf
PDF
Create a java project that - Draw a circle with three random init.pdf
PDF
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
PDF
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
PDF
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
PPT
Paradigmas de Linguagens de Programacao - Aula #4
PPT
Introduction to AspectJ
PDF
Composite Pattern
PDF
Please implement in Java. comments would be appreciated 5.pdf
DOCX
Stack prgs
PDF
public class Point {   Insert your name here    private dou.pdf
PDF
Maps - Part 3 - Transcript.pdf
PDF
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PDF
Model View Intent on Android
I dont know what is wrong with this roulette program I cant seem.pdf
How do you update an address according to this code- Currently- I fig.pdf
Creating an Uber Clone - Part IX.pdf
C++ L11-Polymorphism
There is something wrong with my program-- (once I do a for view all t.pdf
Bindings: the zen of montage
This is a C# project . I am expected to create as this image shows. .pdf
Create a java project that - Draw a circle with three random init.pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Paradigmas de Linguagens de Programacao - Aula #4
Introduction to AspectJ
Composite Pattern
Please implement in Java. comments would be appreciated 5.pdf
Stack prgs
public class Point {   Insert your name here    private dou.pdf
Maps - Part 3 - Transcript.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
Model View Intent on Android
Ad

More from herminaherman (20)

PDF
Write an equation for the height of the point P above the ground as .pdf
PDF
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
PDF
Which of the following is considered to be a distributed denial of s.pdf
PDF
Which of the folllowing statements about the solubility of 1- propan.pdf
PDF
What type of mutation that impacts splicing is a significant contrib.pdf
PDF
What data indicate that all three germ layers are specified in the b.pdf
PDF
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
PDF
Use provided HTML file.Create JavaScript file to validate user i.pdf
PDF
Two four-sided dice are rolled. One die has the letters A through D..pdf
PDF
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
PDF
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
PDF
Private and Public Health are working together through many program .pdf
PDF
Name the two standards that are supported by major DBMSs for distrib.pdf
PDF
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
PDF
Mean can be computed for variables with ordinal-level measurements..pdf
PDF
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
PDF
IV. Which compound would have the higher water solubility, tetrahydro.pdf
PDF
Interseting discussion topic,want to chime inDescribe a symbol .pdf
PDF
IN JAVA Write a program to create a binary data file named RandomInt.pdf
PDF
I keep on get a redefinition error and an undefined error.Customer.pdf
Write an equation for the height of the point P above the ground as .pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Which of the following is considered to be a distributed denial of s.pdf
Which of the folllowing statements about the solubility of 1- propan.pdf
What type of mutation that impacts splicing is a significant contrib.pdf
What data indicate that all three germ layers are specified in the b.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdf
Two four-sided dice are rolled. One die has the letters A through D..pdf
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Private and Public Health are working together through many program .pdf
Name the two standards that are supported by major DBMSs for distrib.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
Mean can be computed for variables with ordinal-level measurements..pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdf
IN JAVA Write a program to create a binary data file named RandomInt.pdf
I keep on get a redefinition error and an undefined error.Customer.pdf
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Sports Quiz easy sports quiz sports quiz
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Lesson notes of climatology university.
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Types and Its function , kingdom of life
Sports Quiz easy sports quiz sports quiz
STATICS OF THE RIGID BODIES Hibbelers.pdf
Computing-Curriculum for Schools in Ghana
human mycosis Human fungal infections are called human mycosis..pptx
Lesson notes of climatology university.
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pre independence Education in Inndia.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Abdominal Access Techniques with Prof. Dr. R K Mishra

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

  • 1. 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));
  • 2. 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())); }
  • 3. if (e.getSource() == eraserButton) { panel.addCoordinateList(new ColorPoints(panel.getBackground(), new Point())); } if (e.getSource() == clearButton) { panel.setCoordinateList(new ArrayList()); 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 coordinateList; private Color selectedColor; private ColorPoints pwc; public Paint()
  • 4. { lineBeingDrawn = false; startingPoint = new Point(0, 0); selectedColor = Color.BLACK; coordinateList = new ArrayList(); 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
  • 5. 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 getCoordinateList() { return coordinateList; }
  • 6. public void setCoordinateList(ArrayListcoordinateList) { 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;
  • 7. } public Point getCoordinate() { return coordinate; } public void setCoordinate(Point coordinate) { this.coordinate = coordinate; } } Solution 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 coordinateList; private Color selectedColor; private ColorPoints pwc;
  • 8. public Paint() { lineBeingDrawn = false; startingPoint = new Point(0, 0); selectedColor = Color.BLACK; coordinateList = new ArrayList(); 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
  • 9. { @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)
  • 10. { 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 getCoordinateList() { return coordinateList; } public void setCoordinateList(ArrayListcoordinateList) { this.coordinateList = coordinateList; } public Color getSelectedColor() { return selectedColor; } public void setSelectedColor(Color selectedColor) { this.selectedColor = selectedColor; }
  • 11. 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);
  • 12. 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,
  • 13. 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()); 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!"); }
  • 14. }