nerdicsapo
nerdicsapo

Reputation: 427

Can't draw oval on JPanel

I'm trying to draw ovals on JPanel when mouse clicked. My code doesn't call paintComponent, so nothing happens on JPanel. Which part I'm missing?

 public class Main extends JFrame implements MouseListener{
        JPanel thePanel = new JPanel(){
            @Override
             protected void paintComponent(Graphics g)
               {
                  super.paintComponent(g);
                g.setColor(Color.red);
                for (Circle c : circles){
                      g.fillOval(c.x, c.y, c.diameter, c.diameter);
                      System.out.println(c.x + "a");
                }

               }
        };
        JFrame frame=new JFrame();
        int x,y;
        ArrayList<Circle >circles = new ArrayList<Circle>();
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main();
                }
            });
        }
    public Main(){

        frame.setSize(512,512);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.addMouseListener(this);
        frame.add(thePanel);
        frame.setVisible(true);

    }   

    @Override
    public void mouseClicked(MouseEvent e) {
            System.out.println(e.getX());
            Circle c = new Circle();
            c.x=e.getX();
            c.y=e.getY();
            c.diameter=10;
            circles.add(c);
            repaint();
    }

circle class

class Circle
    {
      public int x, y, diameter;
    }

I didn't use getters and setters but I don't think that's the problem.

Upvotes: 1

Views: 512

Answers (2)

Yazan Abu Al Failat
Yazan Abu Al Failat

Reputation: 1

Draw oval and dragged width and height by mouse

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Point;

public class DrawOval extends Applet implementsMouseListener,MouseMotionListener {

    private int xstart,xend,ystart,yend;
    private boolean flag=false;
    private int width,heigth;
    private Point clickPoint;
    private Point dragPoint;
    private int x,y;

    public void init() {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    public void paint(Graphics p) {
        if (flag) {
            p.drawOval(x, y, width, heigth);
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {

    }

    @Override
    public void mousePressed(MouseEvent me) {
        clickPoint = me.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        dragPoint = me.getPoint();
        x = Math.min(clickPoint.x, dragPoint.x);
        y = Math.min(clickPoint.y, dragPoint.y);
        width = Math.max(clickPoint.x, dragPoint.x) - x;
        heigth = Math.max(clickPoint.y, dragPoint.y) - y;

        flag = true;
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent me) {

    }
}

Upvotes: 0

khelwood
khelwood

Reputation: 59096

If you change your repaint() to thePanel.repaint(), you should be able to see the circles being added.

They will appear to be a little off-position, because you are getting frame-coordinates from your frame's mouse listener, but trying to paint in panel-coordinates.

Edit:
As camickr pointed out in his comment, you actually have two JFrames: the one instantiated by new JFrame(), and the one instantiated by new Main(). This is the reason your repaint was not having the desired effect: the one you were calling repaint on was not the one you were looking at. camickr suggests that you do not inherit your Main from JFrame, which is good advice.

Upvotes: 3

Related Questions