// SampleGraphics.java // To draw graphics, copy this file to a directory, // and place a copy of the file CoStarsComponent.class in that directory! import java.awt.*; import javax.swing.*; public class SampleGraphics extends CoStarsComponent { private static int eval(String name, int val) { System.out.println(name + " = " + val); return val; } int width0 = eval("initial getWidth ",getWidth()); int height0 = eval("initial getHeight",getHeight()); int width1 = eval("initial width ",width); int height1 = eval("initial height",height); String song1 = "http://kosbie.net/cmu/sampleSounds/brandenburg6.mid"; String song2 = "http://kosbie.net/cmu/sampleSounds/entertainer.mid"; String song3 = "http://kosbie.net/cmu/sampleSounds/route66.mid"; String song4 = "http://kosbie.net/cmu/sampleSounds/sabre.mid"; String sound1 = "http://kosbie.net/cmu/sampleSounds/elephant.wav"; String sound2 = "http://kosbie.net/cmu/sampleSounds/horse.wav"; public void start() { System.out.println("Initial size = " + width + "x" + height); System.out.println(); System.out.println("In the graphics window:"); System.out.println(" click anywhere to move the purple dot"); System.out.println(" press arrows to move crosshairs"); System.out.println(" press p to pause"); System.out.println(" press x to exit"); System.out.println(" press 1, 2, 3, or 4 to play that song"); // System.out.print("\nEnter step delay (in milliseconds): "); // setStepDelay(scanner.nextInt()); setStepDelay(50); play(song1); } public void step() { if (isPaused) return; rectX += 20; ovalY += 20; if (steps % 100 == 0) play(sound2); } public void mouse() { dotX = mouseX; dotY = mouseY; play(sound1); } public void up() { pointY -= 5; } public void down() { pointY += 5; } public void left() { pointX -= 5; } public void right() { pointX += 5; } public void key() { if (key == 'p') isPaused = !isPaused; else if (key == 'x') exit(); else if (key == '1') { stopSounds(); play(song1); } else if (key == '2') { stopSounds(); play(song2); } else if (key == '3') { stopSounds(); play(song3); } else if (key == '4') { stopSounds(); play(song4); } else beep(); } private int rectX, ovalY, dotX, dotY, pointX = width-50, pointY = height-50; private boolean isPaused = false; public void paint() { // RECTS page.setColor(Color.blue); page.fillRect(rectX % width, 50, 50, 50); page.setColor(Color.red); page.fillRect(width-rectX % width,50,50,50); // OVALS page.setColor(Color.green); page.fillOval(50,ovalY % height,100,50); page.setColor(Color.orange); page.fillOval(50,height-ovalY % height,100,50); // CROSSHAIR page.setColor(Color.black); page.drawLine(pointX,0,pointX,height); page.drawLine(0,pointY,width,pointY); // DOT page.setColor(new Color(255,0,255,128)); page.fillOval(dotX-25,dotY-25,50,50); // ISAPPLET? page.setFont(new Font("Arial",Font.BOLD,16)); page.setColor(Color.orange); page.drawString("isApplet() == " + isApplet(),10,30); } public static void main(String[] args) { CoStarsComponent.launch(200,500); } }