java buffer problem?

Soldato
Joined
2 Nov 2002
Posts
3,304
Location
Aberdeen
well this is part of some uni work we have to do. I've followed the instructions to make the buffer work, but for some reason it's not being recognised. We have not been told to declare the buffer image, or anything, and i'm quite stuck here. Any help would be much appreciated.

Code:
    public void paintComponent(Graphics gPanel){
        if (this.buffer == null) {
            Dimension d = getSize();
            this.buffer = new BufferedImage(d.width, d.height,
                    BufferedImage.TYPE_INT_ARGB_PRE);
            
            this.g2Buffer = (Graphics2D) this.buffer.getGraphics();
            this.g2Buffer.setColor(Color.white);
            this.g2Buffer.fillRect(0, 0, d.width, d.height);
            // This time we'll draw with a broad pen
            this.g2Buffer.setStroke(new BasicStroke(8.0f));
        }
        Ellipse2D ellipse;
        if (drawing) {
            this.g2Buffer.setColor(Color.white);
            this.g2Buffer.setXORMode(Color.red);    // into the overwrite mode
            ellipse = new Ellipse2D.Float(xStart, yStart,
                    Math.abs(xOld - xStart), Math.abs(yOld - yStart));
            this.g2Buffer.draw(ellipse);
            ellipse = new Ellipse2D.Float(xStart, yStart,
                    Math.abs(xEnd - xStart), Math.abs(yEnd - yStart));
            this.g2Buffer.draw(ellipse);
            this.g2Buffer.setPaintMode();	 // out of XOR overwrite mode
            xOld = xEnd;			 // store last end point
            yOld = yEnd;
        }
        if (mouseReleased) {
            this.g2Buffer.setColor(Color.red);
            ellipse = new Ellipse2D.Float(xStart, yStart,
                    Math.abs(xEnd - xStart), Math.abs(yEnd - yStart));
            this.g2Buffer.draw(ellipse);
            mouseReleased = false;
        }
        gPanel.drawImage(this.buffer, 0, 0, this);
    }
    
    private int xStart = 0;
    private int yStart = 0;
    private int xEnd, yEnd;
    private int xOld = 0;
    private int yOld = 0;
    private boolean drawing = false;
    private boolean mouseReleased = false;

i'm sure it's a simple error, but i've tried searching but no results have been able to help me.

Any help would be much appreciated.

Lee.
 
well for everytime buffer is mentioned, the errors i get are along the lines of

'C:\..MyPanel.java:123: cannot find symbol
symbol : variable g2Buffer
location: class uk.ac.rgu.comp.cm3063.lab3.MyPanel
this.g2Buffer.draw(ellipse);
'

which to me means it's not be initiated somewhere, but i've tried various buffer = new buffer type declarations, but nothing seems to work.

i thought
Code:
            this.buffer = new BufferedImage(d.width, d.height,
                    BufferedImage.TYPE_INT_ARGB_PRE);

would have sorted this, but obviously not
 
Code:
this.g2Buffer = (Graphics2D) this.buffer.getGraphics();

already declared here.

Saying this, it looks like the uni notes i've been copying from are wrong, so i might need to re-write the program.

i get 'illegal start of type' and also '<identifier expected>'

when entering this code into my variable declarations.
 
Is the g2Buffer actually declared anywhere? The line you quoted last is an assignment, not a declaration.

There should be something in your MyPanel class declaring the buffer. For example:

Code:
public class MyPanel
{
   ...
   private Graphics2D g2Buffer;

   ...
}
It's difficult to tell you exactly what's wrong without seeing the whole file. If you're still stuck, feel free to send it to my email (in my trust) and I'll have a look to see if I can spot what's going on.
 
Back
Top Bottom