简体   繁体   English

摇摆-JPanel背景颜色消失

[英]Swing - JPanel background color disappears

I'm trying to draw inside my JPanel but everytime I click, the background of my JPanel disappears. 我试图在JPanel内绘制,但是每次单击时,JPanel的背景都会消失。 It draws a line where the mouse is. 它在鼠标所在的位置绘制一条线。 I think it has something to do with the 2D graphics Can someone help? 我认为这与2D图形有关。有人可以帮忙吗?

public Brush() {

addMouseListener(this);
    addMouseMotionListener(this);
    setBackground(Color.white);


 }
    @Override
    public void paintComponent(Graphics g) {

        Graphics2D g2;
       // super.paintComponent(g);

        g2 = (Graphics2D) g;

        g2.setColor(brushColor);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
        //Ellipse2D.Double circle = new Ellipse2D.Double(p1.x,p1.y,20,20);

        g2.fillOval(p1.x,p1.y,20,20);

        }


        @Override
        public void mousePressed(MouseEvent e) {
            dragging = true;
            p1 = e.getPoint();
            repaint();
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            dragging = false;
            p1 = e.getPoint();
            repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (dragging) {
                p1 = e.getPoint();
                repaint();
            }
        }

Always call the super.paintComponent(g) method inside of your override. 始终在覆盖内调用super.paintComponent(g)方法。

You're drawing wrong then. 那你画错了。 If you want to draw a bunch of ovals, then either 如果要绘制一堆椭圆形,则可以

  • create a collection of them and draw them with a for loop in paintComponent, or 创建它们的集合并在paintComponent中使用for循环绘制它们,或者
  • draw them in a BufferedImage which is then drawn in your paintComponent method. 在BufferedImage中绘制它们,然后在paintComponent方法中绘制它们。
  • If I want to draw a curve with the mouse, I usually create an ArrayList<Point> and draw lines between contiguous points, either in paintComponent or in a BufferedImage. 如果要用鼠标绘制一条曲线,通常会创建一个ArrayList<Point>并在paintComponent或BufferedImage中的连续点之间绘制线条。

Again, your code is written to draw only one point (oval actually) within paintComponent. 同样,您的代码被编写为在paintComponent中绘制一个点(实际上是椭圆形) If coded correctly, this is all it will do. 如果编码正确,则将完成所有操作。

I suggest, the easiest thing to do is: 我建议,最简单的方法是:

  • Give you class an ArrayList<Point> 给你一个类ArrayList<Point>
  • Add points when mouse is pressed and call repaint 按下鼠标并调用重绘时添加点
  • In paintComponent , call the super method, and then use a for loop to iterate through the ArrayList. paintComponent ,调用super方法,然后使用for循环迭代ArrayList。
  • Start the loop at the Point at item 1, not 0, and then draw a line between the previous Point and the current point. 在第1项的Point(不是0)处开始循环,然后在前一个Point与当前Point之间画一条线。
  • To get fancier, you may wish to have an ArrayList<ArrayList<Point>> where you start a new ArrayList<Point> with each press of the mouse, finish it with each release and add it to the overall collection. 要想获得更好的表现,您可能希望有一个ArrayList<ArrayList<Point>> ,在这里您每次按鼠标都会启动一个新的ArrayList<Point> ,并在每次发行时完成它,并将其添加到整个集合中。 This will allow several lines to be drawn. 这将允许绘制多条线。

Why not give this a go on your own first? 为什么不先尝试一下呢?

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

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