java画点,Java Swing中的图形绘制仅绘制点

I'm currently working on a program where certain numerical variables, which evolve over time, have their value displayed on each iteration. That works well enough, but now I want to plot a graph that shows their evolution over time.

So, I looked into an example of code for plotting graphs in Swing. My final code looks like this:

public class Populus3 extends JPanel

{

public static void main(String[] args) throws IOException {

final Populus3 pop = new Populus3();

JFrame f = new JFrame(); //where I want to plot the graph

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(new GraphingData());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

frame = new JFrame("Animation Frame"); //where I'm running animation for another element of the program

frame.add(pop, BorderLayout.CENTER);

frame.setSize(graphSize, graphSize);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//insert all sort of things

}

public void paint(Graphics g)

{

super.paint(g);

paintCell(g, 1);

Toolkit.getDefaultToolkit().sync(); // necessary for linux users to draw and animate image correctly

g.dispose();

}

public void actionPerformed(ActionEvent e) {

repaint();

}

@Override

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

for(int i = 0; i < particleType.length; i++)

paintCell(g, i); //a method that draws a small circle for the animation panel

}

public static class GraphingData extends JPanel {

int[] data = {

21, 14, 18, 03, 86, 88, 74, 87, 54, 77,

61, 55, 48, 60, 49, 36, 38, 27, 20, 18

};

final int PAD = 20;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

// Draw ordinate.

g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));

// Draw abcissa.

g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));

double xInc = (double)(w - 2*PAD)/(data.length-1);

double scale = (double)(h - 2*PAD)/getMax();

// Mark data points.

g2.setPaint(Color.red);

for(int i = 0; i < data.length; i++) {

double x = PAD + i*xInc;

double y = h - PAD - scale*data[i];

g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));

}

}

private int getMax() {

int max = -Integer.MAX_VALUE;

for(int i = 0; i < data.length; i++) {

if(data[i] > max)

max = data[i];

}

return max;

}

}

}

Now, the animation panel works just fine. The graph panel, on the other hand...when I run the program, it displays a bunch of red dots, without lines to connect them. What am I doing wrong?

解决方案

In addition to @Hovercraft's helpful suggestions, also consider these other approaches:

Accumulate the points in a GeneralPath that may be rendered as required, for example.

w7N3q.png

Connect the points using repeated calls to drawLine() using a suitable coordinate system, outlined here.

CvG2a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值