Java Exercises
Graphics
Methods
|
This
exercise should be carried out after reading the Graphics
Methods Tutorial
You can choose to do
one or both of the exercises listed below.
Exercise
1: House Applet
Exercise
2: Face Applet
Exercise
1: House Applet
The
tutorial walked you though the creation of a house using some graphics
methods. In one exercise, a moon shape was drawn above the house and in
another exercise a sun shape was drawn.
How
about creating an Applet that displays the house with a moon which changes
to house with a sun when the user clicks anywhere on the applet window, and
vice-versa.
You
could also add the text "House at night - click to see House in the
day" and "House in the day - click to see House at night"
as appropriate. You could also change the color of the house depending
on whether it is day or night.
Here
is my House applet.
I
would suggest you first create two different classes; HouseAtNight and
HouseInDay and play with the fill colors until you are
satisfied. Make sure you keep the positioning of the two houses the
same
Once
you are completely happy with both your applets, change the code as follows:-
HouseAtNight:
Change
the line
public class HouseAtNight
extends applet{
to
public class HouseAtNight {
This
makes a stand-alone class instead of a class that extends and inherits from
applet.
Also
remove the line:- import java.applet.*;
HouseInDay:
Change
the line
public class
HouseInDay extends applet {
to
public class
HouseInDay {
This
makes a stand-alone class instead of a class that extends and inherits from
applet.
Also
remove the line:- import java.applet.*;
HouseApplet:
Now
create a new class called HouseApplet as shown below:-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class HouseApplet extends Applet implements MouseListener {
boolean isDay = true; //boolean to know which
house is currently being displayed
HouseInDay dayHouse = new
HouseInDay(); // create an instance of HouseInDay
HouseAtNight nightHouse = new HouseAtNight();
// create an instance of HouseAtNight
public void init() {
addMouseListener(this); //
add a mouse listener so we can catch mouse click events
}
public void paint (Graphics g) {
if (isDay == true) { //
if house displayed is day house
dayHouse.paint(g); // paint the day house
}
else {
nightHouse.paint(g); // paint the night house
}
}
public void mouseClicked(MouseEvent e) { //method
that runs when user clicks with mouse
if (isDay == true) {
//if boolean isDay is true, day house is diplayed
isDay = false;
//so set boolean isDay to false
repaint();
//repaint so paint method runs and night house is displayed
}
else {
isDay = true; //set
boolean isDay to true
repaint(); //repaint
so paint method runs and day house is displayed
}
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}// end class
|
Compile
this class and create a HTML file for it. Now run the applet in the browser
or using appletviewer and see what you get
Exercise
2: Face Applet
Create
an applet that displays a face using Graphics methods. Let your
imagination run riot. I am not going to give you any help on this, it
can be any sort of face you choose.
Have
fun!
|