Help for ActionPerformance and how to use HashMap.


Joined
Feb 8, 2022
Messages
4
Reaction score
0
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:
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:
Ad

Advertisements

Joined
Feb 8, 2022
Messages
4
Reaction score
0
After a long time surfing the web I think i manage to solve my ActionPerformance problems. I don`t know how to change the color if the user press the same color twice in a roll, so I just put the color black in the menu and I will think about that problem some other time. Right now I have two problems in this code:
1. The program only work if from the menu is selected запълнен четириъгълник (filled quadrilateral; fillRect, I`m sorry but I didn`t translate the words this time that`s why I write the comments on the MenuItems). I think the issue here is that the last created object is from the class that draw filled quadrilateral, this is between the lines 23-28.
2. I still have to figure it out how to use HashMap so the created shapes won`t disappear when the new shape is drawed.
I will really appreciate if someone can help with the problems. Here is the updated 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 = "";
    String putColor = "";
    NepalenChetir obj1;
    NepalenOval obj2;
    PalenChetir obj3;
    Button b;
    MenuBar mb;
    MenuItem dRect,dOval,fRect,blue,green,red,black;
    Menu shapes,colors;
    public Kursova() {
        // TODO Auto-generated constructor stub
        super("Kursova");
        x=y=x1=y1=0;
         setSize(700,500);
         obj1 = new NepalenChetir();
         add(obj1);
         obj2 = new NepalenOval();
         add(obj2);
         obj3 = new PalenChetir();
         add(obj3);
         HashMap<String,Integer> Store = new HashMap<String,Integer>();
         Store.put("coordinatesX", x);
         Store.put("coordinatesY", y);
         Store.put("coordinatesX1", x1);
         Store.put("coordinatesY1", 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
         colors =new Menu("цветове"); // Colors
         MenuItem dRect=new MenuItem("незапълнен четириъгълник");  //drawRect
         MenuItem dOval=new MenuItem("незапълнен овал");  //drawOval
         MenuItem fRect=new MenuItem("запълнен четириъгълник"); //fillRect
         MenuItem blue=new MenuItem("син");  // Blue
         MenuItem green=new MenuItem("зелен");  // Green
         MenuItem red=new MenuItem("червен"); // Red
         MenuItem black=new MenuItem("черно"); // Black
         shapes.add(dRect); 
         shapes.add(dOval); 
         shapes.add(fRect);
         colors.add(blue);
         colors.add(green);
         colors.add(red);
         colors.add(black);
         dRect.addActionListener ( new dRect1() );
         dOval.addActionListener ( new dOval2() );
         fRect.addActionListener ( new fRect3() );
         blue.addActionListener ( new Blue4() );
         green.addActionListener ( new Green5() );
         red.addActionListener ( new Red6() );
         black.addActionListener ( new Black7() );
         mb.add(shapes);
         mb.add(colors);
         g.setMenuBar(mb);
        
         g.setLayout(new GridLayout(2,0));
         b = new Button("Извеждане в конзола"); //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.drawShape();//repaint();
                obj2.drawShape();
                obj3.drawShape();
            }

            public void mouseReleased(MouseEvent e) {
                setEndPoint(x1=e.getX(), y1=e.getY());
                obj1.drawShape();
                obj2.drawShape();
                obj3.drawShape();
            }
        }
    
     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 (putColor.equals("син")) {
                g.setColor(Color.blue);
              }
               if(putColor.equals("зелен")) {
                g.setColor(Color.green);
              }
               if (putColor.equals("червен")) {
                g.setColor(Color.red);
              }
               if (putColor.equals("черно")) {
               g.setColor(Color.black);
              }
 
           if(Select.equals("запълнен четириъгълник")) {
            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 paint(Graphics g) {
            super.paint(g);
            fillPerfectRect(g,x,y,x1,y1);
       }   
        public void drawShape() {
            repaint();
        }       
    } //end of PalenChetir
    
    class NepalenOval extends Panel implements PLShape{
        public void drawPerfectOval(Graphics g, int x, int y, int x1, int y1) {
                if (putColor.equals("син")) {
                    g.setColor(Color.blue);
               }
                   if(putColor.equals("зелен")) {
                    g.setColor(Color.green);
                  }
                   if (putColor.equals("червен")) {
                    g.setColor(Color.red);
                  }
                   if (putColor.equals("черно")) {
                   g.setColor(Color.black);
                  }
                    
               if(Select.equals("незапълнен овал")) {
             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 paint(Graphics g) {
                super.paint(g);
                drawPerfectOval(g,x,y,x1,y1);
            }   
            public void drawShape() {
                repaint();
            }       
        } // end of NepalenOval
            
     class NepalenChetir extends Panel implements PLShape {
         public void drawPerfectRect(Graphics g, int x, int y, int x1, int y1) {
             if (putColor.equals("син")) {
                    g.setColor(Color.blue);
                  }
                   if(putColor.equals("зелен")) {
                    g.setColor(Color.green);
                  }
                   if (putColor.equals("червен")) {
                    g.setColor(Color.red);
                  }
                    if (putColor.equals("черно")) {
                  g.setColor(Color.black);
                 }
               if(Select.equals("незапълнен четириъгълник")) {
             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 paint(Graphics g) {
                super.paint(g);
                drawPerfectRect(g,x,y,x1,y1);
            }   
            public void drawShape() {
                repaint();
            }       
    } //end of NepalenChetir
    
     class dRect1 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
    
                 Select = e.getActionCommand();
                 MyMouseListener listener = new MyMouseListener();
                    obj1.addMouseListener(listener);
                    obj1.addMouseMotionListener(listener);
                 obj1.drawShape();
                
         }
     }
    
     class dOval2 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
            
                
                 Select = e.getActionCommand();
                 MyMouseListener listener = new MyMouseListener();
                 obj2.addMouseListener(listener);
                 obj2.addMouseMotionListener(listener);
                 obj2.drawShape();
            
         }
     }
    
     class fRect3 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
            
                
                 Select =e.getActionCommand();
                 MyMouseListener listener = new MyMouseListener();
                 obj3.addMouseListener(listener);
                 obj3.addMouseMotionListener(listener); 
                 obj3.drawShape();
            
         }
     }               
    
     class Blue4 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
             putColor =e.getActionCommand();
             if(putColor.equals("син"))  {
                 repaint();
             }
         }
     }
    
     class Green5 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
             putColor =e.getActionCommand();
             if(putColor.equals("зелен")) {
                 repaint ();
             }
         }
     }
    
     class Red6 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
             putColor =e.getActionCommand();
             if(putColor.equals("червен")) {
                 repaint();
             }                   
         }
     }
    
     class Black7 implements ActionListener {
         public void actionPerformed (ActionEvent e) {
             putColor =e.getActionCommand();
             if(putColor.equals("черно")) {
                 repaint();
             }                   
         }
     }

    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);
    //    System.out.println(Store);
        }
}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
Sorry, I don't have much time right now to look too deeply, but:
- Instead of a HashMap, use an ArrayList. It's a simple, dynamically sized array that can grow as needed.
- In each of your shape/Panel classes, store the coordinates and the Color to paint. Otherwise, you won't be able to paint them all differently. I'd add a method to your interface, or create a new interface, to update the bottom right point to call when the mouse is dragged.
- Instead of pre-creating obj1, 2, and 3, create new objects in your MousePressed or setStartPoint based on the options selected and the start point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top