简体   繁体   English

关于Java中引用的困惑

[英]Confusion about references in Java

I am confused about this piece of code: 我对这段代码感到困惑:

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

Would gc refer to the same Graphics object that bufferImage has? gc引用bufferImage具有的相同Graphics对象? So when gc is setting the color attributes, is it changing a local copy or changing the Graphics object in bufferImage . 因此,当gc设置颜色属性时,它是更改本地副本还是更改bufferImageGraphics对象。 According to what I read online, it should refer to the actual object instead of a local clone. 根据我在网上阅读的内容,它应该引用实际对象而不是本地克隆。 However, when I debugged this code, gc.setColor() does not seem to be doing anything to bufferImage . 但是,当我调试此代码时, gc.setColor()似乎没有对bufferImage做任何事情。 Sorry if this is a dumb question. 抱歉,这是一个愚蠢的问题。

EDIT: More context 编辑:更多上下文

The graphics wrapper class MazePanel: 图形包装器类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()) ;
        }
}

Here the wrapper is being used : (The commented out code works) 在这里使用包装器:(注释掉的代码有效)

 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);

I FIXED IT!!! 我修好了它!!!

I realized that calling Graphics gc = bufferedImage.getGraphics() is returning a different graphics object each time!. 我意识到每次调用Graphics gc = bufferedImage.getGraphics()都会返回一个不同的图形对象!

Thus, I set the graphics when I set the bufferedImage and then use that instance!!! 因此,我在设置bufferedImage时设置了图形,然后使用该实例!!!

I'm not 100% sure of your question, but gc is a Graphics object that is obtained from the BufferedImage. 我不太确定您的问题,但gc是从BufferedImage获取的Graphics对象。 If you call gc.setColor(...) it will by itself do nothing to the BufferedImage, but if you then use the gc object to say draw a line, the line will be drawn on the BufferedImage, and in the color that gc was set to. 如果调用gc.setColor(...)它本身将无助于数据的BufferedImage,但如果你再使用GC对象说画一条线,该线将 BufferedImage的绘制,并在颜色GC被设置为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM