nowli 2014年02月08日
关于Graphics2D画动态曲线
下面的代码在内部类中可以动态画曲线,为什么在外部类启动线程可以获取数据,但画动态曲线的时候数据确为空?哪位可以出手相助?



import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.*;

public class Test extends JFrame implements ActionListener {
JButton button=new JButton("启动");
JButton button1=new JButton("外部类启动");
public static void main(String[] args) {
// TODO Auto-generated method stub
Runnable tr=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new Test();
}
};
javax.swing.SwingUtilities.invokeLater(tr);
}
public Test()
{
super("测试窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
Container c=this.getContentPane();
GridBagLayout layout=new GridBagLayout();
this.setLayout(layout);
GridBagConstraints g=new GridBagConstraints();
cp=new CurvePanel();
c.add(cp);
g.gridx=0;
g.gridy=0;
g.gridwidth=8;
g.gridheight=9;
g.weightx=0.8;
g.weighty=0.9;
g.fill=GridBagConstraints.BOTH;
layout.setConstraints(cp, g);

c.add(button);
g.gridx=0;
g.gridy=9;
g.gridwidth=1;
g.gridheight=1;
g.weightx=0.1;
g.weighty=0.1;
g.fill=GridBagConstraints.NONE;
layout.setConstraints(button, g);
button.addActionListener(this);

c.add(button1);
g.gridx=1;
g.gridy=9;
g.gridwidth=1;
g.gridheight=1;
g.weightx=0.1;
g.weighty=0.1;
g.fill=GridBagConstraints.NONE;
layout.setConstraints(button1, g);
button1.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object obj=e.getSource();
if(obj.equals(button))
{
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
cp.setDrawLine();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}else
if(obj.equals(button1))
{
StartThread st=new StartThread();
new Thread(st).start();
}
}
private CurvePanel cp;
}
class StartThread implements Runnable{
CurvePanel cp=new CurvePanel();
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
cp.setDrawLine();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
//画曲线
class CurvePanel extends JPanel{
List<Integer> values;
public CurvePanel()
{
values=Collections.synchronizedList(new ArrayList<Integer>());
}
public void addValue(int value)
{
values.add(value);
System.out.println(values);
}
public void setDrawLine()
{
Random rand = new Random();
addValue(rand.nextInt(400));
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
for(int i=0;i<values.size()-1;i++)
{
g2d.setColor(Color.red);
g2d.drawLine(5*i, values.get(i), 5*(i+1), values.get(i+1));
}
}
}
...全文
265 点赞 收藏 3
写回复
3 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Eclipse
创建于2007-09-28

3.1w+

社区成员

4.9w+

社区内容

Java Eclipse
社区公告
暂无公告