我正在为 JPanel 中的 BufferedImage 实现我自己的双缓冲,这样我就可以在 BufferedImage 中显示鼠标位置,而无需在 mousemovement 上将每个对象重新绘制到它上面。当父 JFrame 中的 JMenu 打开时,BufferedImage 会在 JMenu 之上重新绘制。
这个类不完整,只有必要的方法,
public class Foo extends JPanel implements ComponentListener {
BufferedImage bufferedImage;
long mousePosX;
long mousePoxY;
protected void paintComponent(Graphics g) {
paintComponent(g, this.xform);
}
protected void paintComponent(Graphics graphics, XFormPixel xformIn) {
bufferedImage = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
super.paintComponent(g);
//Draw lots of stuff to graphics
if(drawMouseLocation) {
int width = this.getWidth();
int height = this.getHeight();
Color origColor = g.getColor();
g.setColor(textColor);
if (cursorLocation != null) {
g.drawString("X: "+mousePosX + " Y: " + mousePosY);
}
}
g.setColor(origColor);
graphics.drawImage(bufferedImage,0,0,null);
}
public void drawMouseLocation() {
int width = this.getWidth();
int height = this.getHeight();
Image image = bufferedImage;
Graphics graphics = this.getGraphics();
Graphics g = image.getGraphics();
Color origColor = g.getColor();
g.setColor(textColor);
if (cursorLocation != null) {
g.drawString("X: "+mousePosX + " Y: " + mousePosY);
}
g.setColor(origColor);
graphics.drawImage(image,0,0,this);
}
}
还有另一种方法可以做到这一点吗?
另一个可能的问题是,当 Foo JPanel 初始化时,它有一个黑色边框,但是当绘制图像以显示鼠标位置时,边框消失了。我假设在父母身上调用 repaint() 或其他东西会解决这两个问题,但它也会在孩子身上调用 repaint,这是我试图避免的。
编辑 1:这是请求的可运行代码。创建它时,我无法使双缓冲正常工作,因此我也遇到了移动鼠标时鼠标位置闪烁的问题。
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class DrawingTestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new DrawingTestFrame();
}
public DrawingTestFrame() {
init();
}
public void init() {
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu dropMenu = new JMenu("Drop This");
dropMenu.add(needs);
dropMenu.add(to);
dropMenu.add(overlap);
menuBar.add(dropMenu);
DrawingTest test = new DrawingTest();
setTitle("Drawing Test");
add(test);
setMinimumSize(new Dimension(550,270));
pack();
setVisible(true);
}
public static Action needs = new AbstractAction("Needs") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent ae) {}};
public static Action to = new AbstractAction("To") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent ae) {}};
public static Action overlap = new AbstractAction("Overlap") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent ae) {}};
}
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class DrawingTest extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
public Component parent;
private Point mouseLocation;
private BufferedImage bufferedImage;
public DrawingTest() {
init();
}
public void init() {
this.setPreferredSize(new Dimension(100, 100));
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.addMouseListener(this);
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
mouseLocation = MouseInfo.getPointerInfo().getLocation();
DrawingTest.this.repaint();
}
public void mouseMoved(MouseEvent e) {
mouseLocation = MouseInfo.getPointerInfo().getLocation();
DrawingTest.this.drawLocation();
}
});
this.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
mouseLocation = MouseInfo.getPointerInfo().getLocation();
this.repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
protected void paintComponent(Graphics graphics) {
bufferedImage = new
BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10,10,110,110);
graphics.drawImage(bufferedImage,0,0,null);
if (mouseLocation != null) {
graphics.drawString("X: " + mouseLocation.getX() +
" Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
}
}
protected void drawLocation() {
this.getGraphics().drawImage(bufferedImage, 0,0,null);
this.getGraphics().setColor(Color.green);
if (mouseLocation != null) {
this.getGraphics().drawString("X: " + mouseLocation.getX() +
" Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
}
}
}
谢谢!