使用Graphics的JPanel自定义绘图 [英] JPanel custom drawing using Graphics

查看:143
本文介绍了使用Graphics的JPanel自定义绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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



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

为什么这有时会起作用,有时无法正常工作?



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

解决方案

不要通过调用像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);
$ b $ 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 {
//在缓冲图像上绘制的代码。
//不要忘记在thisJPanel
}
}

上调用repaint() b $ b

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?

解决方案

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

这篇关于使用Graphics的JPanel自定义绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆