How to draw shapes by using mouse

Joined
Feb 8, 2022
Messages
5
Reaction score
0
Hello! I`m working on a project that allow the users to draw shapes, but for some reason i can`t get the coоrdinates of the mouse, if i write the coordinates of shapes the will draw the shape with the coordinates and sizes I choose. Here is the code i wrote so far.
Interface:
Java:
import java.awt.*;
import java.awt.event.*;

public interface PLShape {
public abstract void drawShape();
}

Code:
Java:
import java.awt.*;
import java.awt.event.*;
 public class Kursova extends Frame implements ActionListener  {
    int x,y,x1,y1;
public Kursova() {
        x=y=x1=y1=0;
        Frame f=new Frame("Kursova");
         f.setSize(700,500);
         MyMouseListener listener = new MyMouseListener();
            f.addMouseListener(listener);
            f.addMouseMotionListener(listener);
         NepalenChetir obj = new NepalenChetir();
         f.add(obj);
         obj.drawShape();
             f.setVisible(true);
         f.addWindowListener(new WindowAdapter () {
             public void windowClosing(WindowEvent e) {
                     System.exit(0);
             }
     });       
} //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);
        }
      
// MyMouseListener class

class MyMouseListener extends MouseAdapter {

            public void mousePressed(MouseEvent e) {
                setStartPoint(e.getX(), e.getY());
            }

            public void mouseDragged(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                repaint();
            }

            public void mouseReleased(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                repaint();
            }
        } // end of class MyMouseListener

// The class that used to draw shape


class NepalenChetir extends Panel implements PLShape {
        //int x,y,x1,y1;
         public void drawPerfectRect(Graphics g, int x, int y, int x1, int y1) {
             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);
            //    g.drawRect(10,20,200,150);
// if i remove the comment of comment of g.drawRect(10,20,200,150);
//the program will draw the shapes with this sizes
            }
            public void drawShape() {repaint();}       
      
    }

public static void main(String[] args) {
        new Kursova();
    }
}
 
Last edited:
Joined
Mar 3, 2021
Messages
240
Reaction score
30
The main problem is that your NepalenChetir object isn't actually getting repainted when you repaint the window. I'm not really sure why. A Panel isn't really meant for drawing right on, but it didn't change when I switched it to a Canvas object. To fix it, I just stored the NepalenChetir object and called repaint on it explicitly. I also removed the extra Frame you were building. Your main class already extends Frame, so I just called all the same functions on that instead (and called the super constructor). We can dig more deeply into why it didn't repaint if you'd like to figure it out. Also, I'd recommend at least switching to the Swing framework instead of AWT. To start, it's 99% the same except all the components start with J (JFrame instead of Frame, etc), but it's got benefits in the long run.

Java:
import java.awt.*;
import java.awt.event.*;



 public class Kursova extends Frame /*implements ActionListener*/  {
    int x,y,x1,y1;
    NepalenChetir obj;
public Kursova() {
    super("Kursova");
        x=y=x1=y1=0;
         setSize(700,500);
         obj = new NepalenChetir();
         add(obj);
         obj.drawShape();
         MyMouseListener listener = new MyMouseListener();
            obj.addMouseListener(listener);
            obj.addMouseMotionListener(listener);
             setVisible(true);
         addWindowListener(new WindowAdapter () {
             public void windowClosing(WindowEvent e) {
                     System.exit(0);
             }
     });     
} //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);
        }
      
// MyMouseListener class

class MyMouseListener extends MouseAdapter {

            public void mousePressed(MouseEvent e) {
                setStartPoint(e.getX(), e.getY());
            }

            public void mouseDragged(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                obj.repaint();
            }

            public void mouseReleased(MouseEvent e) {
                setEndPoint(e.getX(), e.getY());
                obj.repaint();
            }
        } // end of class MyMouseListener

// The class that used to draw shape


class NepalenChetir extends Panel implements PLShape {
        //int x,y,x1,y1;
         public void drawPerfectRect(Graphics g, int x, int y, int x1, int y1) {
             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);
                //g.drawRect(10,20,200,150);
// if i remove the comment of comment of g.drawRect(10,20,200,150);
//the program will draw the shapes with this sizes
            }
            public void drawShape() {repaint();}       
      
    }

public static void main(String[] args) {
        new Kursova();
    }
}
 

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

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top