java中的方法可以无限的被反复调用,问一个很菜鸟的问题!为什么paint被无限调用?...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这是另一个小程序 也有同样的问题

package fx;

import java.awt.*;

import java.util.*;

import javax.swing.*;

public class clock extends JFrame implements Runnable{

Mp mn=null;

int r=180,x,x1,y,y1,kd,xs,ys,xm,ym,xh,yh,xz,yz;

Calendar now=new GregorianCalendar();

Thread clock;

public clock(){

super("时钟");

mn=new Mp();

this.setSize(410, 440);

this.add(mn);

this.setLocationRelativeTo(null);

this.setVisible(true);

this.setDefaultCloseOperation(3);

if(clock==null)

{

clock=new Thread(this);

clock.start();

}

}

public static void main(String[] args) {

new clock();

}

class Mp extends JPanel{

public void paint(Graphics g) {

super.paint(g);

Dimension d=this.getSize();

g.setColor(Color.white);

g.fillRect(0, 0, d.width, d.height);

Calendar now=new GregorianCalendar();

int h=now.get(Calendar.HOUR_OF_DAY);

int m=now.get(Calendar.MINUTE);

int s=now.get(Calendar.SECOND);

g.setColor(Color.DARK_GRAY);

((Graphics2D)g).setStroke(new BasicStroke(6.0f));

g.drawOval(20, 20, r*2, r*2);

g.setColor(Color.black);

//画表面

for(double i=0;i<60;i++){

if(i%5==0){

kd=15;

((Graphics2D)g).setStroke(new BasicStroke(3.0f));

}

else

{

kd=10;

((Graphics2D)g).setStroke(new BasicStroke(1.5f));

}

double a=Math.PI/30*i;

y=r-(int) Math.rint(Math.sin(a)*(r-kd))+20;

y1=r-(int) Math.rint(Math.sin(a)*(r-6))+20;

x=(int) Math.rint(Math.cos(a)*(r-kd))+r+20;

x1=(int) Math.rint(Math.cos(a)*(r-6))+r+20;

g.drawLine(x, y, x1, y1);

}

//数字

setFont(new Font("Times New Roman",Font.BOLD,30));

g.setColor(new Color(123,123,123));

for(int i=1;i<13;i++){

yz=r-(int) Math.rint(Math.cos(Math.PI/6*i)*(r-42))+20+10;

if(i<10){

xz=(int) Math.rint(Math.sin(Math.PI/6*i)*(r-42))+r+20-10;

}

else{

xz=(int) Math.rint(Math.sin(Math.PI/6*i)*(r-35))+r+20-15;

}

g.drawString(i+"", xz, yz);

}

//秒针

g.setColor(Color.red);

((Graphics2D)g).setStroke(new BasicStroke(1.5f));

xs=(int) Math.rint(Math.sin(Math.PI/30*s)*(r-25))+r+20;

ys=r-(int) Math.rint(Math.cos(Math.PI/30*s)*(r-25))+20;

g.drawLine(r+20, r+20, xs, ys);

((Graphics2D)g).setStroke(new BasicStroke(3.0f));

xs=(int) Math.rint(Math.sin(Math.PI/30*(s+30))*(r-155))+20+r;

ys=r-(int) Math.rint(Math.cos(Math.PI/30*(s+30))*(r-155))+20;

g.drawLine(r+20, r+20, xs, ys);

//分针

g.setColor(Color.black);

((Graphics2D)g).setStroke(new BasicStroke(3.0f));

xm=(int) Math.rint(Math.sin(Math.PI/30*m)*(r-38))+r+20;

ym=r-(int) Math.rint(Math.cos(Math.PI/30*m)*(r-38))+20;

g.drawLine(r+20, r+20, xm, ym);

//时针

g.setColor(Color.black);

((Graphics2D)g).setStroke(new BasicStroke(6.0f));

if(h>=12)h=h-12;

xh=(int) Math.rint(Math.sin(Math.PI/6*h)*(r-95))+r+20;

yh=r-(int) Math.rint(Math.cos(Math.PI/6*h)*(r-95))+20;

g.drawLine(r+20, r+20, xh, yh);

//中间的圆

g.setColor(Color.red);

g.fillOval(r+20-6, r+20-6, 12, 12);

System.out.println(s);

}

}

public void run() {

while(true)

{

this.repaint();

try

{

Thread.sleep(1000);

}

catch(InterruptedException e)

{

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值