我的这个java程序paint()方法为什么没执行???

2024-12-20 11:57:45
推荐回答(3个)
回答1:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.lang.Math;
import javax.swing.JApplet;

public class MyDrawBlock extends JApplet {
Image pic;
int picW = 0, picH = 0;
int x = 0, y = 0;
int[][] a;
final int cor = 12, row = 8;

public void init() {
// System.out.println("11111111111");
a = new int[cor][row];
pic = getImage(getCodeBase(), "block.gif");
for (int i = 0; i < cor; i++)
{
for (int j = 0; j < row; j++)
{
a[i][j] = (int) (Math.random() * 100) % 2;
System.out.println("11111111111 "+a[i][j]);
try{
repaint();
}catch(Exception e){};
}
}

}

public void paint(Graphics g) {
System.out.println("aaaaaaaaaaaa");
picW=pic.getWidth(this);
picH=pic.getHeight(this);
System.out.println("picW "+picW);
System.out.println("picH "+picH);
for(int i=0;i {
x=20;y=y+picH;
for(int j=0;j {
if(a[i][j]==1)
{
g.setColor(Color.blue);
// g.fill3DRect(x, y, picW, picH,true);
g.fillRect(0, 0, x, y);

}
x=x+picW;
}
//x=x+picW;

// try {
// Thread.sleep(1000);
// repaint();
// } catch (InterruptedException e) {
//
// }
}
}
/*
* public void run() { try { Therad.sleep(1000); }
* catch(InterruptedException ex) }
*/
}

执行了。不过就执行一次。你得在paint方法中添加repaint方法。
import java.awt.*;
import java.applet.*;

public class Time3 extends Applet {
// public class Time3 extends javax.swing.JApplet {
/**
*
*/

private int time = 1;

public void init() {
setBackground(Color.black);
}

public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen;
screen2D.setColor(Color.white);
screen2D.drawString(Integer.toString(time), 5, 25);
try {
Thread.sleep(1000);
time++;
repaint();
} catch (InterruptedException e) {

}
}

}这个是参程序。也一次看看吧。

回答2:

执行了。

你的图片在吗?否则那个picW等于0,也就啥也看不到了

回答3:

朋友你发现你的这个程序里是不是没有main方法,加main方法就可以了