2014-07-03 163 views
1

我在Java中有一定的code,我试图找到C#。 '我的问题到目前为止是'方法油漆(图形g)'。相当新的编程和仍然学习,我有点不知道如何正确地“转换”此方法到C#。我的代码现在看起来像thisC#的特定Java代码

而且,这里的Java方法不休息之类的:

public void paint(Graphics g) { 
     if (g == null) 
      throw new NullPointerException(); 

     if (offscreenBuffer == null){ 
      offscreenBuffer = createImage(this.getWidth(), this.getHeight()); 
      offscreenGraphics = offscreenBuffer.getGraphics(); 
     } 

     for (int x = 0; x < widthInCharacters; x++) { 
      for (int y = 0; y < heightInCharacters; y++) { 
       if (oldBackgroundColors[x][y] == backgroundColors[x][y] 
       && oldForegroundColors[x][y] == foregroundColors[x][y] 
       && oldChars[x][y] == chars[x][y]) 
        continue; 

       Color bg = backgroundColors[x][y]; 
       Color fg = foregroundColors[x][y]; 

       LookupOp op = setColors(bg, fg); 
       BufferedImage img = op.filter(glyphs[chars[x][y]], null); 
       offscreenGraphics.drawImage(img, x * charWidth, y * charHeight, null); 

       oldBackgroundColors[x][y] = backgroundColors[x][y]; 
       oldForegroundColors[x][y] = foregroundColors[x][y]; 
       oldChars[x][y] = chars[x][y]; 
      } 
     } 

     g.drawImage(offscreenBuffer,0,0,this); 
    } 

回答

1

钩到画图-event在构造函数:

public AsciiPanel() 
{ 
    this(80, 24); // <-- Change also this to signature "public AsciiPanel() : base(80, 24) { ... }" 
    Paint += new PaintEventHandler(MyPaintProcedure); 
} 

然后实现 “MyPaintProcedure” 是这样的:

private void MyPaintProcedure(object sender, PaintEventArgs e) 
{ 
    System.Drawing.Graphics g = e.Graphics; 
} 

其余的应该是非常微不足道的。

+0

谢谢,这已经帮了我很多。然而,如果你能帮我弄清楚C#中这段代码的正确语法是什么,我想我可以通过自己来判断其余部分: 'private Image offscreenBuffer; private graphics offscreenGraphics; ... 如果(offscreenBuffer == NULL){ offscreenBuffer =的createImage(this.getWidth(),this.getHeight()); offscreenGraphics = offscreenBuffer.getGraphics(); }' – Skyswimsky

+0

而且,虽然它与我的问题无关,但评论中是否没有可用的换行符? – Skyswimsky

+0

'offScreenBuffer = createImage(this.Width,this.Height);''offscreenGraphics = Graphics.FromImage(offScreenBuffer);'不,在这里评论中没有允许线路刹车。而且,如果您的问题得到解答,请相应标记。 –