현재 2D 공간에서 움직이고 상호 작용하는 다양한 객체가있는 프로그램에 애니메이션을 추가하려고합니다. 나는 타이머가 작동하는 곳으로 마침내 갔고, 프로그램은 객체가 움직이고 원하는 방식으로 상호 작용할 수 있습니다 ...하지만 열리는 디스플레이 패널은 좌절하게 남아 있습니다. 나는 온라인으로 볼 예제에서 코드를 사용하려고했습니다,하지만 난 여전히 뭔가가 있어야합니다 ... 궁극적으로자바 스윙이 표시되지 않습니다.
public class Populus2 extends JPanel
{
/**
* @param args
*/
static float[] xCoordinates;
static float[] yCoordinates;
static int duration;
static int iteration = 1;
static int graphSize = 500;
..............
static JFrame frame;
static JLabel lbl;
JPanel panel;
static Timer timer;
public static void main(String[] args) throws IOException {
final Populus2 pop = new Populus2();
frame = new JFrame("Animation Frame");
lbl = new JLabel();
Panel panel = new Panel();
panel.add(lbl);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(graphSize, graphSize);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener timeStep = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("at time " + iteration);
spendTime();
pop.repaint();
if(iteration>duration)
timer.stop();
}
};
timer = new Timer(100, timeStep);
timer.setInitialDelay(0);
timer.start();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
System.out.println("******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n******PAINT******\n");
Graphics2D g2d = (Graphics2D) g;
paintCell(g, 1); //eventually, I want to call this method for every "cell" in the program
g.drawLine(30, 30, 80, 80); //this line's basically a test to see if I can display anything at all
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void paintCell(Graphics graphics, int cellNumber)
{
graphics.setColor(Color.black);
graphics.fillOval((int)(graphSize*xCoordinates[cellNumber]/size), (int)(graphSize*yCoordinates[cellNumber]/size), 5, 5);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < types.length; i++)
paintCell(g, i);
}
public static void spendTime()
{
//advances the iteration counter and recalculates the coordinates of all the cells
}
}
, 나는 프로그램이 타이머의 각 반복에 대한 paintCell()를 호출 할 시뮬레이션에서 각 셀은 움직이는 물체를 나타냅니다. 지금은 화면이 비어 있습니다. System.out.println()을 통해 paint()를 호출 한 메시지는 레코드에 대해 표시되지 않습니다. 생각 하시겠습니까?
타이머 내에서 repaint()를 호출합니까? – jonhopkins
음, 나는 timeStep ActonListener 내에서 pop.repaint()를 호출합니다. – user1209014