The following Java program draws a simple house using ...
The following Java program draws a simple house using basic shapes.
The shapes are drawn using paint(Graphics g ) which is one of the standard methods used in Java.
import java.awt.*;
import java.applet.*;
// Program to draw a house
public class House extends Applet
{
public void paint(Graphics g)
{
// Draw the roof
g.setColor(Color.red);
int xs[] = {100,160,220};
int ys[] = {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);
}
}
Explanatory Notes on the “House” class:
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 all classes in this course.
The java.awt package is used to create the graphics used in the applet windows.
Class
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 extends Applet
paint( )
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.
If you want to use the paint method you always begin with the line:
public void paint (Graphics g)
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
ie import java.awt.*
Now you 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)
To find out more about colours you can look up the Java help menu under
Colors (Note American spelling of Colours!)
Standard colors provided in java are:
|Colours |RGB Codes |Colours |RGB Codes |
|Color.black |0, 0, 0 |Color.gray |128, 128, 128 |
|Color.white |255, 255, 255 |Color.lightGray |192, 192, 192 |
|Color.red |255, 0, 0 |Color.darkGray |64, 64, 64 |
|Color.orange |255, 200, 0 |Color.cyan |0, 255, 255 |
|Color.yellow |255, 255, 0 |Color.blue |0, 0, 255 |
|Color.magenta |255, 0, 255 |Color.green |0, 255, 0 |
|Color.pink |255, 175, 175 | | |
To use these in paint( ) you use a statement such as:
g.setColor(Color.green);
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) would have coordinates as shown
(0,0) (200,0)
(0,100) (200,100)
Exercise
C Standard
1. Create a new eclipse project called ‘Prac_1’, and create a new class file inside this project called ‘House’.
2. Type in the code above and get the program working. (Remember to click ‘Run As Applet’)
3. Improve your house by adding 2 windows, making the door black and the roof magenta.
B Standard
Add at least 3 extra features to your house.
A Standard
Place the house in a scene. Eg. Sky & clouds, grass, trees, fence etc.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- words using the following letters
- a company had the following purchases a
- a company had the following purchases an
- 7 letters words using the following letters
- form a word using the following letters
- word using the following letters
- spell a word with the following letters
- simple java program using array
- coding a simple hello world java program
- simple java program for beginners
- convert the following fraction to a decimal
- which of the following is a nonmetal