简体   繁体   English

JPanel 使用 Graphics 自定义绘图

[英]JPanel custom drawing using Graphics

I have a custom JPanel and sometimes throughout my program, I need to call a method which paints the screen black, that's it.我有一个自定义的 JPanel,有时在我的程序中,我需要调用一个将屏幕涂黑的方法,就是这样。

public void clearScreen() {
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.fillRect(0,0,getWidth(),getHeight());
}

When I launch the program, I call this method.当我启动程序时,我调用了这个方法。

However, I find that sometimes it works, and sometimes it doesn't.但是,我发现有时它有效,有时则无效。 It's very odd.这很奇怪。 I also found out that when it doesn't work, the graphics object is NOT null, and the width and height are also correctly defined (from getWidth() and getHeight()).我还发现,当它不起作用时,图形 object 不是 null,并且宽度和高度也正确定义(来自 getWidth() 和 getHeight())。

Why would this sometimes work and sometimes not work?为什么这有时会起作用,有时却不起作用?

What is the correct way to make a custom drawing on my JPanel at some point in the program?在程序中的某个时刻在我的 JPanel 上进行自定义绘图的正确方法是什么? Is it correct to use getGraphics() as I am doing?像我一样使用 getGraphics() 是否正确? My JPanel (at some point) has JComponents, but later on I remove those JComponents and do some custom graphics drawing.我的 JPanel(在某些时候)有 JComponents,但后来我删除了这些 JComponents 并进行了一些自定义图形绘制。 Why would this sometimes only work?为什么这有时只会起作用?

Don't get your Graphics object by calling getGraphics on a component such as a JPanel since the Graphics object obtained will not persist on the next repaint (which is likely the source of your problems).不要通过在 JPanel 等组件上调用 getGraphics 来获取图形 object,因为获得的图形 object 不会在下一次重绘时持续存在(这可能是问题的根源)。

Instead, consider doing all of your drawing in a BufferedImage, and then you can use getGraphics() to your heart's content.相反,请考虑在 BufferedImage 中进行所有绘图,然后您可以使用 getGraphics() 来满足您的需求。 If you do this, don't forget to dispose of the Graphics object when you're done painting with it.如果您这样做,请不要忘记在完成绘画后处理掉 Graphics object。

eg,例如,

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyPaint extends JPanel {
   public static final int IMG_WIDTH = 400;
   public static final int IMG_HEIGHT = IMG_WIDTH;

   private BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
            BufferedImage.TYPE_INT_ARGB);

   public MyPaint() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
   }

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

   public void clearScreen() {
      Graphics g = image.getGraphics();
      g.setColor(Color.black);
      g.fillRect(0, 0, image.getWidth(), image.getHeight());
      g.dispose();
      repaint();
   }

   private class MyMouseAdapter extends MouseAdapter {
      // code to draw on the buffered image. 
      // Don't forget to call repaint() on the "this" JPanel
   }
}

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

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