小弟俺要根据每帧不同的图像即时在canvas上按对应参数画线,
所以就相当于每次将元素个数递增的List <Line>交给canvas让它去drawline,问题是,如何刷新Canvas?现在每次我想看到动态画面, 就得手动不断的resize canvas大小,
不过这样太傻了....求教!!! 谢谢各位
所以就相当于每次将元素个数递增的List <Line>交给canvas让它去drawline,问题是,如何刷新Canvas?现在每次我想看到动态画面, 就得手动不断的resize canvas大小,
不过这样太傻了....求教!!! 谢谢各位
总体而言,就是在其它的类当中产生一个Grid对象,进而不断的调用grid对象中的addNewline函数以达到步进画线的目的grid是主类,用来在canvas上画线
package DrawLine;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Grids extends Frame implements Runnable {
private GridsCanvas xyz;
private int step;
Thread t ;
public Grids(String title, int w, int h, int rows, int cols) {
t = new Thread(this,"histogram");
setTitle(title);
step=50;
xyz = new GridsCanvas(w, h, rows, cols);
add(xyz);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
}
});
pack();
} public void addNewLine(double x, double y, double x2, double y2) {
xyz.addLine(x, y, x2, y2);
}
public void addNewLine(double y, double y2) {
step+=2;
xyz.addLine(step, y, step, y2);
//this.repaint();
}
/*public static void main(String[] a) {
Grids grid = new Grids("Test", 1800, 330, 5, 10);
Thread t = new Thread(grid);
grid.setVisible(true);
int fff = 0;
for (int x = 50; x < 250; x++) {
while (fff < 50000) {
fff++;
}
fff = 0;
grid.addNewLine(x+2, 300, x+2, Math.random() * 100);
}
}*/
public void run() {
}
}GridsCanvas 是画线的面板,里面有update
package DrawLine;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;/**
* Program to draw grids.
*
* @author Ian Darwin, http://www.darwinsys.com/
*/
class GridsCanvas extends Canvas{
int width, height;
int rows;
int cols;
ArrayList<ScrilleysLine> list; GridsCanvas(int w, int h, int r, int c) {
setSize(width = w, height = h);
rows = r;
cols = c;
list = new ArrayList<ScrilleysLine>();
}
public Graphics getCanvasGraphics(){
return this.getGraphics();
}
public void paint(Graphics g) {
int i;
width = getSize().width;
height = getSize().height; // draw the rows
int rowHt = height / (rows);
for(int w = 0; w < list.size(); w++){
ScrilleysLine line = list.get(w);
g.setColor(Color.RED);
g.drawLine((int)line.x, (int)line.y,(int) line.x2,(int) line.y2);
}
}
/*public void update(Graphics g){
for(int w = 0; w < list.size(); w++){
ScrilleysLine line = list.get(w);
g.setColor(Color.RED);
g.drawLine((int)line.x, (int)line.y,(int) line.x2,(int) line.y2);
}
//super.repaint(g);
}*/
public void addLine(double x, double y,double x2, double y2){
ScrilleysLine line = new ScrilleysLine(x,y,x2,y2);
list.add(line);
}
}ScrilleysLine 是自己定义的线类
package DrawLine;
import java.awt.Canvas;
import java.awt.Graphics;
public class ScrilleysLine {
public double x;
public double y;
public double x2;
public double y2;
/**
*
*/
public ScrilleysLine(double x, double y,double x2, double y2) {
this.x = x;
this.y = y;
this.x2 = x2;
this.y2 = y2+100;
}
ScrilleysLine line = new ScrilleysLine(x,y,x2,y2);
list.add(line);
}你这里只是把线添加到List里,并没有通知界面重绘,可以添加repaint()试试