Practical Exercise 1 House

Practical Exercise 1 House

Practical Exercise 1–House

This prac demonstrates how to create a simple drawing using basic shapes.

C Standard

Copy the following code into a new Java class and run it.

Improve your house by adding two windows, making the door black and the roof magenta.

import java.awt.*;

import java.applet.*;

// Program to draw a house

publicclass Prac1 extends Applet

{

publicvoid paint(Graphics g)

{

setSize(400,300);

// Draw the roof

g.setColor(Color.red);

intxs[] = {100,160,220};

intys[] = {100,50,100};

Polygon poly=new Polygon(xs,ys,3);

g.fillPolygon(poly);

// Draw the body of house

g.setColor(Color.blue);

g.fillRect(100,100,120,120);

// draw the door

g.setColor(Color.white);

g.fillRect(145,160,30,60);

// draw sun

g.setColor(Color.yellow);

g.fillOval(240,30,50,50);

//draw chimney

g.setColor(Color.black);

g.fillRect(120,55,10,30);

}

}

B Standard

Add at least three extra features to your house.

Place the house in a scene. E.g. sky and clouds, grass, trees, fence, etc.

A Standard

Find some way to go beyond the basics and show me something interesting. E.g. Research a programming technique that goes beyond the example code.

When you’re finished, take a screenshot of your masterpiece (e.g. using the Snipping Tool) and email it to Rob ().

Notes

Packages

The Java Applications Programming Interface (API) contains a large number of programming features grouped into “packages” of related features. When you write a program or class in Java you must import those packages which are used in the class.

  • The java.applet package is used in mostpracs in this course.
  • The java.awt package is used to create the graphics used in the applet windows.

Classes

We will have a lot more to say about classes in this course. For the time being, you can regard a class as a program, and every class that you write will begin with the line:

  • public class <class name> extends Applet

Methods

A method is a section of code that does a particular job in a class. Methods (in Object Oriented languages) are essentially the same as procedures and functions in other languages. They enable you to break up a long piece of code into smaller more manageable modules. These methods can be used many times in the original class and can be used in other classes.

One of the standard methods used in java applets is the paint( ) method. This is responsible for painting graphics and text into the applet window.

Drawing Shapes

Most graphics operations in Java are built into Java's Graphics class. This Graphics class is part of the java.awt package, so if your applet does any painting you will need to import that class at the beginning of your program, and then you will have access to a great variety of graphics operations. For example:

  • g.drawRect(x, y, length, width)draws an outline of a rectangle
  • g.fillRect(x, y, length, width)fills a rectangle in with colour
  • g.drawRoundRect(x, y, length, width, cwidth,cheight) - draws an outline of a rectangle with rounded corners
  • g.fillRoundRect(x, y, length, width, cwidth, cheight) - fills in a rectangle with rounded corners with colour
  • g.drawOval((x, y, length, width) - draws an outline of an oval or a circle
  • g.fillOval((x, y, length, width) - fills ovals or circles with colour
  • g.drawLine(x1,y1,x2,y2) - draws a line from (x1,y1) to (x2,y2)
  • setBackground(Color.white) - change background colour (default is grey)

Window Coordinates

When you draw a graphics object on the applet window, you must specify its coordinates.

The top left corner of an applet window has coordinates (0,0).

Position from the top left is measured in pixels, and a typical window might be 800 pixels wide by 600 pixels high, so that its bottom right has coordinates (800, 600).

For example the following rectangle (width = 200, length = 100) in the top-left corner would have coordinates as shown to the right.

Hobart College 2017, adapted from Rosny College 2009Page 1 of 2