Hello! I`m trying to improve the project that allow the user to draw shapes by dragging his mouse. I add menu, so the user can choose what shape to create and in what color. If the user chose to create shape without choosing color, the shape will be created in black, also if the user choose the same color twice in a row the color will be black. If the user choose the color without choosing the shape the user won`t be able to draw (but I doubt that is possible with the classes I write). My main issue is that I don`t know what exactly to write in ActionPerformance. If you have idea how to improve the classes that draws shapes feel free to change it, but the implemented method drawShape have to stay. Also I want the created shapes to not disappear when the new one is created, for that purpose I have to use HashMap but I have the big lack of knowledge in that sphere. I tried to find a similar program that use HashMap but i couldn't find anything. Also I want to print the coordinates of all created shapes in the console if that is possible or atleast only the coordinates of the last shape is fine too. I will be very grateful if you help me with my problems or atleast some guidance.
Here is the code:
the interface code:
Here is the code:
Java:
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
public class Kursova extends Frame implements ActionListener {
int x,y,x1,y1;
// String Select = "";
NepalenChetir obj1;
NepalenOval obj2;
PalenChetir obj3;
Button b;
MenuBar mb;
MenuItem dRect,dOval,fRect,blue,green,red;
Menu shapes,colors;
public Kursova() {
// TODO Auto-generated constructor stub
super("Kursova");
x=y=x1=y1=0;
setSize(700,500);
// HashMap<Integer,Integer> line = new HashMap<Integer,Integer>();
// line.put(x, y);
// HashMap<Integer,Integer> draw = new HashMap<Integer,Integer>();
// draw.put(x1, y1);
setVisible(true);
addWindowListener(new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Frame g=new Frame("Instruments");
g.setSize(300,200);
g.setVisible(true);
g.addWindowListener(new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MenuBar mb=new MenuBar();
shapes =new Menu("shapes"); // Shapes
colors =new Menu("colors"); // Colors
MenuItem dRect=new MenuItem("drawRect"); //drawRect
MenuItem dOval=new MenuItem("drawOval"); //drawOval
MenuItem fRect=new MenuItem("fillRect"); //fillRect
MenuItem blue=new MenuItem("blue"); // Blue
MenuItem green=new MenuItem("green"); // Green
MenuItem red=new MenuItem("red"); // Red
shapes.add(dRect);
shapes.add(dOval);
shapes.add(fRect);
colors.add(blue);
colors.add(green);
colors.add(red);
dRect.addActionListener ( new dRect1() );
dOval.addActionListener ( new dOval2() );
fRect.addActionListener ( new fRect3() );
blue.addActionListener ( new Blue4() );
green.addActionListener ( new Green5() );
red.addActionListener ( new Red6() );
mb.add(shapes);
mb.add(colors);
g.setMenuBar(mb);
g.setLayout(new GridLayout(2,0));
b = new Button("print in the console"); //print the value x,y,x1,y1 in the console
b.addActionListener(this);
g.add(b);
} //end of constructor Kursova
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x1 = (x);
y1 = (y);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(x=e.getX(),y=e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
obj1.repaint();
obj2.repaint();
obj3.repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(x1=e.getX(), y1=e.getY());
obj1.repaint();
obj2.repaint();
obj3.repaint();
}
}
class PalenChetir extends Panel implements PLShape{
boolean blue = false, green = false, red = false;
public void fillPerfectRect(Graphics g, int x, int y, int x1, int y1) {
if (blue) {
g.setColor(Color.blue);
}
else {
g.setColor(Color.black);
}
if(green) {
g.setColor(Color.green);
}
else {
g.setColor(Color.black);
}
if (red) {
g.setColor(Color.red);
}
else {
g.setColor(Color.black);
}
int px = Math.min(x,x1);
int py = Math.min(y,y1);
int pw=Math.abs(x-x1);
int ph=Math.abs(y-y1);
g.fillRect(px, py, pw, ph);
}
public void setBlue()
{
if(blue){
blue = false;
}
else { blue = true; }
}
public void setGreen()
{
if(green){
green = false;
}
else { green = true; }
}
public void setRed()
{
if(red){
red = false;
}
else { red = true; }
}
public void paint(Graphics g) {
super.paint(g);
fillPerfectRect(g,x,y,x1,y1);
//g.drawRect(10,20,200,150);
}
public void drawShape() {
repaint();
}
} //end of PalenChetir
class NepalenOval extends Panel implements PLShape{
boolean blue = false, green = false, red = false;
public void drawPerfectOval(Graphics g, int x, int y, int x1, int y1) {
if (blue) {
g.setColor(Color.blue);
}
else {
g.setColor(Color.black);
}
if(green) {
g.setColor(Color.green);
}
else {
g.setColor(Color.black);
}
if (red) {
g.setColor(Color.red);
}
else {
g.setColor(Color.black);
}
int px = Math.min(x,x1);
int py = Math.min(y,y1);
int pw=Math.abs(x-x1);
int ph=Math.abs(y-y1);
g.drawOval(px, py, pw, ph);
}
public void setBlue()
{
if(blue){
blue = false;
}
else { blue = true; }
}
public void setGreen()
{
if(green){
green = false;
}
else { green = true; }
}
public void setRed()
{
if(red){
red = false;
}
else { red = true; }
}
public void paint(Graphics g) {
super.paint(g);
drawPerfectOval(g,x,y,x1,y1);
//g.drawRect(10,20,200,150);
}
public void drawShape() {
repaint();
}
} // end of NepalenOval
class NepalenChetir extends Panel implements PLShape {
boolean blue = false, green = false, red = false;
public void drawPerfectRect(Graphics g, int x, int y, int x1, int y1) {
if (blue) {
g.setColor(Color.blue);
}
else {
g.setColor(Color.black);
}
if(green) {
g.setColor(Color.green);
}
else {
g.setColor(Color.black);
}
if (red) {
g.setColor(Color.red);
}
else {
g.setColor(Color.black);
}
int px = Math.min(x,x1);
int py = Math.min(y,y1);
int pw=Math.abs(x-x1);
int ph=Math.abs(y-y1);
g.drawRect(px, py, pw, ph);
}
public void setBlue()
{
if(blue){
blue = false;
}
else { blue = true; }
}
public void setGreen()
{
if(green){
green = false;
}
else { green = true; }
}
public void setRed()
{
if(red){
red = false;
}
else { red = true; }
}
public void paint(Graphics g) {
super.paint(g);
drawPerfectRect(g,x,y,x1,y1);
//g.drawRect(10,20,200,150);
}
public void drawShape() {
repaint();
}
} //end of NepalenChetir
class dRect1 extends Panel implements ActionListener {
public void actionPerformed (ActionEvent k) {
if (k.getSource() == dRect) {
obj1 = new NepalenChetir();
add(obj1);
obj1.drawShape();
MyMouseListener listener = new MyMouseListener();
obj1.addMouseListener(listener);
obj1.addMouseMotionListener(listener);
}
}
}
class dOval2 extends Panel implements ActionListener {
public void actionPerformed (ActionEvent k) {
if (k.getSource() == dOval) {
obj2 = new NepalenOval();
obj2.drawShape();
MyMouseListener listener = new MyMouseListener();
obj2.addMouseListener(listener);
obj2.addMouseMotionListener(listener);
}
}
}
class fRect3 extends Panel implements ActionListener {
public void actionPerformed (ActionEvent k) {
if (k.getSource() == fRect) {
obj3 = new PalenChetir();
add(obj3);
obj3.drawShape();
MyMouseListener listener = new MyMouseListener();
obj3.addMouseListener(listener);
obj3.addMouseMotionListener(listener);
}
}
}
class Blue4 implements ActionListener {
public void actionPerformed (ActionEvent e) {
/*this classes Blue4, Green5, Red6 should to call methods
setBlue, setGreen, setRed but I don`t know how to get the
last selected shape. If you have better idea how to improve
the classes feel free to change it,but the implemented
method drawShape have to stay. */
}
}
class Green5 implements ActionListener {
public void actionPerformed (ActionEvent e) {
}
}
class Red6 implements ActionListener {
public void actionPerformed (ActionEvent e) {
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Kursova();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("x= "+x+" y= "+y);
System.out.println("x1= "+x1+" y1= "+y1);
}
}
the interface code:
Java:
import java.awt.*;
import java.awt.event.*;
public interface PLShape {
public abstract void drawShape();
}
Last edited: