Title: Drawing Basics
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Drawing Basics

1. The Component class has a method called paint that you can override. When you do, you are passed an instance of the Graphics class that provides many methods that facilitate low level drawing. We will look at these methods, but first let's cover a few more issues.
2. So where is the Component that we are drawing on? An Applet, or a Frame is derived from a Container class which is derived from Component. So if you have a class derived from Applet or Frame, then your class contains a Component class.
3. Overriding the paint method allows you to completely redraw your screen. Normally, paint is called everytime that the Java Runtime System needs to redraw the whole screen. We will look at this issue in more detail later. Also, normally the screen is completely erased just before paint is called.
4. When drawing on your screen, you need to be aware of the coordinate system. Drawing commands often require specification of some x, and y location on the screen. Let's suppose that the Applet or the Frame is given a region that is 500 pixels wide and 200 pixels high to work with. In this case:
  - x=0, y= 0 specifies the upper left corner of the drawable area
  - x=499, y = 0 specifies the upper right corner of the drawable area
  - x=0, y = 199 specifies the lower left corner of the drawable area
  - x=499, y=199 specifies the lower right corner of the drawable area
5. Most drawing happens by making calls to methods on the Graphics class. An instance of the Graphics class is passed into the paint method.
6. Java draws with a single color until a call to setColor is made to switch the color to another color. The set Color takes in an instance of the Color class. The Color class shows up in the java.awt package and has some interesting methods and constructors. It is worthwhile to take a look at the online help for the Color class.
  - One way to get a color class is to use some of the Statically defined constant colors such as:
Color.red, Color.yellow, Color.black, etc.
  - Another way is to think in terms of mixing colors with Red, Green, Blue (RGB mode). There is a constructor that allows you to mix colors by passing numbers that are between 0 and 255:
//mix in some whiteness
Color myLightRed = new Color(255, 150, 150);
  - Another Constructor that can sometimes be useful
Color myColor = new Color(float, float, float);
I will use this to create a random color:
Color myRandom = new Color((float)Math.random(),
(float)Math.random(), (float)Math.random());
  - One last useful constructor:
Color myColor = new Color(int);
This can be handy after converting a hex color input (FF8080) into an integer
7. The Pen color defaults to whatever the last call to setForeground.
8. Methods that start with draw create hollow objects, while methods that start with fill create solid objects. Also the first 4 parameters on these methods are often:
  - starting x
  - starting y
  - width
  - height

Some examples:
drawRectangle(10,12, 20, 50); // (x,y, w, h)
drawOval (10,12, 20, 50);
fillRect(10,12, 20, 50);
fillOval(10,12, 20, 50);
9. drawLine draws a line between points (x1,y1) and (x2,y2)
drawLine(x1,y1, x2, y2);
10. drawString draws a Text string that starts at location x, y:
drawString("Some Name", 10, 150);

Simple.java


import java.awt.*;
import javax.swing.*;

public class Simple extends JFrame {

	Simple(String name)
	{
        super(name);
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

	}
    public void paint(Graphics g) {
        //super.paint(g);
        g.setColor(Color.white);
        g.fillRect(0,0, 600, 400);
        g.setColor(Color.black);



        g.drawRect(10,10, 400, 150);

        g.setColor(Color.red);
        g.fillOval(110,35, 200, 100);

        g.setColor(Color.green);
        g.fillRoundRect(160, 60,  100, 50, 10, 10);
        g.drawLine(10, 180, 400, 180);

        g.setColor(Color.blue);
        g.drawString("Clem's Artwork", 10, 175);
    }
    public static void main(String[] args)
    {
    	Simple simple = new Simple("Simple Drawing");
    }


Last Updated: November 11, 2013 10:55 PM