help with and animation
hi i m trying to make an animation similar to
http://sstl.cee.uiuc.edu/java/twostory/index.html
i have made a block moving can anybody help me or advice me how to do make some thing similar..
thanks....
package DEMO2;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DEMO2 {
public static void main(String[] args) {
JFrame frame = new demoframe();
frame.show();
}
}
class demoframe extends JFrame {
public demoframe() {
setSize(300, 500);
setTitle("Earthquake");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rigid b = new rigid(canvas);
b.start();
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
class rigid extends Thread {
public rigid(JPanel b) {
box = b;
}
public void draw() {
Graphics g = box.getGraphics();
g.fillRect(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillRect(x, y, XSIZE, YSIZE);
/*int xx; int yy;
for (int j=1; j<10; j++){
xx=j*(x-10); yy=j*(y+5);
g.fillRect(xx,yy,XSIZE, YSIZE);
} */
x += dx;
Dimension d = box.getSize();
if (x < 100) {
x = 100;
dx = -dx;
}
if (x + XSIZE + 100 >= d.width) {
x = d.width - XSIZE- 100;
dx = -dx;
}
g.fillRect(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run() {
try {
draw();
for (int i = 1; i <= 1000; i++) {
move();
sleep(200);
}
} catch (InterruptedException e) {
}
}
private JPanel box;
private static final int XSIZE = 75;
private static final int YSIZE = 5;
private int x = 50;
private int y = 400;
private int dx = 2;
private int dy = 2;
}
[3027 byte] By [
jas999a] at [2007-11-27 8:20:11]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo2Rx extends JFrame {
public Demo2Rx() {
// Initialize components.
final RigidRx controller = new RigidRx(canvas);
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
controller.start();
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//canvas.setVisible(false);
System.exit(0);
}
});
// Assemble.
Container contentPane = getContentPane();
contentPane.add(canvas, "Center");
contentPane.add(p, "South");
// Configure JFrame.
setSize(300, 500);
setTitle("Earthquake");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
JPanel canvas = new JPanel() {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
}
};
public static void main(String[] args) {
new Demo2Rx();
}
}
class RigidRx extends Thread {
public RigidRx(JPanel b) {
box = b;
}
public void draw() {
Graphics g = box.getGraphics();
g.fillRect(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillRect(x, y, XSIZE, YSIZE);
// Let the animation increment a member variable
// which you can use to move things around.
// All the action is in the run method. The rest
// is state for this animation class.
int xx=count*(x-10);
int yy=count*(y+5);
g.fillRect(xx,yy,XSIZE, YSIZE);
x += dx;
Dimension d = box.getSize();
if (x < 100) {
x = 100;
dx = -dx;
}
if (x + XSIZE + 100 >= d.width) {
x = d.width - XSIZE- 100;
dx = -dx;
}
g.fillRect(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run() {
for (int i = 1; i <= 1000; i++) {
count = i;
move();
draw();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}
}
private JPanel box;
private static final int XSIZE = 75;
private static final int YSIZE = 5;
private int x = 50;
private int y = 400;
private int dx = 2;
private int dy = 2;
int count = 0;
}
thanks
.. there is a new problem when i run this program.. its meshed animation..
can u plzze advice
package DEMO2;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DEMO2 {
public static void main(String[] args) {
JFrame frame = new demoframe();
frame.show();
}
}
class demoframe extends JFrame {
public demoframe() {
setSize(300, 500);
setTitle("Earthquake");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rigid b = new rigid(canvas);
b.start();
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
class rigid extends Thread {
public rigid(JPanel b) {
building = b;
}
public void draw() {
int x1, x2, y1, y2;
x1=x+20; x2=x+XSIZE-20; y1=y-(YSIZE); y2=y;
Graphics g = building.getGraphics();
g.fillRect(x, y, XSIZE, YSIZE);
for(int i=1; i<10; i++ ){
x1=x+70; x2=x1+XSIZE-30; y1=y-(i*YSIZE); y2=y;
{
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
}
}
public void move(){
if (!building.isVisible())
return;
Graphics g = building.getGraphics();
g.setXORMode(building.getBackground());
{
g.fillRect(x, y, XSIZE, YSIZE);
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
x1=x+20; x2=x+XSIZE-20; y1=y-(YSIZE); y2=y;
x += dx;
Dimension d = building.getSize();
if (x < 100) {
x = 100;
dx = -dx;
}
if (x + XSIZE + 100 >= d.width) {
x = d.width - XSIZE- 100;
dx = -dx;
}
{
g.fillRect(x, y, XSIZE, YSIZE);
}
{
for(int i=1; i<10; i++ ){
x1=x+20; x2=x+XSIZE-20; y1=y-(i*YSIZE); y2=y;
{
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
}
}
}
public void run() {
try {
draw();
for (int i = 1; i <= 100; i++) {
move();
sleep(200);
}
} catch (InterruptedException e) {
}
}
private JPanel building;
private static final int XSIZE = 75;
private static final int YSIZE = 5;
private int x = 50;
private int y = 400;
private int dx = 2;
private int x1, x2, y1, y2;
}
Don't forget to use the "Code Formatting Tags",see http://forum.java.sun.com/help.jspa?sec=formatting,so the posted code retains its original formatting.
thanks for advice.. code
package DEMO2;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DEMO2 {
public static void main(String[] args) {
JFrame frame = new demoframe();
frame.show();
}
}
class demoframe extends JFrame {
public demoframe() {
setSize(300, 500);
setTitle("Earthquake");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rigid b = new rigid(canvas);
b.start();
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}
private JPanel canvas;
}
class rigid extends Thread {
public rigid(JPanel b) {
building = b;
}
public void draw() {
int x1, x2, y1, y2;
x1=x+20; x2=x+XSIZE-20; y1=y-(YSIZE); y2=y;
Graphics g = building.getGraphics();
g.fillRect(x, y, XSIZE, YSIZE);
for(int i=1; i<10; i++ ){
x1=x+70; x2=x1+XSIZE-30; y1=y-(i*YSIZE); y2=y;
{
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
}
}
public void move(){
if (!building.isVisible())
return;
Graphics g = building.getGraphics();
g.setXORMode(building.getBackground());
{
g.fillRect(x, y, XSIZE, YSIZE);
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
x1=x+20; x2=x+XSIZE-20; y1=y-(YSIZE); y2=y;
x += dx;
Dimension d = building.getSize();
if (x < 100) {
x = 100;
dx = -dx;
}
if (x + XSIZE + 100 >= d.width) {
x = d.width - XSIZE- 100;
dx = -dx;
}
{
g.fillRect(x, y, XSIZE, YSIZE);
}
{
for(int i=1; i<10; i++ ){
x1=x+20; x2=x+XSIZE-20; y1=y-(i*YSIZE); y2=y;
{
g.drawLine(x1, y1, x1, y2);
g.drawLine(x2, y1, x2, y2);
g.drawLine(x1, y1, x2, y1);
}
}
g.dispose();//
}
}
public void run() {
try {
draw();
for (int i = 1; i <= 100; i++) {
move();
sleep(200);
}
} catch (InterruptedException e) {
}
}
private JPanel building;
private static final int XSIZE = 75;
private static final int YSIZE = 5;
private int x = 50;
private int y = 400;
private int dx = 2;
private int x1, x2, y1, y2;
}