public static void changeTurtle(Turtle turtle) { Random generator = new Random(); int width = generator.nextInt(100); int height = generator.nextInt(100); turtle.setWidth(width); turtle.setHeight(height); int colorVar = generator.nextInt(7); Color penColor; if (colorVar == 0) { penColor = Color.RED; } else if (colorVar == 1) { penColor = Color.ORANGE; } else if (colorVar == 2) { penColor = Color.YELLOW; } else if (colorVar == 3) { penColor = Color.GREEN; } else if (colorVar == 4) { penColor = Color.BLUE; } else if (colorVar == 5) { penColor = Color.MAGENTA; } else { penColor = Color.CYAN; } turtle.setPenColor(penColor); }
public static void reposition(Turtle turtle) { Random generator = new Random(); int posx = generator.nextInt(1000) + 200; int posy = generator.nextInt(500) + 200; turtle.penUp(); turtle.moveTo(posx, posy); turtle.penDown(); }
public static void main(String[] args) { // Part 1 Turtle t1, t2, t3; t1 = new Turtle(); t2 = new Turtle("Larry"); t3 = new Turtle("kevin", 100, 500); System.out.println("Turtle 1 is called" + " " + t1.getname()); System.out.println("Turtle 1 is" + " " + t1.getage() + " " + "years old"); System.out.println("Turtle 1 is going at" + " " + t1.getspeed() + " " + "mph"); System.out.println("Turtle 2 is called" + " " + t2.getname()); System.out.println("Turtle 2 is" + " " + t2.getage() + " " + "years old"); System.out.println("Turtle 2 is going at" + " " + t2.getspeed() + " " + "mph"); System.out.println("--------------------------"); // Part 2 Person p1; p1 = new Person("Vincent"); p1.setTurtle(t1); System.out.println(p1.getPersonName() + "'s turtle's speed is" + " " + p1.getTurtleSpeed()); System.out.println(p1.getPersonName() + "'s turtle's name is" + " " + p1.getPet()); }
/** Construcs a new scoreboard */ public Scoreboard() { // set size calling the constructor of superclass super(60, 35); setBorderWidth(0); setTransparentBackground(true); painter = new Turtle(); painter.setVisible(false); painter.setDirection(90); painter.setPenColor(Color.white); painter.setFont(new Font("Lucida Sans", Font.BOLD, 20)); add(painter); painter.center(); resetScore(); }
private void paintTurtle(Graphics2D g) { if (turtle.isHidden()) { return; } Image image = getImage(); int xCenter = image.getWidth(null) / 2; int yCenter = image.getHeight(null) / 2; int x = turtle.getX() - xCenter; int y = turtle.getY() - yCenter; AffineTransform rotate = AffineTransform.getRotateInstance( Math.toRadians(turtle.getHeadingInDegrees()), xCenter, yCenter); AffineTransform move = AffineTransform.getTranslateInstance(x, y); move.concatenate(rotate); g.drawImage(image, move, null); }
public static void main(String[] args) { World newWorld = new World(1400, 900); Picture img = new Picture("background.jpg"); newWorld.setPicture(img); Turtle turtle1 = new Turtle(500, 500, newWorld); Turtle turtle2 = new Turtle(300, 300, newWorld); turtle1.setPenWidth(20); turtle2.setPenWidth(20); drawStar(turtle1); drawStar(turtle2); reposition(turtle1); reposition(turtle2); drawStar(turtle1); drawStar(turtle2); }
public static void main(String args[]) { World turtleWorld = new World(); Turtle turtle = new Turtle(turtleWorld); turtle.setPenColor(Color.GREEN); turtle.speed(0); turtle.setPenWidth(25); turtle.penDown(); for (int i = 0; i < 4; i++) { turtle.forward(50); turtle.turnRight(); } }
private void paintLines(Graphics2D g) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); for (LineSegment l : turtle.getTrail()) { if (l != null) { g.setColor(l.getColor()); g.setStroke(new BasicStroke(l.getWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); g.draw(new Line2D.Double(l.getStart().x, l.getStart().y, l.getEnd().x, l.getEnd().y)); } } }
/** * Draws a fractal line of input length. * * @param length Desired length of the line. */ @Override public void move(double length) { if (length < MIN_MOVE_LENGTH) { super.move(length); } else { move(length / 3); turn(-60); move(length / 3); turn(120); move(length / 3); turn(-60); move(length / 3); } }
public static void main(String[] args) { FileChooser.setMediaPath("mediasources/"); World world = new World(1000, 1000); Picture back = new Picture(1000, 1000); back.setAllPixelsToAColor(Color.GREEN); world.setPicture(back); Turtle turtle = new Turtle(world); turtle.setColor(new Color(255, 0, 255)); turtle.setPenWidth(2); Picture p = new Picture(FileChooser.getMediaPath("chisum.jpg")); p = p.scale(.5, .5); // turtle.penUp(); for (int i = 0; i < 360; i = i + 45) { turtle.forward(200); turtle.drop(p); turtle.backward(200); turtle.turn(45); } }
public void hit(Enemy enemy) { if (enemy instanceof Turtle && ((Turtle) enemy).currentState == Turtle.State.STANDING_SHELL) ((Turtle) enemy) .kick( enemy.b2body.getPosition().x > b2body.getPosition().x ? Turtle.KICK_RIGHT : Turtle.KICK_LEFT); else { if (marioIsBig) { marioIsBig = false; timeToRedefineMario = true; setBounds(getX(), getY(), getWidth(), getHeight() / 2); MarioBros.manager.get("audio/sounds/powerdown.wav", Sound.class).play(); } else { MarioBros.manager.get("audio/music/mario_music.ogg", Music.class).stop(); MarioBros.manager.get("audio/sounds/mariodie.wav", Sound.class).play(); marioIsDead = true; Filter filter = new Filter(); filter.maskBits = MarioBros.NOTHING_BIT; for (Fixture fixture : b2body.getFixtureList()) fixture.setFilterData(filter); b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true); } } }
public static void main(String asdf[]) { World w; // ticket to hold where our World is in memory Turtle mt; // to hold where my Turtle is w = new World(); // go to the World factory and, please, make me a World mt = new Turtle(w); // make a new Turtle, like mamma turtle does in her belly mt.setPenWidth(6); mt.turn(180.0); // tells Turtle to turn all the way around, same effect as turning right twice. mt.forward(); mt.turnLeft(); // class voted to make an L by turning left mt.forward(); // we now have two lines of the L mt.turnLeft(); mt.forward(30); // the toe was too long, so we tried a shorter length // forward() and forward(int pixels) are two "polymorphic" // methods: Different methods with the SAME name but // different because of signature. mt.turnLeft(); mt.forward(30 / 3); // add a hook to the toe. The hook was too long, // so we program dividing the original length by 3 mt.hide(); // makes the Turtle's body become hidden, so the letter looks nicer. // DONE! RELEASE IT! 1/31/2012 }
// write method to return turtle's speed public int getTurtleSpeed() { return turtle.getSpeed(); }
public static void main(String args[]) { World turtleWorld = new World(); Turtle turtle = new Turtle(turtleWorld); turtle.setPenColor(Color.RED); turtle.penDown(); int x = 1; while (true) { turtle.forward(x); x += 1; turtle.turn(90); turtle.forward(x); x += 1; turtle.turn(90); turtle.forward(x); x += 1; turtle.turn(90); turtle.forward(x); x += 1; turtle.turn(90); } }
/* ****************** CHECKLIST ****************************** [x] the size of the scribble MUST be controlled linearly by the value of scale [x] use tu.moveTo() */ boolean scrible(int xPos, int yPos, double scale) { Turtle tu = new Turtle(this); tu.penUp(); tu.moveTo(xPos, yPos); tu.penDown(); int size = (int) scale; tu.penUp(); tu.forward(size / 2); tu.penDown(); tu.turn(-90); for (int i = 0; i < 28; i++) { size = size - 10; tu.forward(size); tu.turn(-90); tu.forward(size); tu.turn(-90); } return true; }
/** * Method displayMatch that take a search image a template image and a point where the template * matches the search image and writes and displays the search image with a box drawn the size of * the template image at the point given. * * @param: search: The search image of type Picture * @param: template: The template image of type Picture * @param: minMatch: The point where the template appears in the search image, of type Point. */ public static void displayMatch(Picture search, Picture template, Point minMatch) { Turtle turtle = new Turtle((int) minMatch.getX(), (int) minMatch.getY(), search); turtle.setPenWidth(3); turtle.turnRight(); turtle.forward(template.getWidth()); turtle.turnRight(); turtle.forward(template.getHeight()); turtle.turnRight(); turtle.forward(template.getWidth()); turtle.turnRight(); turtle.forward(template.getHeight()); turtle.turnRight(); search.show(); search.write("Waldoboxed.png"); }
public static void drawStar(Turtle turtle) { changeTurtle(turtle); turtle.turn(30); turtle.forward(200); changeTurtle(turtle); turtle.turn(135); turtle.forward(200); changeTurtle(turtle); turtle.turn(150); turtle.forward(200); changeTurtle(turtle); turtle.turn(145); turtle.forward(150); changeTurtle(turtle); turtle.turn(140); turtle.forward(180); turtle.turn(120); }
public static void main(String[] args) throws InterruptedException { Turtle t = new Turtle(); t.hide(); t.speed(0); double x = 0; double y = 0; t.onKey("clearScreen", "c"); System.out.println("-----2.5: THREE SHAPES-----"); System.out.println("\nLeft-click the canvas to draw a square."); System.out.println("Middle-click the canvas to draw a circle."); System.out.println("Right-click the canvas to draw a triangle."); System.out.println("Press 'c' to clear the canvas."); while (true) { if (t.mouseButton1()) { x = t.mouseX(); y = t.mouseY(); x = t.canvasX(x); y = t.canvasY(y); t.penColor("pink"); t.setDirection(0.0); t.up(); t.setPosition(x, y); t.down(); for (int i = 0; i < 4; i++) { t.forward(50.0); t.right(90.0); } Thread.sleep(100); } else if (t.mouseButton2()) { x = t.mouseX(); y = t.mouseY(); x = t.canvasX(x); y = t.canvasY(y); t.penColor("olive"); t.setDirection(0.0); t.up(); t.setPosition(x, y); t.down(); for (int i = 0; i < 360; i++) { t.forward(0.5); t.right(1.0); } Thread.sleep(100); } else if (t.mouseButton3()) { x = t.mouseX(); y = t.mouseY(); x = t.canvasX(x); y = t.canvasY(y); t.penColor("light blue"); t.setDirection(0.0); t.up(); t.setPosition(x, y); t.down(); for (int i = 0; i < 3; i++) { t.forward(50.0); t.right(120.0); } Thread.sleep(100); } } }
public void run() { // initialize variables double angRad = 0; int x = 0; int y = 0; mars.setPicture(new Picture("Proj01.jpg")); mars.getPicture().addMessage("Charles Combs", 10, 20); // this code does the turtle dancing seen in the penguin example joe.setName("joe"); joe.setBodyColor(Color.YELLOW); joe.setShellColor(Color.RED); joe.setPenColor(Color.YELLOW); joe.setPenWidth(3); joe.forward(); joe.turn(+90); joe.setPenColor(Color.RED); joe.setPenWidth(1); // at the top of the 100 pixel run, now red and one wide, facing east for (int ang = 180; ang < 274; ang += 1) { angRad = Math.toRadians(ang); x = (96) + (int) (Math.sin(angRad) * (96)); y = (100) + (int) (Math.cos(angRad) * (100)); joe.moveTo(x + 99, y + 46); } // end for loop joe.setPenColor(Color.GREEN); joe.setPenWidth(3); for (int ang = 274; ang < 361; ang += 1) { angRad = Math.toRadians(ang); x = (100) + (int) (Math.sin(angRad) * (100)); y = (100) + (int) (Math.cos(angRad) * (100)); joe.moveTo(x + 99, y + 38); } // end for loop joe.moveTo(197, 237); sue.setName("sue"); sue.setPenWidth(2); sue.setPenColor(Color.BLUE); sue.moveTo(237, 187); sue.setPenDown(false); sue.moveTo(277, 227); sue.setPenDown(true); sue.moveTo(317, 267); } // end run method
/** Repaints the displayed score. */ private void repaintScore() { clear(); painter.printCenter(Integer.toString(score)); }
public static void clearScreen(Turtle t) { t.clear(); }
// 起動処理 public static void main(String[] args) { Turtle.startTurtle(new DoubleHouse()); }