Search code examples
javac#drawingcode-conversion

Specific Java code to C#


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

Solution

  • 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.