我正在尝试实现一种方法来绘制网格的特定部分.为此,我重写了paintComponent方法:
public class Frame extends JPanel {
...
@Override
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.clearRect(0, 0, getWidth(), getHeight());
this.rectWidth = getWidth() / this.NUM_COLUMNS;
this.rectHeight = getHeight() / this.NUM_ROWS;
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLUMNS; j++) {
int x = i * this.rectWidth;
int y = j * this.rectHeight;
g.setColor( Color.WHITE );
g.fillRect( x, y, this.rectWidth, this.rectHeight );
}
}
}
}
很好,但是然后我想在函数调用上绘制特定部分,如下所示:
public void specificPaint( int coordinateX, int coordinateY, Color color ){
Graphics g = this.getGraphics();
int x = coordinateX * this.rectWidth;
int y = coordinateY * this.rectHeight;
g.setColor( color );
g.fillRect( x, y, this.rectWidth, this.rectWidth);
}
电话应该像
// TESTING
this.modelMap.specificPaint( 40,40,Color.RED );
this.modelMap.specificPaint( 10,10,Color.RED );
this.modelMap.specificPaint( 20,25,Color.BLUE );
我在Graphics对象中收到一个空指针错误,为什么我不能恢复和使用它?
有没有更好的方法?
解决方法:
从组件获取Graphics对象时,请勿执行以下操作:
Graphics g = this.getGraphics();
这样获得的Graphics对象将不会长期存在,这可能导致NPE(就像您正在获取的那样)或图像在重新绘制时不持久.而是在paintComponent方法中进行绘制.请注意,您可以调用repaint(…)并通过传入Rectangle参数来指定要绘制的特定Rectangle.
请注意,您可以在BufferedImage上调用getGraphics()并对其进行绘制,然后再在paintComponent方法中绘制BufferedImage,以进行不移动的点画.
不相关的建议:避免调用您的Frame类,因为如果不小心,可能会导致与java.awt.Frame类的名称冲突.
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class MyPanel extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private int cols;
private int rows;
private static final Color BG = Color.BLACK;
private static final int GAP = 1;
private BufferedImage img;
private int rectWidth;
private int rectHeight;
public MyPanel(int rows, int cols) {
this.cols = cols;
this.rows = rows;
img = createMyImage();
}
public void specificPaint(int coordinateX, int coordinateY, Color color) {
Graphics g = img.getGraphics(); // get img's Graphics object
int x = coordinateX * this.rectWidth + GAP;
int y = coordinateY * this.rectHeight + GAP;
g.setColor(color);
g.fillRect(x, y, rectWidth - 2 * GAP, rectWidth - 2 * GAP);
g.dispose();
repaint();
}
private BufferedImage createMyImage() {
img = new BufferedImage(PREF_W, PREF_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setBackground(BG);
g2.clearRect(0, 0, img.getWidth(), img.getHeight());
this.rectWidth = img.getWidth() / cols;
this.rectHeight = img.getHeight() / rows;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int x = i * this.rectWidth + GAP;
int y = j * this.rectHeight + GAP;
g2.setColor(Color.WHITE);
g2.fillRect(x, y, this.rectWidth - 2 * GAP, this.rectHeight - 2 * GAP);
}
}
g2.dispose();
return img;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
// if you need to draw changing non-static images, do it here
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
int w = img.getWidth();
int h = img.getHeight();
return new Dimension(w, h);
}
private static void createAndShowGui() {
MyPanel modelMap = new MyPanel(50, 50);
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(modelMap);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
modelMap.specificPaint( 40,40,Color.RED );
modelMap.specificPaint( 10,10,Color.RED );
modelMap.specificPaint( 20,25,Color.BLUE );
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}