做完一个了,
如下:
还剩下两个,哈哈import java.applet.Applet;
import java.awt.*;
public class Rect extends Applet //继承Applet类
{
int fltX=20 ; //定义横坐标变量并初始化
int fltY=20 ; //定义纵坐标变量并初始化
int height=200;
int width=100;
public void paint(Graphics g)
{
g.setColor(Color.blue) ;//设置画图颜色为蓝色
g.drawRect(20,20,height,width) ; //按要求画矩形
g.setColor(Color.red) ;//设置画图颜色为红色
for(int i=1;i<=3;i++)//for循环均匀画三条红色线条,使分成四行
{
fltY+=(100/4) ;
g.drawLine(fltX,fltY,200+fltX,fltY) ;
}
fltY=20;//回到初始值
for(int i=1;i<=7;i++)//for循环均匀画七条红色线条,使分成八列
{
fltX+=(200/8) ;
g.drawLine(fltX,fltY,fltX,100+fltY) ;
}
fltX=20;//回到初始值
} }
如下:
还剩下两个,哈哈import java.applet.Applet;
import java.awt.*;
public class Rect extends Applet //继承Applet类
{
int fltX=20 ; //定义横坐标变量并初始化
int fltY=20 ; //定义纵坐标变量并初始化
int height=200;
int width=100;
public void paint(Graphics g)
{
g.setColor(Color.blue) ;//设置画图颜色为蓝色
g.drawRect(20,20,height,width) ; //按要求画矩形
g.setColor(Color.red) ;//设置画图颜色为红色
for(int i=1;i<=3;i++)//for循环均匀画三条红色线条,使分成四行
{
fltY+=(100/4) ;
g.drawLine(fltX,fltY,200+fltX,fltY) ;
}
fltY=20;//回到初始值
for(int i=1;i<=7;i++)//for循环均匀画七条红色线条,使分成八列
{
fltX+=(200/8) ;
g.drawLine(fltX,fltY,fltX,100+fltY) ;
}
fltX=20;//回到初始值
} }
解决方案 »
- eclipse插件开发的时候如何把导出来的插件变成目录
- Math.random随机取的数是否有可能重复
- 为什么文件最后修改时间用于为0
- 文件分发问题
- Java SE和Java EE有什么区别
- 捕捉键盘事件时遇到奇怪问题
- JNI加载不了so?但so在路径内啊。
- executeUpdate和executeQuery语句返回一个什么值是代表查询,修改失败?
- 请教java的分布式应用,我建立好的server,但是只要执行到server.accept()就没反映了,窗口上的其它按钮什么的都不能用了!
- struts配置文件加载问题
- 请问在 JDK 中用哪个类对 String 进行 Base64编码?
- 对于.lnk的shortcut文件,如何在Java中取得它的实际地址?
第一个://HTML CODE: <applet code="BlueRect.class" width="240" height="140"></applet>import java.awt.*;
public class BlueRect extends java.applet.Applet { private int x = 20;
private int y = 20;
private int width = 200;
private int height = 100;
private int columns = 8;
private int rows = 4; public void paint(Graphics g) { g.setColor(Color.blue);
g.fillRect(x, y, width, height);
g.setColor(Color.white); for(int i = 1; i < columns; i++) {
int startX = width / columns * i + x;
g.drawLine(startX, y, startX, y + height);
} for(int i = 1; i < rows; i++) {
int startY = height / rows * i + y;
g.drawLine(x, startY, x + width, startY);
} }
}第二、三个还没写出来。-_-!
import java.awt.event.*;
public class Chooser extends java.applet.Applet implements ActionListener {
private Label output = new Label("请选择");
private Checkbox option1 = new Checkbox("学习");
private Checkbox option2 = new Checkbox("玩耍");
private Button submit = new Button("提交");
public void init() {
setLayout(new GridLayout(2, 1));
output.setAlignment(Label.CENTER);
add(output);
Panel selectPane = new Panel();
selectPane.add(option1);
selectPane.add(option2);
submit.addActionListener(this);
selectPane.add(submit);
add(selectPane);
}
public void actionPerformed(ActionEvent e) {
if(option1.getState() && !option2.getState())
output.setText("我只喜欢学习");
else if(!option1.getState() && option2.getState())
output.setText("我就喜欢玩耍");
else if(option1.getState() && option2.getState())
output.setText("玩耍和学习我都喜欢");
}
}
//HTML CODE: <applet code="PinkRect.class" width="150" height="100"></applet>import java.awt.*;
public class PinkRect extends java.applet.Applet implements Runnable {
private final int LEFT = 1;
private final int RIGHT = 0; private int rectX = 5;
private int rectY = 5;
private int circleRadius = 15;
private int circleX = 0;
private int circleY;
private int direction;
public void init() {
setBackground(Color.blue);
direction = RIGHT;
new Thread(this).start();
} public void paint(Graphics g) {
int rectWidth = getWidth() - rectX * 2;
int rectHeight = getHeight() - rectY * 2;
g.setColor(Color.pink);
g.fillRect(rectX, rectY, rectWidth, rectHeight);
circleY = getHeight() / 2 - circleRadius / 2;
g.setColor(Color.white);
g.fillOval(circleX, circleY, circleRadius, circleRadius); }
public void run() {
while(true) {
if(direction == LEFT)
circleX -= 5;
else
circleX += 5;
//如果到了画布边缘就改变方向
if(circleX > getWidth() - circleRadius)
direction = LEFT;
else if(circleX < 0)
direction = RIGHT; try {
Thread.sleep(50);
} catch(Exception e) { }
repaint();
}
}
}
http://carl.blueidea.com/app.htm
高手在吗?
怎么没回应了?
{
paint(g);
}或者直接OffscreanGraphicsGraphics myGraphics;
Image myOffscreenImage = createImage(size().width, size().height);
Graphics offscreenGraphics = myOffscreenImage.getGraphics();
offscreenGraphics.drawImage(img, 50, 50, this);
myGraphics.drawImage(myOffscreenImage, 0, 0, this);
PinkRect.java:55: illegal start of expression
public void update(Graphics g){ paint(g); }
^
PinkRect.java:56: ';' expected
^
2 errors
第三个改进如下:import java.awt.*;public class PinkRect extends java.applet.Applet implements Runnable { private final int LEFT = 1;
private final int RIGHT = 0; private int rectX = 5;
private int rectY = 5;
private int circleRadius = 15;
private int circleX = 6;
private int circleY;
private int direction; public void init() {
setBackground(Color.blue);
direction = RIGHT;
new Thread(this).start();
} public void paint(Graphics g) {
int rectWidth = getWidth() - rectX * 2;
int rectHeight = getHeight() - rectY * 2;
g.setColor(Color.pink);
g.fillRect(rectX, rectY, rectWidth, rectHeight);
circleY = getHeight() / 2 - circleRadius / 2;
g.setColor(Color.white);
g.fillOval(circleX, circleY, circleRadius, circleRadius);
} public void update(Graphics g) {
paint(g);
} public void run() {
while(true) {
if(direction == LEFT) circleX -= 1;
else circleX += 1; //如果到了画布边缘就改变方向
if(circleX >= getWidth() - circleRadius - 5) direction = LEFT;
else if(circleX <= 5) direction = RIGHT; try {
Thread.sleep(10);
} catch(Exception e) { } repaint(); }
}
}to楼主,我的程序不难懂吧?变量/常量名都是自说明性的,我只在我认为需要注释的地方加了注释。