gpt4 book ai didi

java - Java中引用的困惑

转载 作者:行者123 更新时间:2023-12-02 05:16:09 24 4
gpt4 key购买 nike

我对这段代码感到困惑:

    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 所具有的同一个 Graphics 对象?那么当gc设置颜色属性时,是改变本地副本还是改变bufferImage中的Graphics对象。根据我在网上读到的内容,它应该指的是实际对象而不是本地克隆。但是,当我调试此代码时,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);

最佳答案

我修好了!!!

我意识到调用 Graphics gc = bufferedImage.getGraphics() 每次都会返回不同的图形对象!。

因此,我在设置 bufferedImage 时设置图形,然后使用该实例!!!

关于java - Java中引用的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com