当前位置:编程学习 > JAVA >>

java编程实现一个正方形放大缩小的代码

求java编程实现一个正方形放大缩小的代码, 能实现时间间隔内循环放大缩小最好。 比如间隔1S放大/缩小。 拜托!
补充:不需要一定是正方形了。 随便个图形。 圆形。 三角形都行。。
		
追问:

我编译的时候有个错误啊。 麻烦再帮我看一下。   说是什么变量要声明

答案:①部分截图:

 

 

 

②源程序:

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//实现矩形在规定时间间隔里循环放大缩小
public class Test02 extends JFrame implements Runnable{
 static int w = 0,h = 0; //w:矩形宽度,h:矩形高度 
 boolean flag = false;//false:放大,true:缩小
 public Test02(){  
  this.setSize (500,500);
  this.setVisible (true);
  this.setTitle (w+","+h);
  this.setLocationRelativeTo (this);
  this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 } 
 public void paint(Graphics g){
  int width = getWidth();  //窗口宽度
  int height = getWidth();  //窗口高度
  g.setColor (Color.WHITE); //设置画笔颜色
  g.fillRect (0,0,width,height);  //填充窗口
  int x = (width-w)/2; //x:矩形左上角横坐标
  int y = (height-h)/2; //y:矩形左上角纵坐标
  g.setColor (Color.BLUE); //同上...
  g.drawRect (x,y,w,h);   //画矩形,实心矩形为fillRect(....)
  g.setColor (Color.RED); //同上...
  g.drawOval (x,y,w,h);     //画椭圆
  setTitle (w+","+h);     //以矩形宽和高来设置窗口标题
 }
 public void setSize(){
  float n = getWidth()/getWidth();
  if(w==0||h==0)
   flag = false;
  if(w==getWidth()||h==getWidth())
   flag = true;
  if(!flag){
   w+=1;
   h+=n;
  }
  if(flag){
   w-=1;
   h-=n;
  }  
 }
 public void run(){
  while(true){
   try{
    Thread.sleep (5);//间隔
   }catch(InterruptedException ie){}   
   setSize();
   repaint();
  }
 }
 public static void main(String[] args){
  Test02 t = new Test02();
  Thread th = new Thread(t);
  th.start();
 }
}

import java.awt.Graphics;import javax.swing.JFrame;public class Test extends JFrame {private int c=150;private int r=50;public static void main(String args[]) {try {Test frame = new Test();frame.setVisible(true);for(int i=10;;i++){double tmp=Math.sin(i*0.1);frame.redraw((int) (tmp*tmp*100));Thread.sleep(200);}} catch (Exception e) {e.printStackTrace();}}public Test() {super();setBounds(100, 100, 500, 375);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void paint(Graphics g){super.paint(g);g.drawOval(c-r, c-r, 2*r, 2*r);}public void redraw(int p_r){this.r=p_r;this.repaint();}}

上一个:java是怎样的一种语言?
下一个:JAVA和 .net 有什么主要区别

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,