Graphics commands

Writing on the graphics screen

    g.drawString("Hello World",60, 30);

To change fonts
        
this must be used before the drawString

    g.setFont(new Font("Ariel", Font.BOLD, 20));

 


Example:
    g.setColor(Color.green);
Slight changes:  
       
g.setColor(Color.red.darker());
   g.setColor(Color.magenta.brighter());

Rectangles

    g.drawRect(50, 30,150,60);

    g.fillRect(240, 40, 50, 100);

Square - if width and height are the same

    g.drawRect(150, 30, 80, 80);


    g.fillRect(200, 140, 50, 50);

 

Diagonal lines

    g.drawLine(30, 50, 180, 150);
    g.drawLine(250, 50, 25, 120);

Vertical line - keep x's the same
    g.drawLine ( 200, 30, 200, 170);

Horizontal Line - keep y's the same
    g.drawLine ( 40, 80, 260, 80);

Circles - if width and height are the same

    g.drawOval(150, 130, 60, 60);

    g.fillOval(240, 40, 20, 20);

Ellipses

    g.drawOval(200, 50,20, 60);

    g.fillOval(40, 140, 90, 20);

 

Rectangles with the rounded corners

g.drawRoundRect( 50, 60, 100, 80, 40, 40);

           or

g.fillRoundRect(50, 150, 40, 90, 30, 30);

 


 Arc    for location or length of degrees refer to the degree wheel below

g.drawArc( 50, 60, 40,40, 45, 180);

        or for a sector

g.fillArc(120, 60, 50, 50, 180,90);

 Polygons are many sided figures - good for triangles, non-rectangluar quadrilaterals, pentagons, etc.

Here is sample: - note we are moving around the figure

  Polygon trap = new Polygon();
    trap.addPoint(100,170);
    trap.addPoint(200,170);
    trap.addPoint(225,230);
    trap.addPoint(75,230);
  g.fillPolygon(trap);



Additional commands - these do NOT belong to the paint method - put in the init method:

  
    setBackground(Color.black);

    setSize(300,200);

 

Here are some HTML colors - CLICK

Examples of using these :

g.setColor(new Color(60, 90,160));

g.setColor(new Color(160, 90,30));