public class XXX{
public static void main(String[] args){
new XXX();}
private JFrame window;
private JPanel panel;
private JTextField inputField;
private int cnt;
public XXX(){
window = new JFrame();
window.setBounds(100,100,400,200);
panel = new JPanel();
window.add(panel);
panel.setLayout(new BorderLayout());
panel.add(drawPanel,BorderLayout.CENTER);
inputField = new JTextField("5");
panel.add(inputField,BorderLayout.SOUTH);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
window.setVisible(true);
}
});
Thread animationThread = new Thread(new Runnable(){
public void run(){
while(true){
try{
cnt += Integer.parseInt(inputField.getText());
inputField.setForeground(Color.BLACK);
}
catch(NumberFormatException e){
inputField.setForeground(Color.RED);
}
drawPanel.repaint();
if(cnt > panel.getSize().width){
cnt = 0;
}
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
});
animationThread.start();
}
private JPanel drawPanel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(cnt,50,50,50);
}
};
}