使用 Graphics JPanel 自定义绘图

发布于 2024-11-30 00:46:06 字数 502 浏览 0 评论 0原文

我有一个自定义 JPanel,有时在我的程序中,我需要调用一个将屏幕绘制为黑色的方法,就是这样。

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

当我启动程序时,我调用这个方法。

然而,我发现有时有效,有时无效。这很奇怪。我还发现,当它不起作用时,图形对象不为空,并且宽度和高度也被正确定义(来自 getWidth() 和 getHeight())。

为什么这有时有效有时无效?

在程序中的某个时刻在 JPanel 上进行自定义绘图的正确方法是什么?像我一样使用 getGraphics() 是否正确?我的 JPanel(在某些时候)有 JComponent,但后来我删除了这些 JComponent 并进行了一些自定义图形绘制。为什么这有时会起作用?

I have a custom JPanel and sometimes throughout my program, I need to call a method which paints the screen black, that's it.

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()).

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? Is it correct to use getGraphics() as I am doing? My JPanel (at some point) has JComponents, but later on I remove those JComponents and do some custom graphics drawing. Why would this sometimes only work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-12-07 00:46:06

不要通过在 JPanel 等组件上调用 getGraphics 来获取 Graphics 对象,因为获得的 Graphics 对象不会在下次重新绘制时保留(这可能是问题的根源)。

相反,请考虑在 BufferedImage 中完成所有绘图,然后您就可以随心所欲地使用 getGraphics() 了。如果您这样做,请不要忘记在完成绘画后处理 Graphics 对象。

例如,

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
   }
}

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

Instead, consider doing all of your drawing in a BufferedImage, and then you can use getGraphics() to your heart's content. If you do this, don't forget to dispose of the Graphics object when you're done painting with it.

e.g.,

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
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文