Drawing

Dr Andy Evans

[Fullscreen]

Drawing on components

  • All containers have a 'graphics context' which is encapsulated in a 'Graphics' class object.
  • We can get the Graphics object and use its methods to draw...
    	
    Frame f = new Frame();
    Graphics g = f.getGraphics();
    g.drawline(0,0,300,200); 	// x1, y1, x2, y2
    g.dispose();
    
    
  • or, if we're in the frame class...
    	
    Graphics g = getGraphics();
    g.drawline(0,0,300,200);
    g.dispose();
    
    
  • All graphics are relative to the upper left corner (0, 0).
  • If drawings stray outside the Component's size, they're clipped automatically (i.e. they're stopped at the edges).

Displaying an Image

  • We can then use a Graphics object's drawImage method to draw the image to the screen.
    	
    drawImage(Image im, int left, 
     int top, ImageObserver imob);
    
    
    	
    g = frame.getGraphics();
    g.drawImage(image,0,0,panel); 
    
    
  • All components implement ImageObserver. This interface has one method imageUpdate, which returns whether an image has finished loading.

Vector drawing methods

	
drawLine(int startx, int starty, int endX, int endY)
drawOval(int topLeftX, int topLeftY, width, height)
drawRect(int topLeftX,topLeftY,lowRightX,lowRightY)
drawPolygon(int[] xs, int[] ys, int numberOfPoints)

  • drawPolygon closes polygons automatically.
  • To write text:
    	
    drawString(String str, int x, int y)
    
    
  • To set the font you need a java.awt.Font class object.
  • To get the line height etc. so you know where to start the next line you need a java.awt.FontMetrics class object.

Drawing on Frames

  • Note that setSize() sets the total size, so you lose some space under the menu, side borders etc.
  • You can find these "insets". The following wouldn't be unusual:
    	
    Insets insets = getInsets();
    setSize(300 + insets.left + insets.right, 
            300 + insets.top + insets.bottom);
    
    
    Or:
    	
    g.drawString("Top left text", getInsets().left, 
       getFontMetrics(getFont()).getHeight() 
       + getInsets().top);
    
    
  • getFont()).getHeight(): This gets the default line height -- drawString draws from y = bottom of the line.

Colour revisited

  • Note: the Graphics object has a set colour encapsulated in a java.awt.Color object - all lines are drawn in this.
  • Background set in Components not their Graphics objects:
    	
    setBackground(Color colorObject) 
    
    
  • Graphics setXORMode(Color) - swaps present colour for Color if the background drawn over is the present colour.

Repaint

  • The JVM calls components' repaint() method when it feels like it (when maximized, uncovered).
  • Repaint calls two methods:
    	
    update()            // paints the background.
    paint(Graphics g)   // paints the foreground.
    
    
  • If we don't overwrite paint, each repaint the component will lose any graphics we've added as it repaints the default look.
  • Even if you overwrite paint, call repaint() so update() runs.

Example: Frame
with constant text

Note that a Graphics object is passed in for you.
	
import java.awt.*;

public class TitlePanel extends Frame {

    public void paint (Graphics g) {
		g.drawString("Hello World", 100, 100);
    }

}