/*
* Scene3.java
*
* Created on 2005年11月20日, 下午2:41
*/package JAVA_2D;/**
*
* @author Administrator
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Scene3 extends JFrame{
static int bx=90;
static int by=90;
static int OvalW=60;
static int OvalH=60;
static int r=30;
static int blank=5;
static int lLength=20;
static int moveL=(int)(1.4*blank);
static int moveC=(int)(0.7*lLength/2);
static int currentX=850;
static int currentY=180;
final static int braeStartX=492;
final static int braeStartY=620;
static int braeMX=50;
static int braeMY=45;
static JLabel dog=new JLabel();
static int x=0;
static int y=0;
/** Creates a new instance of Scene3 */
public Scene3() {
this.setSize(new Dimension(900,650));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setVisible(true);
//dog.setBounds(braeStartX,braeStartY,120,130);
dog.setIcon(new ImageIcon(getClass().getResource("images/21.gif")));
//this.getContentPane().add(dog);
getContentPane().setBackground(Color.white);
//myInit();
}
/*public void myInit() {
this.getContentPane().setLayout(null);
//dog.setBounds(braeStartX,braeStartY,120,130);
dog.setIcon(new ImageIcon(getClass().getResource("images/21.gif")));
this.getContentPane().add(dog);
getContentPane().setBackground(Color.white);
}*/
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.red);
g2.fillOval(bx,by,OvalW,OvalH);
g2.setColor(Color.yellow);
g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC,(bx-moveL)+moveC,(by-moveL)-moveC);//左上45度
g2.drawLine((bx-moveL)+moveC+lLength,(by-moveL)-moveC,(bx-moveL)+moveC+lLength+lLength,(by-moveL)-moveC);//上横线
g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC,(bx+2*r+moveL)-moveC,(by-moveL)-moveC);//右上45度
g2.drawLine((bx-moveL)+moveC+lLength,(by+2*r+moveL)+moveC,(bx-moveL)+moveC+lLength+lLength,(by+2*r+moveL)+moveC);//下横线
g2.drawLine((bx-moveL)-moveC,(by+2*r+moveL)-moveC,(bx-moveL)+moveC,(by+2*r+moveL)+moveC);//左下45度
g2.drawLine((bx+2*r+moveL)+moveC,(by+2*r+moveL)-moveC,(bx+2*r+moveL)-moveC,(by+2*r+moveL)+moveC);//右下45度
g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC+lLength,(bx-moveL)-moveC,(by-moveL)+moveC+lLength+lLength);//左竖线
g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength,(bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength+lLength);//右竖线
//第一个三角形的顶点
int treeTopX1=(bx-moveL)+moveC+lLength+lLength/2;
int treeTopY1=(by+2*r+moveL)+moveC+2*5*lLength;
int i=1;
int triangleL=(int)(0.7*2*i*lLength);
//第一个三角形的点
int xPoints[]={treeTopX1,treeTopX1-triangleL,treeTopX1+triangleL};
int yPoints[]={treeTopY1,treeTopY1+triangleL,treeTopY1+triangleL};
g2.setColor(Color.green);
g2.fillPolygon(xPoints, yPoints, 3);
//第二个三角形的顶点
int treeTopX2=treeTopX1;
int treeTopY2=treeTopY1+triangleL;
i++;
triangleL=(int)(0.7*2*i*lLength);
//第二个三角形的点
int xPoints1[]={treeTopX2,treeTopX2-triangleL,treeTopX2+triangleL};
int yPoints1[]={treeTopY2,treeTopY2+triangleL,treeTopY2+triangleL};
g2.fillPolygon(xPoints1, yPoints1, 3);
//第三个三角形的顶点
int treeTopX3=treeTopX2;
int treeTopY3=treeTopY2+triangleL;
i++;
triangleL=(int)(0.7*2*i*lLength);
//第三个三角形的点
int xPoints2[]={treeTopX3,treeTopX3-triangleL,treeTopX3+triangleL};
int yPoints2[]={treeTopY3,treeTopY3+triangleL,treeTopY3+triangleL};
g2.fillPolygon(xPoints2, yPoints2, 3);
g2.setColor(Color.GRAY);
//树根
g2.fillRect(treeTopX3-triangleL+triangleL/4*3,treeTopY3+triangleL,triangleL/2, triangleL/2);
g2.setColor(Color.black);
//g2.drawLine(445,650,985,200);
for(i=0;i<11;i++) {
//for(int j=0;j<11;j++) {
g2.drawLine(braeStartX+i*braeMX,braeStartY-i*braeMY,braeStartX+10*braeMX,braeStartY-i*braeMY);
g2.drawLine(braeStartX+i*braeMX,braeStartY-i*braeMY,braeStartX+i*braeMX,braeStartY-i*braeMY+i*braeMY);
//}
}
g2.drawLine(braeStartX,braeStartY,braeStartX+i*braeMX,braeStartY-i*braeMY);
}
/*public void moveDog() {
javax.swing.Timer t=new javax.swing.Timer(1000,new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int x=braeMX-60,y=braeMY+130;
x=x+braeMX;
y=y+braeMY;
dog.setBounds(x,y,120,130); }
}
);
t.start();
}*/
public static void main(String args[]) {
Scene3 scene3=new Scene3();
scene3.getContentPane().add(dog);
//scene3.setBounds(0, 0, 900, 650);
javax.swing.Timer t=new javax.swing.Timer(1000,new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
x=braeStartX-60;
y=braeStartY-133;
int n=0;
int count=0;
/*if(count%2==0) {
n=n+5;
}
else {
n=n-3;
}*/
x=x+50;
y=y-50;
//count++;
/*if(count==7) {
x=braeStartX-60;
y=braeStartY-150;
}*/
dog.setBounds(x,y,120,130); }
}
);
t.start();
//scene3.getContentPane().setBackground(Color.white);
}
}
以上是源程序,在这里先谢谢各位拉。
* Scene3.java
*
* Created on 2005年11月20日, 下午2:41
*/package JAVA_2D;/**
*
* @author Administrator
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Scene3 extends JFrame{
static int bx=90;
static int by=90;
static int OvalW=60;
static int OvalH=60;
static int r=30;
static int blank=5;
static int lLength=20;
static int moveL=(int)(1.4*blank);
static int moveC=(int)(0.7*lLength/2);
static int currentX=850;
static int currentY=180;
final static int braeStartX=492;
final static int braeStartY=620;
static int braeMX=50;
static int braeMY=45;
static JLabel dog=new JLabel();
static int x=0;
static int y=0;
/** Creates a new instance of Scene3 */
public Scene3() {
this.setSize(new Dimension(900,650));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setVisible(true);
//dog.setBounds(braeStartX,braeStartY,120,130);
dog.setIcon(new ImageIcon(getClass().getResource("images/21.gif")));
//this.getContentPane().add(dog);
getContentPane().setBackground(Color.white);
//myInit();
}
/*public void myInit() {
this.getContentPane().setLayout(null);
//dog.setBounds(braeStartX,braeStartY,120,130);
dog.setIcon(new ImageIcon(getClass().getResource("images/21.gif")));
this.getContentPane().add(dog);
getContentPane().setBackground(Color.white);
}*/
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.red);
g2.fillOval(bx,by,OvalW,OvalH);
g2.setColor(Color.yellow);
g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC,(bx-moveL)+moveC,(by-moveL)-moveC);//左上45度
g2.drawLine((bx-moveL)+moveC+lLength,(by-moveL)-moveC,(bx-moveL)+moveC+lLength+lLength,(by-moveL)-moveC);//上横线
g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC,(bx+2*r+moveL)-moveC,(by-moveL)-moveC);//右上45度
g2.drawLine((bx-moveL)+moveC+lLength,(by+2*r+moveL)+moveC,(bx-moveL)+moveC+lLength+lLength,(by+2*r+moveL)+moveC);//下横线
g2.drawLine((bx-moveL)-moveC,(by+2*r+moveL)-moveC,(bx-moveL)+moveC,(by+2*r+moveL)+moveC);//左下45度
g2.drawLine((bx+2*r+moveL)+moveC,(by+2*r+moveL)-moveC,(bx+2*r+moveL)-moveC,(by+2*r+moveL)+moveC);//右下45度
g2.drawLine((bx-moveL)-moveC,(by-moveL)+moveC+lLength,(bx-moveL)-moveC,(by-moveL)+moveC+lLength+lLength);//左竖线
g2.drawLine((bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength,(bx+2*r+moveL)+moveC,(by-moveL)+moveC+lLength+lLength);//右竖线
//第一个三角形的顶点
int treeTopX1=(bx-moveL)+moveC+lLength+lLength/2;
int treeTopY1=(by+2*r+moveL)+moveC+2*5*lLength;
int i=1;
int triangleL=(int)(0.7*2*i*lLength);
//第一个三角形的点
int xPoints[]={treeTopX1,treeTopX1-triangleL,treeTopX1+triangleL};
int yPoints[]={treeTopY1,treeTopY1+triangleL,treeTopY1+triangleL};
g2.setColor(Color.green);
g2.fillPolygon(xPoints, yPoints, 3);
//第二个三角形的顶点
int treeTopX2=treeTopX1;
int treeTopY2=treeTopY1+triangleL;
i++;
triangleL=(int)(0.7*2*i*lLength);
//第二个三角形的点
int xPoints1[]={treeTopX2,treeTopX2-triangleL,treeTopX2+triangleL};
int yPoints1[]={treeTopY2,treeTopY2+triangleL,treeTopY2+triangleL};
g2.fillPolygon(xPoints1, yPoints1, 3);
//第三个三角形的顶点
int treeTopX3=treeTopX2;
int treeTopY3=treeTopY2+triangleL;
i++;
triangleL=(int)(0.7*2*i*lLength);
//第三个三角形的点
int xPoints2[]={treeTopX3,treeTopX3-triangleL,treeTopX3+triangleL};
int yPoints2[]={treeTopY3,treeTopY3+triangleL,treeTopY3+triangleL};
g2.fillPolygon(xPoints2, yPoints2, 3);
g2.setColor(Color.GRAY);
//树根
g2.fillRect(treeTopX3-triangleL+triangleL/4*3,treeTopY3+triangleL,triangleL/2, triangleL/2);
g2.setColor(Color.black);
//g2.drawLine(445,650,985,200);
for(i=0;i<11;i++) {
//for(int j=0;j<11;j++) {
g2.drawLine(braeStartX+i*braeMX,braeStartY-i*braeMY,braeStartX+10*braeMX,braeStartY-i*braeMY);
g2.drawLine(braeStartX+i*braeMX,braeStartY-i*braeMY,braeStartX+i*braeMX,braeStartY-i*braeMY+i*braeMY);
//}
}
g2.drawLine(braeStartX,braeStartY,braeStartX+i*braeMX,braeStartY-i*braeMY);
}
/*public void moveDog() {
javax.swing.Timer t=new javax.swing.Timer(1000,new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int x=braeMX-60,y=braeMY+130;
x=x+braeMX;
y=y+braeMY;
dog.setBounds(x,y,120,130); }
}
);
t.start();
}*/
public static void main(String args[]) {
Scene3 scene3=new Scene3();
scene3.getContentPane().add(dog);
//scene3.setBounds(0, 0, 900, 650);
javax.swing.Timer t=new javax.swing.Timer(1000,new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
x=braeStartX-60;
y=braeStartY-133;
int n=0;
int count=0;
/*if(count%2==0) {
n=n+5;
}
else {
n=n-3;
}*/
x=x+50;
y=y-50;
//count++;
/*if(count==7) {
x=braeStartX-60;
y=braeStartY-150;
}*/
dog.setBounds(x,y,120,130); }
}
);
t.start();
//scene3.getContentPane().setBackground(Color.white);
}
}
以上是源程序,在这里先谢谢各位拉。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货