1. 根据现有的函数画 5 环.
import acm.graphics.*; import acm.program.*; import java.awt.*; public class OlympicRings extends GraphicsProgram { public void run() { draw3Circle(100, 100, circleR, Color.green); draw3Circle(100+distance, 100, circleR, Color.red); draw3Circle(100+distance+distance, 100, circleR, Color.blue); draw3Circle(newOrigalW, newOrigalH, circleR, Color.yellow); draw3Circle(newOrigalW+distance, newOrigalH, circleR, Color.black); } private int circleR = 100; private int circleSpace = 20; private int distance = circleR + circleSpace; private int newOrigalW = 100 + circleR/2 + circleSpace/2; private int newOrigalH = 100 + circleR/2 + circleSpace/2; private void draw3Circle(int originalW, int originalH, int radius, Color temp) { for (int i=0; i<5; i++) { GOval oo = new GOval(originalW+i, originalH+i, radius-(i*2), radius-(i*2)); oo.setColor(temp); add(oo); } } }
运行结果:
2. Rainbow
思路, 画大圆, 多层的, 西边部分用一个大的rect(白色的) 覆盖掉.
import acm.graphics.*; import acm.program.*; import java.awt.*; public class Rainbow extends GraphicsProgram { public void run() { drawCircles(originalW, originalH, circleR, Color.black); drawCircles(originalW+regularDistance*1, originalH+regularDistance*1, circleR-regularDistance*1*2, Color.yellow); drawCircles(originalW+regularDistance*2, originalH+regularDistance*2, circleR-regularDistance*2*2, Color.blue); drawCircles(originalW+regularDistance*3, originalH+regularDistance*3, circleR-regularDistance*3*2, Color.red); drawCircles(originalW+regularDistance*4, originalH+regularDistance*4, circleR-regularDistance*4*2, Color.orange); drawCircles(originalW+regularDistance*5, originalH+regularDistance*5, circleR-regularDistance*5*2, Color.green); drawCircles(originalW+regularDistance*6, originalH+regularDistance*6, circleR-regularDistance*6*2, Color.GRAY); GRect rc = new GRect(100, 200, rectW, rectH); rc.setFilled(true); rc.setColor(Color.white); rc.setFillColor(Color.white); add(rc); } private void drawCircles(int originalW, int originalH, int radius, Color temp) { for (int i=0; i<5; i++) { GOval oo = new GOval(originalW+i, originalH+i, radius-(i*2), radius-(i*2)); oo.setColor(temp); add(oo); } } private int originalW = 100; private int originalH = 100; private int circleR = 1000; private int rectW = 2000; private int rectH = 2000; private int regularDistance = 5; }
运行结果:
3. 金字塔
/* * File: Pyramid.java * Name: * Section Leader: * ------------------ * This file is the starter file for the Pyramid problem. * It includes definitions of the constants that match the * sample run in the assignment, but you should make sure * that changing these values causes the generated display * to change accordingly. */ import acm.graphics.*; import acm.program.*; import java.awt.*; public class Pyramid extends GraphicsProgram { /** Width of each brick in pixels */ private static final int BRICK_WIDTH = 30; /** Width of each brick in pixels */ private static final int BRICK_HEIGHT = 12; /** Number of bricks in the base of the pyramid */ private static final int BRICKS_IN_BASE = 14; private int startX = 50; private int startY = 300; public void run() { /* You fill this in. */ for (int i=0; i<BRICKS_IN_BASE; i++) { int rowX = startX + i*(BRICK_WIDTH/2); int currentY = startY - i*BRICK_HEIGHT; for (int j=0; j<BRICKS_IN_BASE-i; j++) { int currentX = rowX + (BRICK_WIDTH*j); GRect gr = new GRect(currentX, currentY, BRICK_WIDTH, BRICK_HEIGHT); add(gr); } } } }
运行结果:
4. 右手规则迷宫
import stanford.karel.*; public class Maze extends SuperKarel{ public void run() { while (!beepersPresent()) { if(!rightBlock()) { turnRight(); move(); } else if (!frontIsBlocked()) { move(); } else { turnAround(); } } } private boolean rightBlock() { turnRight(); boolean right = false; if (frontIsBlocked()) { right = true; } else { right = false; } turnLeft(); return right; } }
原则: 按照优先原则
1. 右手边有路, 就转右, 然后前进.
2. 如果右手边是墙, 前方能走, 就前进下一格.
3. 如果无法前进, 右手边也不能转, 就转向.(换一个右手墙, 因为之前的右手墙是死胡同)