JPanel无法正确绘制

Ste*_*oth 4 java geometry swing jpanel paintcomponent

我是Java和Swing的新手,这也是我的第一篇文章,如果它没有多大意义,那就很抱歉.

我想要做的是当我点击JPanel时,我希望它在我点击的位置添加一个圆圈.目前,所有似乎都发生的事情就是当我点击时,我要添加的JPanel中会出现一个小的灰色方块,但我似乎找不到任何方法让它像圆圈一样绘制.

我有一个扩展JPanel的类叫做"Ball",这是我点击时添加的内容.目前,我并不担心它在正确的位置,只是为了正确地抽球.以下是我的"Ball"类的代码:

package paintsliders;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

class Ball extends JPanel{

private int x,y,w,h;

//I will use this constructor to put the ball in the correct location later.
Ball(){
    /*this.w = 100;
    this.h = 100;
    this.x = 200;
    this.y = 200;*/
}

//draw the ball
    @Override
public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawOval(200,200,10,10);
      g.setColor(Color.RED);
    }

}
Run Code Online (Sandbox Code Playgroud)

我可以猜测它与paintComponent方法有关,但我看到的任何地方似乎都没有解决方案.

任何帮助都会很棒,谢谢!

Mad*_*mer 5

Graphcis背景下已经被翻译,以满足组件应该出现在它的父容器中的X/Y位置,这意味着顶部,左上角Graphics的内语境paintComponent方法实际上为0x0.

你需要为球定义一些大小,你在10x10处绘画,这表明你的球组件应该返回preferredSize10x10

public Dimension getPreferredSize() {
    return new Dimension(10, 10);
}
Run Code Online (Sandbox Code Playgroud)

当球被添加到父容器时,您将负责为球提供适当的布局细节...

public void mouseClicked(MouseEvent evt) {
    Point p = evt.getPoint();
    Ball ball = new Ball();
    Dimension size = ball.getPreferredSize();
    ball.setBounds(new Rectangle(p, size));
    add(ball);
}
Run Code Online (Sandbox Code Playgroud)

当然,这假设您null为父容器设置了布局

更新

就像是...

见现场运行

public class PaintBalls {

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

    public PaintBalls() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Board());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Board extends JPanel {

        public Board() {
            setLayout(null);
            setBackground(Color.WHITE);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    Point p = e.getPoint();
                    Ball ball = new Ball();
                    Dimension size = ball.getPreferredSize();
                    p.x -= size.width / 2;
                    p.y -= size.height / 2;

                    ball.setBounds(new Rectangle(p, size));
                    add(ball);
                    repaint();
                }

            });
        }

    }

    public class Ball extends JPanel {

        public Ball() {
            setOpaque(false);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(10, 10);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fillOval(0, 0, 10, 10);
            g2d.dispose();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)