Write down the syntax to create new Graphics object code example

Example 1: Write a simple java swing application that will display rectangle graphics as shown in the picture below:

// Graphics context's current color.
void setColor(Color c)
Color getColor()
 
// Graphics context's current font.
void setFont(Font f)
Font getFont()

// Set/Get the current clip area. Clip area shall be rectangular and no rendering is performed outside the clip area.
void setClip(int xTopLeft, int yTopLeft, int width, int height)
void setClip(Shape rect)
public abstract void clipRect(int x, int y, int width, int height) // intersects the current clip with the given rectangle
Rectangle getClipBounds()  // returns an Rectangle
Shape getClip()            // returns an object (typically Rectangle) implements Shape

Example 2: Write a simple java swing application that will display rectangle graphics as shown in the picture below:

public void paintComponent(Graphics g) {
   super.paintComponent(g);
   int width =  getWidth();   // Find out the width of this component.
   int height = getHeight();  // Find out its height.
   . . .   // Draw the content of the component.
}

Tags:

Java Example