第二章编程示例 笔记

欢迎大家转载,为保留作者成果,转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也可以下载!如果你没有积分,可以联系我索要!

构造函数

new GLabel(String x, y)

创建新的包含指定字符串的GLabel对象,该字符串从点(x,y)开始

new GRect(x, y, width, height)

创建新的有指定尺寸的GRect对象,该尺寸左上角位于(x,y)

new GOval(x, y, width, height)

创建新的GOval对象,其大小设置为与相同参数的GRect内部相切

new GLine(x1, y1, x2, y2)

创建新的连接点(x1,y1)和(x2,y2)的GLine对象

 

所有图形对象的通用方法

object.setColor(color)

将对象的颜色设置为color,它是java.awt里的一个普通颜色名

object.setLocation(x, y)

将对象的位置改变为点(x, y

object.move(dx, dy)

给x坐标增加dx,给y坐标增加dy来移动对象

 

GRect和Goval专用的方法

object.setFilled(fill)

设置对象是否填充(true表示填充,false表示不填充)

object.setFillColor(color)

设置用于填充对象内部的颜色,它可能与边界的颜色不同

 

GLabel专用的方法

label.setFont(string)

将字体设置为string指定位置的label,它给出了字体族名、风格和磅值

 


编程练习:

1、

/*
* File: HelloProgram.java
* -
* This program displays "my name is luoriver,SIP VoIP communication group is 323827101" on the screen.
* It is inspired by the first program in Brian
* Kernighan and Dennis Ritchie's classic book,
* The C Programming Language.
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class MyProgram extends GraphicsProgram {
	public void run() {
	    GLabel hello = new GLabel("my name is luoriver,Welcom to SIP VoIP communication group is 323827101",20,50) ;
	    hello.setColor(Color.BLUE);
	    add(hello) ;
		GLabel author = new GLabel("sign by luoriver", 200,70);
		author.setFont("宋体-24");
		add(author);
	}
}

2、

/* 
 * File: Algorithm.java
 * -----------------------------
 * This is a double algorithm code,
 * 键入两个数相乘,再除以2
 * 
 * Author luoriver SIP VoIP communication group is 323827101
 */
import acm.program.*;

public class Algorithm extends ConsoleProgram{
    public void run(){
        double b = readDouble("Enter b: "); 
        double h = readDouble("Enter h: ");
        double a = (b * h) / 2 ;
        println("a = " + a) ;
    }	
}

3、

/*
 * File: Add2Intergrate1.java
 * ----------------------------------------
 * This program adds two integers and prints their sum
 */
import acm.program.*;
public class Add2Intergrate1 extends ConsoleProgram{
	public void run(){
		println("This program adds two integers.") ;
		int n1 = readInt("Enter n1: ");
		int n2 = readInt("Enter n2: ");
		int n3 = readInt("Enter n3: ");
		int total = n1 + n2 + n3;
		println("The reselt is " + total + ".") ;
	}

}

4、

/*
* File: House.java
* ---------------------------
* This program displays a house on the screen.
* 
* Author luoriver
*/

import acm.graphics.*;
import acm.program.GraphicsProgram;

public class House extends GraphicsProgram {
	public void run(){
	    drawTriangle(); //画一个三角形
	    drawBigFond();  //画一个大长方形
	    drawTwoFond();  //画两个正方形
	    drawDoor();     //画大门
	     drawCircle(); //画锁
	      
	}
	
	private void drawTriangle(){
	
		GLine line1 = new GLine(300,100,100,200) ;
		GLine line2 = new GLine(300,100,500,200) ;
		GLine line3 = new GLine(100,200,500,200) ;
		add(line1);
		add(line2);
		add(line3);
	}	
	
	private void drawBigFond(){
		GRect fond = new GRect(100,200,400,200);
		add(fond);
	}
	
	private void drawTwoFond(){
		GRect fond1 = new GRect(150,220,50,50);
		GRect fond2 = new GRect(400,220,50,50);
		add(fond1);
		add(fond2);
	}
	
	private void drawDoor(){
	    GRect door = new GRect(250,250,100,150);	
	    add(door);
	}
	
	private void drawCircle(){
		GOval lock = new GOval(330,330,15,15);
		add(lock);
	}
}

5、

/*
* File: Face.java
* ---------------------------
* This program displays a face on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Face extends GraphicsProgram {
	public void run() {
	    drawRectangle();
	    drawEyes();
	    drawMouth();
	}
	
	private void drawRectangle(){
		GRect fond = new GRect(20,20,200,400);
		fond.setFilled(true);
		fond.setFillColor(Color.GRAY);
		add(fond);
			
	}
	
	private void drawEyes(){
	    GOval lefteye = new GOval(70,70,30,30) ;
	    GOval righteye = new GOval(150,70,30,30) ;
	    lefteye.setFilled(true);
	    righteye.setFilled(true);
	    lefteye.setFillColor(Color.ORANGE);
	    righteye.setFillColor(Color.ORANGE);
	    
	    add(lefteye);
	    add(righteye);
	}
	
	private void drawMouth(){
		GRect fond1 = new GRect(60,320,120,30);
		fond1.setFilled(true);
		fond1.setFillColor(Color.WHITE);
		add(fond1);
	}
}

6、

/*
* File: Target.java
* ---------------------------
* This program displays a target on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Target extends GraphicsProgram {
	public void run() {
	    drawOut();
	    drawMiddle();
	    drawIn();
	}
	
	
	private void drawOut(){
	    GOval out = new GOval(100,50,300,300) ;
	    out.setFilled(true);
	    out.setFillColor(Color.RED);
	    add(out);
	    
	}
	
	private void drawMiddle(){
		GOval out = new GOval(150,100,200,200) ;
	    out.setFilled(true);
	    out.setFillColor(Color.WHITE);
	    add(out);
	}
	
	private void drawIn(){
		GOval out = new GOval(200,150,100,100) ;
	    out.setFilled(true);
	    out.setFillColor(Color.RED);
	    add(out);
	}
	

}

7、

/*
* File: Pentacyclic.java
* ---------------------------
* This program displays a pentacyclic on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Pentacyclic extends GraphicsProgram {
	public void run() {
	    drawTriCircle();
	    drawDoubleCircle();
	}
	
	private void drawTriCircle(){
		GOval fond1 = new GOval(20,20,80,80);
		GOval fond2 = new GOval(120,20,80,80);
		GOval fond3 = new GOval(220,20,80,80);
		fond1.setColor(Color.BLUE) ;
		fond2.setColor(Color.YELLOW) ;
		fond3.setColor(Color.BLACK) ;
		add(fond1);
		add(fond2);
		add(fond3);
			
	}
	
	public void drawDoubleCircle(){
		GOval fond1 = new GOval(70,50,80,80);
		GOval fond2 = new GOval(170,50,80,80);
		fond1.setColor(Color.GREEN) ;
		fond2.setColor(Color.RED) ;
		add(fond1);
		add(fond2);
		
	}
}

8、

/*
* File: Pentacyclic2.java
* ---------------------------
* This program displays a pentacyclic on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class Pentacyclic2 extends GraphicsProgram {
	public void run() {
	    drawBlueCircle();
	    drawYellowCircle();
	    drawBlackCircle();
	    drawGreenCircle();
	    drawRedCircle();
	}
	
	private void drawBlueCircle(){
		for(int i=0;i<3;i++){
		GOval blue = new GOval(20+i,20+i,80-2*i,80-2*i);
		blue.setColor(Color.BLUE) ;
		add(blue);
		}
	}
	
	private void drawYellowCircle(){
		for(int i=0;i<3;i++){
		GOval yellow = new GOval(120+i,20+i,80-2*i,80-2*i);
		yellow.setColor(Color.YELLOW) ;
		add(yellow);
		}
	}
	
	private void drawBlackCircle(){
		for(int i=0;i<3;i++){
		GOval black = new GOval(220+i,20+i,80-2*i,80-2*i);
		black.setColor(Color.BLACK) ;
		add(black);
		}
	}
	
	private void drawGreenCircle(){
		for(int i=0;i<3;i++){
		GOval green = new GOval(70+i,50+i,80-2*i,80-2*i);
		green.setColor(Color.GREEN) ;
		add(green);
		}
	}
	
	private void drawRedCircle(){
		for(int i=0;i<3;i++){
		GOval red = new GOval(170+i,50+i,80-2*i,80-2*i);
		red.setColor(Color.RED) ;
		add(red);
		}
	}
	
	
}

9、

/*
* File: Rainbow.java
* ---------------------------
* This program displays rainbow on the screen.
* 
* Author luoriver
*/

import java.awt.*;

import acm.graphics.*;
import acm.program.GraphicsProgram;
public class RainBow extends GraphicsProgram {
	public void run() {
	    drawBackGround();
	    drawRainBow();
	    
	    
	}
	
	private void drawBackGround(){
		GRect fond = new GRect(0,0,20000,20000);
		fond.setFilled(true);
		fond.setFillColor(Color.CYAN);
		add(fond); 	
	}
	
	private void drawRainBow(){
		//画一个红圈
		for(int i=0;i<10;i++){
			GOval red = new GOval(-120+i,120+i,1200-2*i,1200-2*i);
			red.setColor(Color.RED) ;
			add(red);	
		}
		
		//画一个橙色
		for(int i=0;i<10;i++){
			GOval orange = new GOval(-110+i,130+i,1180-2*i,1180-2*i);
			orange.setColor(Color.ORANGE) ;
			add(orange);
		}
		//画一个黄色
		for(int i=0;i<10;i++){
			GOval yellow = new GOval(-100+i,140+i,1160-2*i,1160-2*i);
			yellow.setColor(Color.YELLOW) ;
			add(yellow);
		}
		//画一个绿色
		for(int i=0;i<10;i++){
			GOval green = new GOval(-90+i,150+i,1140-2*i,1140-2*i);
			green.setColor(Color.GREEN) ;
			add(green);
		}
		
		//画一个蓝色
		for(int i=0;i<10;i++){
			GOval blue = new GOval(-80+i,160+i,1120-2*i,1120-2*i);
			blue.setColor(Color.blue) ;
			add(blue);
		}
		
		//画一个粉色
		for(int i=0;i<10;i++){
			GOval pink = new GOval(-70+i,170+i,1100-2*i,1100-2*i);
			pink.setColor(Color.PINK) ;
			add(pink);
		}
		
	}
}	



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值