无法在JPanel上绘制椭圆形

时间:2017-03-28 13:17:08

标签: java swing

点击鼠标时,我正试图在JPanel上绘制椭圆。我的代码不调用paintComponent,因此JPanel上没有任何操作。我缺少哪一部分?

 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();
    }

圈子类

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

我没有使用getter和setter但我认为这不是问题。

2 个答案:

答案 0 :(得分:3)

如果您将repaint()更改为thePanel.repaint(),您应该能够看到正在添加的圈子。

它们看起来有点偏离位置,因为你从帧的鼠标监听器中获取帧坐标,但是试图在面板坐标中绘制。

编辑:
正如camickr在他的评论中指出的那样,你实际上有两个JFrame:一个由new JFrame()实例化,一个由new Main()实例化。这就是你repaint没有达到预期效果的原因:你打电话repaint的那个不是你正在看的那个。 camickr建议您不要从Main继承JFrame,这是一个很好的建议。

答案 1 :(得分:0)

通过鼠标绘制椭圆并拖动宽度和高度

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) {

    }
}