简体   繁体   English

无法在JPanel上绘制椭圆形

[英]Can't draw oval on JPanel

I'm trying to draw ovals on JPanel when mouse clicked. 当我点击鼠标时,我正试图在JPanel上绘制椭圆。 My code doesn't call paintComponent, so nothing happens on JPanel. 我的代码不调用paintComponent,因此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. 我没有使用getter和setter但我认为这不是问题所在。

If you change your repaint() to thePanel.repaint() , you should be able to see the circles being added. 如果将repaint()更改为thePanel.repaint() ,则应该能够看到正在添加的圆圈。

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 JFrame s: the one instantiated by new JFrame() , and the one instantiated by new Main() . 正如camickr在他的评论中指出的那样,你实际上有两个JFrame :一个是由new JFrame()实例化的,另一个是由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. 这就是你的repaint没有达到预期效果的原因:你所说的那个repaint不是你正在看的那个。 camickr suggests that you do not inherit your Main from JFrame , which is good advice. camickr建议您不要从JFrame继承Main ,这是一个很好的建议。

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM