-
Hi, need help, completely confused....
Hi, I'm trying to work with threads to produce an animation that paints an oval onto the screen in 2 alternate places. One on the screen at a time. The thing I am having trouble with is using a mouse listener to change the sleep time of the threads. I want to use the left mouse to reduce the delay, and the right to increase the delay. This is my code so far...
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class AnimApp extends JComponent implements Runnable {
Image[] images = new Image[2];
int frame = 0;
int x = 100;
int y = 100;
int x_con = x;
int delay;
public void paint(Graphics g) {
g.fillOval(x, y,10,10);
}
public void run() {
try {
while (true) {
if (x == x_con) {
x=(x_con/2);
}else if (x == x_con/2) {
x=x+(x_con/2);
}
// Causes the paint() method to be called
repaint();
// Wait
Thread.sleep(delay);
}
} catch (Exception e) {
}
}
public void test() {
AnimApp app = new AnimApp();
JFrame frame = new JFrame();
frame.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("Yes");
//Get a new animation thread and start
delay = delay+500;
}//
}//end new MouseAdapter
)
frame.add(app);
frame.setSize(300, 300);
frame.setTitle("Hello");
frame.setVisible(true);
(new Thread(app)).start();
}
}
The method test calls the class, I cant for some reason get it to work if change it to a constructor. I'm just completely lost.. HELP!!
Steve
-
Here is my fixup, among other thing I use the getContentPane() method for
adding components to the JFrame, and I set the delay to an initial value (not zero).
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AnimApp
extends JComponent
implements Runnable {
Image[] images = new Image[2];
int frame = 0;
int x = 100;
int y = 100;
int x_con = x;
int delay=300;
Thread runner=null;
public AnimApp() {
JFrame frame = new JFrame();
//frame.setLayout(new GridLayout());
frame.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
delay +=100;
if (delay < 100) delay=100;
} else {
delay -= 100;
}
} //
} //end new MouseAdapter
);
frame.getContentPane().add(this);
frame.setSize(300, 300);
frame.setTitle("Hello");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (runner!=null && runner.isAlive())
runner.interrupt();
}
});
runner=new Thread(this);
runner.start();
}
public void paint(Graphics g) {
g.fillOval(x, y, 10, 10);
}
public void run() {
try {
while (true) {
if (x == x_con) {
x = (x_con / 2);
}
else if (x == x_con / 2) {
x = x + (x_con / 2);
}
// Causes the paint() method to be called
repaint();
// Wait
Thread.sleep(delay);
}
}
catch (Exception e) {
}
}
public static void main(String [] args) {
AnimApp app = new AnimApp();
}
}
eschew obfuscation
-
 Originally Posted by sjalle
Here is my fixup, among other thing I use the getContentPane() method for
adding components to the JFrame, and I set the delay to an initial value (not zero).
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AnimApp
extends JComponent
implements Runnable {
Image[] images = new Image[2];
int frame = 0;
int x = 100;
int y = 100;
int x_con = x;
int delay=300;
Thread runner=null;
public AnimApp() {
JFrame frame = new JFrame();
//frame.setLayout(new GridLayout());
frame.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
delay +=100;
if (delay < 100) delay=100;
} else {
delay -= 100;
}
} //
} //end new MouseAdapter
);
frame.getContentPane().add(this);
frame.setSize(300, 300);
frame.setTitle("Hello");
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (runner!=null && runner.isAlive())
runner.interrupt();
}
});
runner=new Thread(this);
runner.start();
}
public void paint(Graphics g) {
g.fillOval(x, y, 10, 10);
}
public void run() {
try {
while (true) {
if (x == x_con) {
x = (x_con / 2);
}
else if (x == x_con / 2) {
x = x + (x_con / 2);
}
// Causes the paint() method to be called
repaint();
// Wait
Thread.sleep(delay);
}
}
catch (Exception e) {
}
}
public static void main(String [] args) {
AnimApp app = new AnimApp();
}
}
Thanks very much that is brilliant. I've been so stuck for ages!!!!
Steve
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks