DOCX

advertisement
CSE 1102
Fall 2009
Chapter 2 Examples and Exercises
Example 1) The following code:
import wheels.users.*;
public class MoonCartoon extends Frame {
private Ellipse _moon;
private Rectangle _sky;
private ConversationBubble _bubble;
public MoonCartoon () {
_sky = new Rectangle(java.awt.Color.BLACK);
_sky.setLocation(0,0);
_sky.setSize(1000,1000);
_moon = new Ellipse(new java.awt.Color(200,200,200));
_moon.setLocation (300, 40);
_moon.setSize(75, 75);
}
public static void main ( String[] args ) {
MoonCartoon cartoon = new MoonCartoon();
}
}
Produces the following night sky scene:
Exercise 1) Modify the code above to add some stars to the night sky. Also, either add some craters to
the moon (ellipses of a different shade of gray) or change the phase of the moon to a crescent moon.
You may change the size of the moon if you like.
Example 2) The following code produces a graphical representation of the “Blue Plate Special.”
import wheels.users.*;
public class BluePlateSpecial extends Frame {
private Ellipse _plate;
private Ellipse _egg;
private Ellipse _yolk;
private Rectangle _bacon;
private Rectangle _baconSecondSlice;
public BluePlateSpecial() {
_plate = new Ellipse(java.awt.Color.blue);
_plate.setSize(400,400);
_plate.setLocation(100,20);
_egg = new Ellipse(java.awt.Color.black);
_egg.setLocation (300, 60);
_egg.setSize(80, 160);
_egg.setFillColor(java.awt.Color.white);
_yolk = new Ellipse(java.awt.Color.yellow);
_yolk.setSize(50,50);
_yolk.setLocation(315,80);
_bacon = new Rectangle(new java.awt.Color(160,82,45));
_bacon.setSize(200,25);
_bacon.setLocation(200, 300);
_baconSecondSlice= new Rectangle(new java.awt.Color(160,82,45));
_baconSecondSlice.setSize(200,25);
_baconSecondSlice.setLocation(200, 345);
}
public static void main ( String[] args ) {
BluePlateSpecial cartoon = new BluePlateSpecial();
}
}
Exercise 2) Add a second, larger, egg and two sausages (use a brown ellipse) to the plate.
Example 3) The following code produces a cartoon of a snowman:
Snowman.java:
import wheels.users.*;
public class Snowman {
private Ellipse _head, _body, _leftEye, _rightEye;
int _x, _y;
public Snowman() {
_body = new Ellipse(java.awt.Color.white);
_body.setSize(100, 100);
_head = new Ellipse(java.awt.Color.white);
_head.setSize(80, 80);
_leftEye = new Ellipse(java.awt.Color.black);
_leftEye.setSize(15, 15);
_rightEye = new Ellipse(java.awt.Color.black);
_rightEye.setSize(15, 15);
_x = 10;
_y = 240;
setLocation(_x, _y);
setOutline(java.awt.Color.black, 2);
}
public void setOutline (java.awt.Color color,
int thickness) {
_body.setFrameColor(color);
_body.setFrameThickness(thickness);
_head.setFrameColor(color);
_head.setFrameThickness(thickness);
}
public void setLocation(int x, int y) {
_body.setLocation(x, y+60);
_head.setLocation(x+10, y);
_leftEye.setLocation(x+25, y+25);
_rightEye.setLocation(x+65, y+25);
}
public int getX () {
return _x;
}
public int getY () {
return _y;
}
}
Hat.java:
import wheels.users.*;
public class Hat {
private Rectangle _hatBrim, _hatUpper;
public Hat(Snowman mySnowman) {
_hatBrim = new Rectangle(java.awt.Color.black);
_hatBrim.setSize(80, 20);
_hatUpper = new Rectangle(java.awt.Color.black);
_hatUpper.setSize(60, 60);
setLocation(mySnowman.getX() + 10, mySnowman.getY() - 60);
}
public void setLocation(int x, int y) {
_hatBrim.setLocation(x, y + 50);
_hatUpper.setLocation(x + 10, y);
}
public int setLocation (int x, int y, int z) {return 3;}
}
SnowCartoon.java:
/*
* Chapter 2: SnowCartoon.java
* Displays a yellow sun, a snowman with a hat, and a
* conversation bubble.
* This program contains three classes.
*/
import wheels.users.*;
public class SnowCartoon extends Frame {
private Snowman _snowman;
private Hat _hat;
private Ellipse _sun;
private ConversationBubble _bubble;
public SnowCartoon() {
_snowman = new Snowman();
_snowman.setLocation(10, 240);
_hat = new Hat(_snowman);
_sun = new Ellipse(java.awt.Color.yellow);
_sun.setLocation (300, 40);
_sun.setSize(60, 60);
_bubble = new
ConversationBubble("Welcome to my program!",
ConversationBubble.TAIL_DIR_LEFT);
_bubble.setLocation(110, 110);
}
public static void main ( String[] argv ) {
SnowCartoon cartoon = new SnowCartoon();
}
}
Produces the following output:
Exercise 3) Add a second snowman to the scene. Add a nose (orange ellipse) to the Snowman class.
Change the direction of the tail of the conversation bubble to point to this new snowman. Change the
text of the conversation bubble to read “Smells like carrots!” So you get something like:
Example 4)The following code:
import wheels.users.*;
public class AlienCartoon extends Frame {
private Ellipse _face;
private Ellipse _leftEye;
private Ellipse _rightEye;
public AlienCartoon () {
_face = new Ellipse(java.awt.Color.green);
_face.setLocation (250, 40);
_face.setSize(200, 300);
_leftEye = new Ellipse(java.awt.Color.black);
_leftEye.setLocation(300,100);
_leftEye.setSize(40,60);
_rightEye = new Ellipse(java.awt.Color.black);
_rightEye.setLocation(360,100);
_rightEye.setSize(40,60);
}
public static void main ( String[] args ) {
AlienCartoon cartoon = new AlienCartoon ();
}
}
Produces the following cartoon of an alien:
Exercise 4) Change the alien skin to be a light gray color. Add two small ovals for a nose and a long thin
oval for a mouth. See below for an example of what your alien might look like:
Download