欢迎大家转载,为保留作者成果,转载请注明出处,http://blog.youkuaiyun.com/netluoriver,有些文件在资源中也可以下载!如果你没有积分,可以联系我索要!
1、
</pre></p><p><pre name="code" class="java">package Ninth;
/* File: Luoriver.java
* ------------------------
* the ninth chapter: 课后编程练习第1题
* 注意把图像放在包目录下
* http://blog.youkuaiyun.com/netluoriver
*
*/
import acm.graphics.*;
import acm.program.*;
public class Luoriver extends GraphicsProgram {
@Override
public void run() {
// 此时这里的图像要放在工程的根目录才可以显示
add(new GImage("luoriver.jpeg", 50, 50));
}
}
2、
package Ninth;
/**
* File: ninthFound.java
* -------------------------
* 第9章课后编程练习第2题
*/
import java.awt.Color;
import acm.graphics.GRect;
import acm.program.*;
import acm.util.*;
public class ninthFound extends GraphicsProgram {
public void run() {
/*
* 设置正方形为屏幕中间
*/
double x = getWidth() / 2;
double y = getHeight() / 2;
double dx = x - FOUND_WIDTH / 2;
double dy = y - FOUND_WIDTH / 2;
while (true) {
GRect foundGRect = new GRect(dx, dy, FOUND_WIDTH, FOUND_WIDTH);
foundGRect.setFilled(true);
foundGRect.setColor(Color.white);
foundGRect.setFillColor(rgen.nextColor());
add(foundGRect);
pause(1000);
}
}
/*
* create an instance variable for the random number generator
*/
private RandomGenerator rgen = RandomGenerator.getInstance();
/**
* 正方形的宽度和高度
*
* @param FOUND_WIDTH
* 定义正方形的宽度
*/
private static final double FOUND_WIDTH = 100;
}
3、
GDimand
package Ninth;
/* File: GDiamond.java
* ------------------------
* 第9章第3题
* http://blog.youkuaiyun.com/netluoriver
*
*/
import acm.graphics.*;
public class GDiamond extends GPolygon{
public GDiamond(double width, double height) {
double dx = width / 2;
double dy = height / 2;
addVertex(-dx / 2, 0);
addVertex(0, dy / 2);
addVertex(dx / 2, 0);
addVertex(0, -dy / 2);
}
}
GTrapezoid
package Ninth;
/* File: GTrapezoid.java
* ------------------------
* 第9章: 课后编程练习第4题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import acm.graphics.GMath;
import acm.graphics.GPolygon;
public class GTrapezoid extends GPolygon {
public GTrapezoid(double side_top,double height, double angle){
addVertex(- side_top / 2, -height / 2);
addVertex(side_top / 2, -height / 2);
addPolarEdge(height * GMath.tanDegrees(angle), -angle );
addPolarEdge(side_top + height * GMath.tanDegrees(angle), 3 * angle);
}
}
GTshape
package Ninth;
/* File: GTshape.java
* ------------------------
* 第9章: 课后编程练习第4题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import acm.graphics.GMath;
import acm.graphics.GPolygon;
import acm.program.GraphicsProgram;
public class GTshape extends GPolygon {
public GTshape(double width, double height, double thickness) {
double w = width;
double h = height;
double t = thickness;
addVertex(-w, -h);
addEdge(w, 0);
addEdge(0, t);
addEdge(-(w / 2 - t / 2), 0);
addEdge(0, h - t);
addEdge(-t, 0);
addEdge(0, -(h - t));
addEdge(-(w / 2 - t / 2), 0);
}
}
5
package Ninth;
/**
* Pumpkin.java
* ---------------------------
* * 第9章: 课后编程练习第5题
*
* http://blog.youkuaiyun.com/netluoriver
*
*this is a show hallowmas
* @author luoriver
*
* 2014年7月13日上午9:40:36
*/
import java.awt.Color;
import acm.program.*;
import acm.graphics.GOval;
public class Pumpkin extends GraphicsProgram{
public void run(){
double dx = (getWidth() - head_width) / 2;
double dy= (getHeight() - head_width) / 2;
/**
* draw head
*/
GOval head = new GOval(head_width, head_width);
head.setFillColor(Color.ORANGE);
head.setFilled(true);
add(head, dx, dy);
/**
* draw leftEye
*/
Pumpkin_Eye leftEye = new Pumpkin_Eye(eye_height, eye_width);
add(leftEye, dx + eye_insert, dy + head_width / 4);
/**
* draw rightEye
*/
Pumpkin_Eye rightEye = new Pumpkin_Eye(eye_height, eye_width);
add(rightEye, dx + head_width - eye_insert, dy + head_width / 4);
/**
* draw Nose
*/
Pumpkin_Nose nose = new Pumpkin_Nose(nose_height, nose_width);
add(nose, getWidth() / 2, (getHeight() + nose_height) / 2 );
}
//定义头的大小
private static final int head_width = 100;
//定义眼睛的大小
private static final int eye_height = 20;
private static final int eye_width = 20;
private static final int eye_insert=25;
//定义鼻子的大小
private static final int nose_width = 20;
private static final int nose_height = 20;
}
Pumpkin_Eye
package Ninth;
/* File: Pumpkin_Eye.java
* ------------------------
* 第9章: 课后编程练习第5题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import acm.program.GraphicsProgram;
import acm.graphics.GMath;
import acm.graphics.GPolygon;
public class Pumpkin_Eye extends GPolygon{
public Pumpkin_Eye(int eye_height, int eye_width){
addVertex(-eye_width / 2, 0);
addVertex( eye_width / 2, 0);
addVertex(eye_width/16, eye_height);
setFilled(true);
setFillColor(Color.BLACK);
}
}
Pumpkin_Nose
package Ninth;
/* File: Pumpkin_Nose.java
* ------------------------
* 第9章: 课后编程练习第5题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import acm.program.GraphicsProgram;
import acm.graphics.GMath;
import acm.graphics.GPolygon;
public class Pumpkin_Nose extends GPolygon {
public Pumpkin_Nose(int Nose_height, int Nose_width){
addVertex(Nose_width / 16, -Nose_height /2 );
addVertex(Nose_width /2 , Nose_height /2 );
addVertex(-Nose_width /2, Nose_height /2 );
setFilled(true);
setFillColor(Color.BLACK);
}
}
6、
package Ninth;
/* File: Pumpkin_pices.java
* ------------------------
* 第9章: 课后编程练习第6题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import acm.program.GraphicsProgram;
import acm.graphics.GArc;
import acm.graphics.GMath;
import acm.graphics.*;
import acm.graphics.GPolygon;
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
public class Pumpkin_pices extends GraphicsProgram {
private static final long serialVersionUID = 1L;
private double sweep = 360 / N_PIECES + 360 % N_PIECES;
// Create the pie by the piece
public void run() {
for (int i = 0; i < N_PIECES; i++) {
piePiece(r, sweep * i);
}
}
// Create the object, set the properties, and add it to the canvas.
private void piePiece(double radius, double start) {
GArc piece = new GArc(2 * radius, 2 * radius, start, sweep);
piece.setFilled(true);
piece.setFillColor(Color.ORANGE);
add(piece, (getWidth() - 2 * radius) / 2, (getHeight() - 2 * radius) / 2);
}
private static final int N_PIECES = 9;
private static final int r = 50;
}
package Ninth;
/* File: Heart.java
* ------------------------
* 第9章: 课后编程练习第7题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import acm.program.GraphicsProgram;
import acm.graphics.GMath;
import acm.graphics.GPolygon;
public class Heart extends GPolygon{
public Heart (int SIDE){
addVertex(SIDE /2, 0);
addVertex(0, SIDE /2 );
addVertex(-SIDE / 2, 0);
addArc(SIDE * GMath.sinDegrees(45) , SIDE * GMath.sinDegrees(45), -135, -180);
addArc(SIDE * GMath.sinDegrees(45), SIDE * GMath.sinDegrees(45), -225, -180);
setFilled(true);
setFillColor(Color.RED);
}
}
10、不会,只写了个圆圈:
package Ninth;
/* File: Ninth_ten.java
* ------------------------
* 第9章: 课后编程练习第10题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import javafx.scene.shape.Circle;
import acm.program.GraphicsProgram;
import acm.program.GraphicsProgram;
import acm.graphics.GLabel;
import acm.graphics.GLine;
import acm.graphics.GMath;
import acm.graphics.GOval;
import acm.graphics.GPolygon;
public class Ninth_ten extends GraphicsProgram {
public void run() {
int x = getWidth();
int y = getHeight();
// dx为打印位置的开始
int dx = x / 2 - R;
int sy = vertical_distance;
int count =(y - vertical_distance) / (2 * R + vertical_distance);
println("the width is :" + x );
println("the height is :" + y );
print(count);
for (int i = 0; i < count; i++) {
for (int j = 0; j <= i; j++) {
GOval circle = new GOval(R, R);
add(circle, dx - i * R + j * 2 * R, sy + i * ( vertical_distance));
GLabel digit = new GLabel("1");
// add(digit, dx - i * R + j * 2 * R, sy + i * ( vertical_distance + R));
}
}
}
/*
* for(int i = 0; i < 10; i++){ for(int j = 0; j< i; j++ ){ print(j);
*
*
* } println(); }
*/
private static final int R =10;
private static final int cross_distance = 6;
private static final int vertical_distance = 10;
}
11、不会,只写了第1图形
package Ninth;
/* File: Embroidery.java
* ------------------------
* 第9章: 课后编程练习第11题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import acm.program.GraphicsProgram;
public class Embroidery extends GraphicsProgram{
public void run(){
wood_block newBlock = new wood_block(LENGTH);
newBlock.setColor(Color.black);
add(newBlock);
nested_square nested = new nested_square(LENGTH);
add(nested, 200, 200);
}
private static final double LENGTH= 67;
}
构造函数:
QulitBlock
package Ninth;
/* File: QulitBlock.java
* ------------------------
* 第9章: 课后编程练习第11题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import org.omg.CORBA.DoubleSeqHelper;
import acm.graphics.GCompound;
import acm.graphics.GRect;
import acm.graphics.*;
public class QulitBlock extends GCompound{
public QulitBlock(double LENGTH){
GRect found = new GRect(LENGTH, LENGTH);
found.setFilled(true);
found.setFillColor(Color.white);
add(found, 0,0);
}
/*
* 设置正方形的边长
*/
// private static final double WIDTH = 20;
}
package Ninth;
/* File: wood_block.java
* ------------------------
* 第9章: 课后编程练习第11题
*
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import com.sun.javafx.geom.AreaOp.AddOp;
import Ninth.QulitBlock;
import acm.program.*;
import acm.graphics.*;
public class wood_block extends QulitBlock {
// @Override
public wood_block(double LENGTH){
super(LENGTH);
double WOOD_HEIGHT = LENGTH - WOOD_WIDTH;
for (int i = 0; i < 4; i++) {
GRect rect1 = new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT);
rect1.setFilled(true);
rect1.setFillColor(Color.gray);
add(rect1,i * WOOD_HEIGHT, i * WOOD_HEIGHT);
GRect rect2 = new GRect(WOOD_HEIGHT, WOOD_WIDTH - 2 * i * WOOD_HEIGHT);
rect2.setFilled(true);
rect2.setFillColor(Color.gray);
add(rect2,i * WOOD_HEIGHT, (i +1) * WOOD_HEIGHT);
GRect rect3 = new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT);
rect3.setFilled(true);
rect3.setFillColor(Color.gray);
add(rect3,(i+1) * WOOD_HEIGHT, WOOD_WIDTH - i * WOOD_HEIGHT);
GRect rect4 = new GRect(WOOD_HEIGHT, WOOD_WIDTH - 2 * i * WOOD_HEIGHT);
rect4.setFilled(true);
rect4.setFillColor(Color.gray);
add(rect4, WOOD_WIDTH - i * WOOD_HEIGHT, i * WOOD_HEIGHT);
/*new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT).setFilled(true);
new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT).setFillColor(Color.GRAY);
add(new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT), i
* WOOD_HEIGHT, i * WOOD_HEIGHT);*/
/*add(new GRect(WOOD_HEIGHT, WOOD_WIDTH - 2 * i * WOOD_HEIGHT),
WOOD_WIDTH - i * WOOD_HEIGHT, i * WOOD_HEIGHT);
add(new GRect(WOOD_WIDTH - 2 * i * WOOD_HEIGHT, WOOD_HEIGHT),
WOOD_HEIGHT + i * WOOD_HEIGHT, WOOD_WIDTH - i * WOOD_HEIGHT);
add(new GRect(WOOD_HEIGHT, WOOD_WIDTH - 2 * i * WOOD_HEIGHT), i
* WOOD_HEIGHT, WOOD_HEIGHT + i * WOOD_HEIGHT);*/
}
}
// private static final double LENGTH = 67;
private static final double WOOD_WIDTH = 60;
}
12
package Ninth;
/* File: draw_pc_man.java
* ------------------------
* 第9章第12题
* http://blog.youkuaiyun.com/netluoriver
*
*/
import java.awt.Color;
import java.beans.Visibility;
import org.omg.CORBA.PRIVATE_MEMBER;
import acm.graphics.GArc;
import acm.graphics.GCompound;
import acm.graphics.GPolygon;
import acm.program.GraphicsProgram;
import com.sun.javafx.geom.AreaOp.AddOp;
public class draw_pc_man extends GraphicsProgram{
public void run() {
int start = 60;
int sweep = 240;
int step_angle = 15;
int dy = 2 * step_angle;
int move_location = 0;
int Y = getHeight() / 2;
while(true){
/**
** 画一个与背景色一样的圆形是为了遮挡pc_man张开时生成的阴影,可以将这段代码注释后
**查看不用这段代码产生的效果
*/
Pc_man man = new Pc_man(side, start, sweep);
add(man,0,Y - side / 2);
man.move(move_location, 0);
// pause(pause_time);
pause(MOUSE_PAUSE_TIEM);
start += step_angle;
sweep -= dy;
//当小于0度或大于60度时便向相反的方向运动
if(start <= 0 || start > 60) step_angle = -step_angle;
//当大于360或小于240度时便向相反的方向运动
if(sweep >= 360 || sweep < 240) dy = -dy;
GArc man1 = new GArc(side, side, 0, 360);
man1.setFilled(true);
man1.setColor(Color.white);
man1.setFillColor(Color.white);
add(man1,0,Y);
man1.move(move_location, 0);
move_location += move_rate;
removeAll();
// double i = man.getLocation();
}
}
private static final double side = 30;
private int move_rate = 1;
private static final int MOUSE_PAUSE_TIEM = 100;
}
构造函数:
package Ninth;
import java.awt.Color;
import jdk.internal.dynalink.beans.StaticClass;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.omg.CORBA.PUBLIC_MEMBER;
import com.sun.xml.internal.ws.api.config.management.policy.ManagedClientAssertion;
import acm.graphics.GArc;
import acm.graphics.GCompound;
import acm.graphics.GPolygon;
import acm.program.GraphicsProgram;
//public class Pc_man extends GraphicsProgram{
public class Pc_man extends GCompound {
public Pc_man(double side, int start, int sweep) {
GArc man = new GArc(side, side, start, sweep);
man.setFilled(true);
man.setFillColor(Color.YELLOW);
add(man, 0, 0);
}
}
后面的没有做