animation

hi i m try to make a structure moving i was able to make part of it can somebody help please...

thanks in advance

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;

publicclass DEMO2{

publicstaticvoid main(String[] args){

JFrame frame =new demoframe();

frame.show();

}

}

class demoframeextends JFrame{

public demoframe(){

setSize(300, 500);

setTitle("Earthquake");

addWindowListener(new WindowAdapter(){

publicvoid 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(){

publicvoid actionPerformed(ActionEvent evt){

rigid b =new rigid(canvas);

b.start();

}

});

addButton(p,"Close",new ActionListener(){

publicvoid actionPerformed(ActionEvent evt){

canvas.setVisible(false);

System.exit(0);

}

});

contentPane.add(p,"South");

}

publicvoid addButton(Container c, String title, ActionListener a){

JButton b =new JButton(title);

c.add(b);

b.addActionListener(a);

}

private JPanel canvas;

}

class rigidextends Thread{

public rigid(JPanel b){

building = b;

}

publicvoid draw(){

Graphics g = building.getGraphics();

g.fillRect(x, y, XSIZE, YSIZE);

for(int i=1; i<(fl+1); 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);

}

}

publicvoid move(){

if (!building.isVisible())

return;

Graphics g = building.getGraphics();

g.setXORMode(building.getBackground());

g.fillRect(x, y, XSIZE, YSIZE);

for(int i=1; i<(fl+1); 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);

}

x += dx;

Dimension d = building.getSize();

if (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<(fl+1); 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);

}

}}

publicvoid run(){

try{

draw();

for (int i = 1; i <= 1000; i++){

move();

sleep(200);

}

}

catch (InterruptedException e){}

}

private JPanel building;

privatestaticfinalint XSIZE = 75;

privatestaticfinalint YSIZE = 25;

privateint x = 99;

privateint y = 400;

privateint dx = 2;

privateint fl = 10;

privateint x1, x2, y1, y2;

}

[7464 byte] By [jas999a] at [2007-11-27 8:55:12]
# 1

Your draw method needs to be a part of your gui class. You draw some things on a Graphics context, but that Graphics never gets displayed on the screen.

The JFrame paintComponent(Graphics g) method is what you need. Use that Graphics context instead of making your own. The Graphics that gets passed to the paintComponent method is the one that the gui displays on the screen. Have your thread update the building every x seconds, then tell the gui to redraw the building.

hunter9000a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 2
hican anybody explain answer inside code... i will really appreciatethanks in advancejas
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 3
123
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 4
i really need help
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 5
> i really need helpI gave you help, what was wrong with it? Did you not understand what I said?
hunter9000a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 6
the thing is i m going to link it as DDE dynamic data exchange with Matlab.. if i use only graphics in short i dont know how to do it the way u told me...
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 7

> the thing is i m going to link it as DDE dynamic data

> exchange with Matlab.. if i use only graphics

What does that have to do with the problem?

> in short i dont know how to do it the way u told me...

Then you need to read the custom painting tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

hunter9000a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 8
thanks
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 9
actually i m making a vibration simulation in Matlab.. i have the codes i need to link it Java so it will be visual and interactive.. i need variables so i can assign each variables and link them at last
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 10

these are variables

private static final int XSIZE = 75;

private static final int YSIZE = 25;

private int x = 99;

private int y = 400;

private int dx = 2;

private int fl = 10;

private int x1, x2, y1, y2;

jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...
# 11
123
jas999a at 2007-7-12 21:15:59 > top of Java-index,Java Essentials,Java Programming...