我对这段代码感到困惑:
public void setGraphicsColor(Colors colors) {
Graphics gc = bufferImage.getGraphics();
switch(colors) {
case BLACK:
gc.setColor(Color.black);
break;
case DARK_GRAY:
gc.setColor(Color.darkGray);
break;
case WHITE:
gc.setColor(Color.white);
break;
default:
break;
};
}
gc
是否会引用bufferImage
所拥有的相同图形对象?因此,当gc
设置颜色属性时,它是更改本地副本还是更改Graphics
中的bufferImage
对象。根据我在网上看到的,它应该引用实际的对象而不是本地克隆。但是,当我调试此代码时,gc.setColor()
似乎没有对bufferImage
执行任何操作。
对不起,如果这是一个愚蠢的问题。
编辑:更多背景
图形包装类MazePanel:
public class MazePanel extends Panel {
/* Panel operates a double buffer see
* http://www.codeproject.com/Articles/2136/Double-buffer-in-standard-Java-AWT
* for details
*/
Image bufferImage ;
Color color;
public enum Colors {
BLACK, DARK_GRAY, WHITE
};
public MazePanel() {
super() ;
this.setFocusable(false) ;
}
@Override
public void update(Graphics g) {
paint(g) ;
}
@Override
public void paint(Graphics g) {
g.drawImage(bufferImage,0,0,null) ;
}
/*
public void setBufferImage(Image buffer) {
bufferImage = buffer ;
}
*/
public void initBufferImage() {
bufferImage = createImage(Constants.VIEW_WIDTH, Constants.VIEW_HEIGHT);
if (null == bufferImage)
{
System.out.println("Error: creation of buffered image failed, presumedly container not displayable");
}
}
public Image getBufferImage() {
return this.bufferImage;
}
public void setBufferImage(Image im) {
this.bufferImage = im;
}
public Graphics getBufferImageGraphics() {
if (null == bufferImage)
initBufferImage() ;
return bufferImage.getGraphics() ;
}
public void setGraphicsColor(Colors colors) {
Graphics gc = bufferImage.getGraphics();
switch(colors) {
case BLACK:
gc.setColor(Color.black);
break;
case DARK_GRAY:
gc.setColor(Color.darkGray);
break;
case WHITE:
gc.setColor(Color.white);
break;
default:
break;
};
}
public void setGraphicsColor(Color color) {
Graphics gc = bufferImage.getGraphics();
gc.setColor(color);
}
public void setGraphicsColor(int r, int g, int b) {
Graphics gc = bufferImage.getGraphics();
Color color = new Color(r, g, b);
gc.setColor(color);
}
public void fillGraphicsRect(int x, int y, int width, int height) {
Graphics gc = bufferImage.getGraphics();
gc.fillRect(x, y, width, height);
}
public void update() {
paint(getGraphics()) ;
}
}
这里使用了包装器:(注释掉的代码可以工作)
public void redraw(MazePanel mp, int state, int px, int py, int view_dx, int view_dy, int walk_step, int view_offset, RangeSet rset, int ang) {
if (state != Constants.STATE_PLAY)
return ;
// new adjustment
// this.gc = mp.getBufferImageGraphics() ;
this.mazePanel.setBufferImage(mp.getBufferImage());
this.rset = rset ;
this.view_dx = view_dx ;
this.view_dy = view_dy ;
this.angle = ang ;
// calculate view
viewx = (px*map_unit+map_unit/2) + viewd_unscale(view_dx*(step_size*walk_step-view_offset));
viewy = (py*map_unit+map_unit/2) + viewd_unscale(view_dy*(step_size*walk_step-view_offset));
// update graphics
// draw black background on lower half
// gc.setColor(Color.black);
mazePanel.setGraphicsColor(Colors.BLACK);
// gc.fillRect(0, 0, view_width, view_height/2);
mazePanel.fillGraphicsRect(0, 0, view_width, view_height/2);
答案 0 :(得分:2)
我固定了它!!!
我意识到调用Graphics gc = bufferedImage.getGraphics()每次都返回一个不同的图形对象!
因此,我在设置bufferedImage时设置图形,然后使用该实例!!!
答案 1 :(得分:0)
我不是100%肯定你的问题,但gc是从BufferedImage获得的Graphics对象。如果你调用gc.setColor(...)
它本身对BufferedImage没有任何作用,但是如果你然后使用gc对象来描绘一条线,那么该行将被绘制为 on BufferedImage,并且gc设置的颜色。