Reputation:
This program when run shows a oval at location 70,70 and there is a start button. After i click the start button the program stops for some time and the oval moves one position southeast . It actually should have rolled down towards the other corner.
This is the program....
package javaapplication1.pkg161;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Player {
int x = 70;
int y = 70;
static JFrame f;
public static void main(String args[]) {
Player p = new Player();
p.go();
}
public void go() {
f = new JFrame("title");
f.setSize(200, 200);
f.setVisible(true);
Window win = new Window();
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 130; i++) {
win.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
System.out.println("error");
}
}
}
});
}
class Window extends JPanel {
public void paintComponent(Graphics g) {
x++;
y++;
g.fillOval(x, y, 100, 100);
}
}
}
Upvotes: 1
Views: 46
Reputation: 347184
Swing is a single threaded framework, this means you can't perform long running operations within the context of the Event Dispatching Thread, otherwise you will prevent from processing the Event Queue and your program will hang.
Start by taking a look at Concurrency in Swing and How to use Swing Timers
You're also violating the paint chain contract. Take a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting is done in Swing
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Player {
int x = 70;
int y = 70;
private Timer timer;
Window win = new Window();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Player p = new Player();
p.go();
}
});
}
public void go() {
JFrame f = new JFrame("title");
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
timer = new Timer(40, new ActionListener() {
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (counter < 130) {
counter++;
win.updateLocation();
win.repaint();
} else {
timer.stop();
counter = 0;
}
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!timer.isRunning()) {
timer.start();
}
}
});
}
class Window extends JPanel {
private int x = 70;
private int y = 70;
public void updateLocation() {
x++;
y++;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(170+130, 170+130);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 100, 100);
}
}
}
Upvotes: 3