1

I have a certain code in Java that I am trying to get over to C#. My problem so far is with the 'Method paint(Graphics g)'. Rather new to programming and still learning, I kinda don't have a clue how to properly "translate" this Method into C#. My code right now looks like this.

Also, here the Java Method without the rest of the class:

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);
    }
durron597
  • 31,968
  • 17
  • 99
  • 158
Skyswimsky
  • 71
  • 2
  • 9

1 Answers1

1

Hook into Paint -event in your constructor:

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

Then implement "MyPaintProcedure" like this:

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

The rest should be pretty much trivial.

Simo Erkinheimo
  • 1,347
  • 9
  • 17
  • Thank you, that already helped me a lot. However, if you could help me out how the correct Syntax for this piece of code in C# is I think I will be able to figure the rest out by myself: `private Image offscreenBuffer; private Graphics offscreenGraphics; ... if (offscreenBuffer == null){ offscreenBuffer = createImage(this.getWidth(), this.getHeight()); offscreenGraphics = offscreenBuffer.getGraphics(); }` – Skyswimsky Jul 09 '14 at 09:12
  • And, while it has nothing to do with my question, are there no line-breaks avaible in comments? – Skyswimsky Jul 09 '14 at 09:15
  • `offScreenBuffer = createImage(this.Width, this.Height);` `offscreenGraphics = Graphics.FromImage(offScreenBuffer);` And no, there are no line brakes allowed in comments here. And, if your question is aswered, please mark it accordingly. – Simo Erkinheimo Jul 09 '14 at 09:43
  • I would like to use the createImage Method, but C# doesn't seem to have it. – Skyswimsky Jul 09 '14 at 09:58
  • Oh, I thought it was your own method. I took another look at your code and it seems you want to manipulate the panel's graphics off-screen before displaying all of that at once (to avoid flickering?) You don't need to do that with .NET. Just draw your glyphs directly to `e.Graphics` and put `DoubleBuffered = true;` to your AsciiPanel's contructor. – Simo Erkinheimo Jul 09 '14 at 19:52
  • Oh, that is neat! Thanks – Skyswimsky Jul 10 '14 at 10:12
  • You're welcome. You should see a gray check-mark on the left side of my answer. Please check it if you feel like my answer answers your original question. – Simo Erkinheimo Jul 10 '14 at 11:17