0
데이터 포인트가 들어올 때 들어오는 데이터 포인트를 반복하고 JPanel에 그래프를 그리는 방법을 알아 내려고하고 있습니다. 시작하기 위해 List of DataPoints를 호출하고 각 행을 그립니다 (다시 칠하기). 나는자바 스윙 목록을 반복하고 각 행을 그려보십시오.
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyPanel extends JPanel{
protected int x1 = 0;
protected int x2 = 0;
protected int y1 = 0;
protected int y2 = 0;
public MyPanel(){
iteratePoints();
}
/**
* @param args
*/
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(new MyPanel());
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
System.out.println("In print");
g.drawLine(x1, y1, x2, y2);
};
private void iteratePoints(){
System.out.println("iteratepoints");
ArrayList<DataGrid> dataList = new ArrayList<DataGrid>();
dataList.add(new DataGrid(0,4));
dataList.add(new DataGrid(5,14));
dataList.add(new DataGrid(10,1));
dataList.add(new DataGrid(15,10));
dataList.add(new DataGrid(20,14));
dataList.add(new DataGrid(25,1));
for(int i = 0; i<dataList.size(); i++)
{
x2 = dataList.get(i).x;
y2 = dataList.get(i).y;
this.repaint();
x1 = x2;
y1 = y2;
}
}
private class DataGrid
{
public int x, y;
DataGrid(int x, int y)
{
this.x = x;
this.y = y;
}
}
}
목록을 x 및 y 변수 대신 클래스의 필드로 지정하십시오. 당신의 paintComponent 메쏘드에서, 줄을 반복하고 각각을 익사 시키십시오. –
문제는 결국 포인트가 아이 추적기 (모든 프레임)에서 나올 것입니다. 그래서 나는 그들이 들어올 때 각 라인을 그리는 방법이 필요하다. 각 라인을 List에 추가하고 그것을 paintComponent에서 반복한다면, 전체 프레임은 전체리스트를 반복하고 다시 그려야 할 것이다. 이것이 최선의 방법인가요? – jbg2408
충분할 수 있습니다. Camickr이 대답 할 때 BufferedImage에 페인트 할 수도 있습니다. –