I was trying to do my computer science homework but I am stuck as I was trying to use the following methods.
-
public Graphics create(int x,int y,int width,int height)That Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.
Parameters:
- x – the x coordinate.
- y – the y coordinate.
- width – the width of the clipping rectangle.
- height – the height of the clipping rectangle.
-
public abstract void translate(int x,int y)That Translates the origin of the graphics context to the point (x, y) in the current coordinate system.
Can anyone explain and give examples of how to use them?
i was trying to do this..
public Graphics drawPlayer1()
{
myPencil.up();
myPencil.move(-620,300);
myPencil.down();
myPencil.fillCircle(20);
myPencil.up();
myPencil.move(-590,300);
myPencil.drawString("Player1: " + player1);
p1.create(-620,300,40,40);
return p1;
}//end drawPlayer1
and it threw me a nullPointerException when it comes to p1.create(-620,300,40,40);
I’m with Andrew on this one, I’ve never used
Graphics#create(int, int, int, int). I do useGraphics#createthough.Basically, the create method will create a new graphics context which is a copy of the original. This allows you to manipulate the copy with out effecting the original. This is important if you are performing operations on the graphics that can’t be (easily) undone.
Translate simple “zeros” the graphics context to the new location. The Swing painting process does this for each component it paints. Basically, before
paintis called, the graphics context is translated to the components position, meaning that all painting within the component is done from 0x0