Select a Chapter: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Back to the Main Page | ||
Chapter Eight listings: 8 classes |
import javax.swing.JApplet; import java.awt.*; // import Graphics, Graphics2D, and Color public class Dancer extends JApplet { /** Illustration: Draw things on the applet's drawing area. */ public void paint (Graphics g) { Graphics2D page = (Graphics2D) g; // 1 page.setColor (Color.blue); // 2 page.drawString ("stars goes here", 10, 45); // 3 page.setColor (Color.red); // 4 page.drawString ("stripes go here", 10, 90); // 5 page.drawLine (10, 60, 30, 70); // 6 } //====================== }
import javax.swing.JApplet; import java.awt.*; // import Graphics, Graphics2D, and Rectangle public class Boxes extends JApplet { /** Illustration: Draw 3 boxes on the applet's drawing area. */ public void paint (Graphics g) { Graphics2D page = (Graphics2D) g; Rectangle box = new Rectangle (50, 40); box.setLocation (30, 60); page.draw (box); // the inner box in the figure box.grow (5, 5); page.draw (box); // the outer box in the figure box.translate (70, 0); page.fill (box); // the filled box on the right } //======================= }
import javax.swing.JApplet; import java.awt.*; public class Dancer extends JApplet { private Flag itsFlag = new Flag(); public void start() { // leave empty for now; animation comes later } //======================= public void paint (Graphics g) { itsFlag.draw ((Graphics2D) g, 10, 20); } //======================= }
import java.awt.Graphics2D; import java.awt.Color; import java.awt.Rectangle; public class Flag { public static final int FLAG_WIDE = 173; public static final int FLAG_TALL = 91; public static final int UNION_WIDE = 69; public static final int UNION_TALL = 49; public static final int PIP = 2; public static final int STAR = 5; //////////////////////////////////// private Rectangle itsUnion = new Rectangle (UNION_WIDE, UNION_TALL); // width & height private Rectangle itsStripe = new Rectangle (FLAG_WIDE, FLAG_TALL / 13); /** Draw one American flag. */ public void draw (Graphics2D page, int x, int y) { drawStripes (page, x, y); page.setColor (Color.blue); itsUnion.setLocation (x, y); page.fill (itsUnion); drawAllStars (page, x, y); } //====================== /** Draw 13 stripes in 173x91 area, top-left at (x,y). */ private void drawStripes (Graphics2D page, int x, int y) { itsStripe.setLocation (x, y); for (int k = 0; k < 13; k++) { if (k % 2 == 0) page.setColor (Color.red); else page.setColor (Color.white); page.fill (itsStripe); itsStripe.translate (0, TALL); } } //====================== /** Draw the 50 stars on the flag whose union is at (x,y). */ private void drawAllStars (Graphics2D page, int x, int y) { page.setColor (Color.white); for (int row = 0; row < 9; row++) { int end = row % 2 == 0 ? x + PIP : x + PIP + 6; drawRowOfStars (page, x + UNION_WIDE - STAR, end, y + PIP + row * STAR); } } //====================== /** Draw one row of 5 or 6 stars on the union. */ private void drawRowOfStars (Graphics2D page, int end, int x, int y) { for (; x < end; x += 12) drawOneStar (page, x, y); } //====================== /** Draw 1 star with bounding box at position (x,y). */ private void drawOneStar (Graphics2D page, int x, int y) { int twoPips = PIP * 2; page.drawLine (x + PIP, y, x, y + twoPips); page.drawLine (x, y + twoPips, x + twoPips, y + PIP); page.drawLine (x + twoPips, y + PIP, x, y + PIP); page.drawLine (x, y + PIP, x + twoPips, y + twoPips); page.drawLine (x + twoPips, y + twoPips, x + PIP, y); } //====================== }
import javax.swing.JApplet; import java.awt.*; public class Dancer extends JApplet { private Flag itsFlag = new Flag(); private int itsLeftEdge; private int itsTopEdge; public void start() { setVisible (true); for (int count = 0; count < 2; count++) { danceRight(); danceLeft(); } } //====================== private void danceRight() { itsTopEdge = 20; for (itsLeftEdge = 10; itsLeftEdge <= 49; itsLeftEdge++) { repaint(); pause (50); itsTopEdge += (itsLeftEdge / 10 % 2 == 1) ? 4 : -4; } } //====================== private void danceLeft() { itsTopEdge = 20; for (itsLeftEdge = 49; itsLeftEdge >= 10; itsLeftEdge--) { repaint(); pause (50); itsTopEdge += (itsLeftEdge / 10 % 2 == 1) ? -4 : 4; } } //====================== private static void pause (int wait) { long timeToQuit = System.currentTimeMillis() + wait; while (System.currentTimeMillis() < timeToQuit) { } // take no action } //====================== public void paint (Graphics g) { Graphics2D page = (Graphics2D) g; page.setColor (Color.white); page.fillRect (0, 0, 320, 160); // clear the area itsFlag.draw (page, itsLeftEdge, itsTopEdge); } //====================== }
import java.awt.*; public class Collision extends javax.swing.JApplet { public static final int NUM_CYCLES = 4000; public static final int SIZE = 21; // number of Balls ////////////////////////////////// private Ball[] itsItem = new Ball [SIZE]; // the 21 Balls /** Draw the display of 21 balls several thousand times. */ public void start() { this.setVisible (true); Ball.initialize (this.getWidth(), this.getHeight()); for (int k = 0; k < SIZE; k++) itsItem[k] = new Ball(); for (int cycle = 1; cycle <= NUM_CYCLES; cycle++) { updateEachBall(); repaint(); long timeToQuit = System.currentTimeMillis() + 90; while (System.currentTimeMillis() < timeToQuit) { } // take no action } } //====================== /** Check each ball and make the appropriate changes. */ private void updateEachBall() { for (int k = 0; k < SIZE; k++) { if (itsItem[k].isHealthy()) moveAndCheckHits (itsItem[k]); else if (itsItem[k].isDying()) itsItem[k].deteriorates(); else itsItem[k] = new Ball(); } } //====================== /** Poison any 1 ball that guy comes very close to. */ private void moveAndCheckHits (Ball guy) { guy.move(); int c = 0; while (c < SIZE && itsItem[c].misses (guy)) c++; if (c < SIZE && itsItem[c].isHealthy()) itsItem[c].getsPoison(); } //====================== /** Draw the current status of the 21 balls. */ public void paint (Graphics g) { Graphics2D page = (Graphics2D) g; page.setColor (Color.white); page.fill (new Rectangle (getWidth(), getHeight())); for (int k = 0; k < SIZE; k++) itsItem[k].drawImage (page); } //====================== }
import java.util.Random; import java.awt.geom.Ellipse2D; import java.awt.*; public class Ball { public static final int UNIT = 15; // width of figure private static final int HEALTHY = 100; // cycles to languish private static Random randy = new Random(); private static int theRight; // right side of the screen private static int theBottom; // bottom edge of the screen ////////////////////////////////// private int itsXspot; // x-value of current location private int itsYspot; // y-value of current location private int itsXmove; // change in x each cycle private int itsYmove; // change in y each cycle private int itsStatus; // tells how healthy it is public static void initialize (int rightSide, int bottom) { theRight = rightSide; theBottom = bottom; } //====================== public Ball() { itsXspot = (theRight * (1 + randy.nextInt (9))) / 10; itsYspot = theBottom - randy.nextInt (40); itsXmove = randy.nextInt (5) - 2; // range -2 to 2 itsYmove = randy.nextInt (3) - 3; // range -1 to -3 itsStatus = HEALTHY; } //====================== public boolean isHealthy() { return itsStatus == HEALTHY; } //====================== public void getsPoison() { itsStatus = HEALTHY - 1; } //====================== public boolean isDying() { return itsStatus < HEALTHY && itsStatus > 0; } //====================== public void deteriorates() { itsStatus--; } //====================== // the Ball class, completed public void move() { itsXspot += itsXmove; itsYspot += itsYmove; if (itsXspot <= 1 || itsXspot >= theRight - UNIT) itsXmove = - itsXmove; if (itsYspot <= 1 || itsYspot >= theBottom - UNIT) itsYmove = - itsYmove; } //====================== public boolean misses (Ball guy) { return this == guy || Math.abs (itsXspot - guy.itsXspot) + Math.abs (itsYspot - guy.itsYspot) > 2; } //====================== public void drawImage (Graphics2D page) { if (isHealthy()) page.setColor (Color.red); else page.setColor (Color.blue); page.fillOval (itsXspot, itsYspot, UNIT, UNIT); } //====================== }
import java.awt.Color; public class Turtlet { public static final double DEGREE = Math.PI / 180; public static final Color RED = Color.red, BLUE = Color.blue, BLACK = Color.black, GRAY = Color.gray, YELLOW = Color.yellow, PINK = Color.pink, ORANGE = Color.orange, GREEN = Color.green, MAGENTA = Color.magenta, WHITE = Color.white; private static java.awt.Graphics thePage; ////////////////////////////////// private double heading = 0; // heading initially east private double xcor, ycor; // current position of Turtle /** Set the drawing Color to the given value. Made an instance method * only so that it cannot be called until thePage is assigned, although * the drawing color is a property of thePage, not of one Turtle. */ public void switchTo (Color given) { thePage.setColor (given); } //====================== /** Write words without changing the Turtle's state. */ public void say (String message) { thePage.drawString (message, (int) xcor, (int) ycor); } //====================== /** Pause the animation for wait milliseconds. Made a class method * so that an applet can pause an animation "between turtles". */ public static void sleep (int wait) { try { Thread.sleep (wait); }catch (InterruptedException ex) {} } //====================== /** Supply the nearest int value to methods requiring ints. */ private int round (double x) { return (int) (x + 0.5); } //====================== /** Make a circle of the given radius, Turtle at center. */ public void swingAround (double radius) { if (radius > 0.0) { int rad = round (2 * radius); thePage.drawOval (round (xcor - radius), round (ycor - radius), rad, rad); } } //====================== // the Turtle class, completed /** Rotate left by left degrees; MOVE for forward steps. */ public Turtle move (double left, double forward) { heading += left * DEGREE; xcor += forward * Math.cos (heading); ycor -= forward * Math.sin (heading); return this; } //====================== /** Rotate left by left degrees; PAINT for forward steps. */ public Turtle paint (double left, double forward) { heading += left * DEGREE; double x = xcor + forward * Math.cos (heading); double y = ycor - forward * Math.sin (heading); page.draw (new Line2D.Double (xcor, ycor, x, y)); xcor = x; ycor = y; return this; } //====================== /** Fill a circle of the given radius, Turtle at center. */ public void fillCircle (double radius) { page.fill (new Ellipse2D.Double (xcor - radius, ycor - radius, 2 * radius, 2 * radius)); } //====================== /** Fill a rectangle of the given width and height, * Turtle at center. */ public void fillBox (double width, double height) { page.fill (new Rectangle2D.Double (xcor - width / 2, ycor - height / 2, width, height)); } //====================== /** Create a turtle on a given page at (xstart, ystart). */ public Turtle (Graphics g, double xstart, double ystart) { page = (Graphics2D) g; xcor = xstart; ycor = ystart; } //====================== public Turtle() { if (page == null) { JFrame area = new JFrame ("Turtle drawings"); area.addWindowListener (new Closer()); // in Chapter 9 area.setSize (WIDTH, HEIGHT); area.setVisible (true); page = (Graphics2D) area.getGraphics(); area.paint (page); page.setColor (BLACK); } } //====================== } |
Layout and Design Copyright © Psumonix, LLC. All Rights Reserved. |