plotDim=plotPanel.size(); outDim=outPanel.size(); buttonDim=buttonPanel.size(); int x0=(int)(plotDim.width*0.05); int y0=(int)(plotDim.height*0.95); result1TextArea.setBounds((int)(outDim.width*0.01),(int)(outDim.height*0.01),(int)(outDim.width*0.99),(int)(outDim.height*0.80)); result1TextArea.setEditable(false); outPanel.add(result1TextArea,null);
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class plot extends Applet implements Runnable,ActionListener{
boolean isReset=true,isRun=true,isStop=false;
Panel plotPanel=new Panel();
Panel outPanel=new Panel();
Panel buttonPanel=new Panel();
TextArea result1TextArea=new TextArea();
Button runButton=new Button("重新开始");
Button resetButton=new Button("彻底清除");
Dimension appletDim,plotDim,outDim,buttonDim;
Image bufferImage=null;
Graphics bufferGraphics,plotGraphics;
Thread animThread=null;
int x0;
int y0;
public void init(){
appletDim=this.size();
this.setLayout(null);
this.setBackground(Color.lightGray);
plotPanel.setLayout(null);
outPanel.setLayout(null);
buttonPanel.setLayout(null); plotPanel.setBounds((int)(appletDim.width*0.01),(int)(appletDim.height*0.01),(int)(appletDim.width*0.80),(int)(appletDim.height*0.60));
buttonPanel.setBounds((int)(appletDim.width*0.86),(int)(appletDim.height*0.01),(int)(appletDim.width*0.12),(int)(appletDim.height*0.60));
outPanel.setBounds((int)(appletDim.width*0.01),(int)(appletDim.height*0.61),(int)(appletDim.width*0.98),(int)(appletDim.height*0.38)); plotPanel.setBackground(Color.yellow);
outPanel.setBackground(Color.lightGray);
buttonPanel.setBackground(Color.lightGray);
plotDim=plotPanel.size();
outDim=outPanel.size();
buttonDim=buttonPanel.size();
int x0=(int)(plotDim.width*0.05);
int y0=(int)(plotDim.height*0.95);
result1TextArea.setBounds((int)(outDim.width*0.01),(int)(outDim.height*0.01),(int)(outDim.width*0.99),(int)(outDim.height*0.80));
result1TextArea.setEditable(false);
outPanel.add(result1TextArea,null);
runButton.setBounds((int)(buttonDim.width*0.01),(int)(buttonDim.height*0.01),(int)(buttonDim.width*0.8),(int)(buttonDim.height*0.1));
buttonPanel.add(runButton);
resetButton.setBounds((int)(buttonDim.width*0.01),(int)(buttonDim.height*0.15),(int)(buttonDim.width*0.8),(int)(buttonDim.height*0.1));
buttonPanel.add(resetButton);
runButton.addActionListener(this);
resetButton.addActionListener(this);
this.add(plotPanel,null);
this.add(outPanel,null);
this.add(buttonPanel,null);
this.show();
plotGraphics=plotPanel.getGraphics();
bufferImage=createImage(plotDim.width,plotDim.height);
bufferGraphics=bufferImage.getGraphics();
bufferGraphics.setColor(Color.yellow);
bufferGraphics.fillRect(0,0,plotDim.width,plotDim.height);
} public void stop() {
animThread = null;
}
public void run(){
try{
while(true){
int[] value=new int[31];
int total=0;
int N=30;
int yy;
int xx;
String str_tmp; bufferGraphics.clearRect(0,0,plotDim.width,plotDim.height);
bufferGraphics.setColor(Color.yellow);
bufferGraphics.fillRect(0,0,plotDim.width,plotDim.height); for(int i = 0; i <10; i++ ) {
animThread.sleep(5);
yy = (int)((plotDim.height*0.9*i) / 10);
bufferGraphics.drawLine(x0, y0-yy, (int)(x0+plotDim.width*0.9), y0-yy);
} for(int i = 0; i < 30; i++ ) {
animThread.sleep(5);
xx = (int)((plotDim.width*0.9* i) / 16);
bufferGraphics.drawLine(x0, y0, x0,(int)(y0-plotDim.height*0.9));
bufferGraphics.drawLine(x0+xx, y0, x0+xx, (int)(y0-plotDim.height*0.9));
}
for(int i = 0; i < 10; i = i+5 ) {
animThread.sleep(5);
yy = (int)((plotDim.height*0.9* i) / 10);
bufferGraphics.drawLine(x0, y0, (int)(x0+plotDim.width*0.9), y0);
bufferGraphics.drawLine(x0, y0-yy,(int)(x0+plotDim.width*0.95), y0-yy);
str_tmp = Float.toString((float)(i));
bufferGraphics.drawString(str_tmp, x0-20, y0-(int)((plotDim.height*0.9*i) / 10));
}
for(int i = 0; i < 30; i = i+6) {
animThread.sleep(5);
xx = (int)((plotDim.width*0.9* i) /30);
bufferGraphics.drawLine(x0, y0, x0, (int)(y0 - plotDim.height*0.9));
bufferGraphics.drawLine(x0+xx, y0, x0+xx, (int)(y0-plotDim.height*0.9));
str_tmp = Float.toString((float)(i));
bufferGraphics.drawString(str_tmp, (int)(x0+plotDim.width*0.9* i/30) , y0+20);
}
str_tmp = "数值(单位)";
bufferGraphics.drawString(str_tmp, x0-5, (int)(y0-plotDim.height*0.9-5));
str_tmp = "试验(次数)";
bufferGraphics.drawString(str_tmp,(int)(x0+plotDim.width*0.9+5), y0);
for(int i=0;i<N;i++)
value[i]=(int)(doubleRandom((double)(plotDim.height*0.05),(double)(plotDim.height*0.95)));
for(int i=0;i<N;i++){
bufferGraphics.setColor(Color.blue);
bufferGraphics.drawLine((int)((plotDim.width*0.9* i)/30),value[i],(int)((plotDim.width*0.9* (i+1))/30),value[i+1]);
}
repaint();
}
}catch(Exception e ){
e.printStackTrace();
}
}
public void update(Graphics g){
paint(g);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==runButton){
if(isRun==true){
isRun=false;
isReset=false;
animThread=new Thread(this);
animThread.start();
}
}
if(e.getSource()==resetButton){
if(isReset==false){
if(isStop==true){
animThread.resume();
isStop=false;
}
isRun=true;
isReset=true;
animThread.stop();
animThread=null;
}
}
}
public void paint(Graphics g){
g.clearRect(0,0,appletDim.width-1,appletDim.height-1);
plotGraphics.drawImage(bufferImage,0,0,this);
}
public static double doubleRandom(double start,double end){
double hpp;
hpp=(end-start)*Math.random()+start;
return hpp;
}
}
生命周期中只调用一次,而后来,浏览器绘制图形时(包括你改变大小,最小窗口有还原)调用了paint(Graphics g)方法,所以应该重载paint(Graphics g)方法,如果屏幕闪烁,应该考虑用双缓冲技术.
paint(g);
}
这里无需这样
只要调用repaint()就行了。
public void paint(Graphics g){
g.clearRect(0,0,appletDim.width-1,appletDim.height-1);
plotGraphics.drawImage(bufferImage,0,0,this);
}
这里都用g来画图
不必调plotGraphics
public void paint(Graphics g){
int[] value=new int[31];
int total=0;
int N=30;
int yy;
int xx;
String str_tmp; bufferGraphics.clearRect(0,0,plotDim.width,plotDim.height);
bufferGraphics.setColor(Color.yellow);
bufferGraphics.fillRect(0,0,plotDim.width,plotDim.height); for(int i = 0; i <10; i++ ) {
yy = (int)((plotDim.height*0.9*i) / 10);
bufferGraphics.drawLine(x0, y0-yy, (int)(x0+plotDim.width*0.9), y0-yy);
} for(int i = 0; i < 30; i++ ) {
xx = (int)((plotDim.width*0.9* i) / 30);
bufferGraphics.drawLine(x0, y0, x0,(int)(y0-plotDim.height*0.9));
bufferGraphics.drawLine(x0+xx, y0, x0+xx, (int)(y0-plotDim.height*0.9));
}
for(int i = 0; i < 10; i = i+5 ) {
yy = (int)((plotDim.height*0.9* i) / 10);
bufferGraphics.drawLine(x0, y0, (int)(x0+plotDim.width*0.9), y0);
bufferGraphics.drawLine(x0, y0-yy,(int)(x0+plotDim.width*0.95), y0-yy);
str_tmp = Float.toString((float)(i));
bufferGraphics.drawString(str_tmp, x0-20, y0-(int)((plotDim.height*0.9*i) / 10));
}
for(int i = 0; i < 30; i = i+6) {
xx = (int)((plotDim.width*0.9* i) /30);
bufferGraphics.drawLine(x0, y0, x0, (int)(y0 - plotDim.height*0.9));
bufferGraphics.drawLine(x0+xx, y0, x0+xx, (int)(y0-plotDim.height*0.9));
str_tmp = Float.toString((float)(i));
bufferGraphics.drawString(str_tmp, (int)(x0+plotDim.width*0.9* i/30) , y0+20);
}
str_tmp = "数值(单位)";
bufferGraphics.drawString(str_tmp, x0-5, (int)(y0-plotDim.height*0.9-5));
str_tmp = "试验(次数)";
bufferGraphics.drawString(str_tmp,(int)(x0+plotDim.width*0.9+5), y0);
for(int i=0;i<N;i++)
value[i]=(int)(doubleRandom((double)(plotDim.height*0.05),(double)(plotDim.height*0.95)));
for(int i=0;i<N;i++){
bufferGraphics.setColor(Color.blue);
bufferGraphics.drawLine((int)((plotDim.width*0.9* i)/30),value[i],(int)((plotDim.width*0.9* (i+1))/30),value[i+1]);
}
g.clearRect(0,0,appletDim.width-1,appletDim.height-1);
plotGraphics.drawImage(bufferImage,0,0,this);
}
str_tmp = "数值(单位)";
// 这里是在 (0, -113) 打印的文字,当然显示不出来
bufferGraphics.drawString(str_tmp, x0-5, (int)(y0-plotDim.height*0.9-5));
str_tmp = "试验(次数)";
// 这里算出来的坐标似乎也是一个临界值,我看到只能画出来一点点
bufferGraphics.drawString(str_tmp,(int)(x0+plotDim.width*0.9+5), y0);这些语句前面加一句 bufferGraphics.setColor(Color.BLUE);因为如果不加这句,画文本是用的背景色画的,当然看不出来。建议你把后面循环里的那个设置颜色的语句搬到这前面去。