PDA

Click to See Complete Forum and Search --> : java graphics-animation


maki
May 2nd, 2006, 06:09 AM
Hallo!

I am new in java programming and i am trying to create animation in java graphics. What i am trying to do is give motion to a simple line. The code that i have created so far is the following:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class My extends JPanel implements ActionListener {


private JFrame f;

private JMenuBar jmb;
private JMenu selectMenu;
private JMenuItem quitItem;
private JMenuItem againItem;
private JMenuItem helloItem;

private int horizontialDimension;
private int verticalDimension;

private int index;

Graphics display;

Font font;

InnerThread in;

Thread tr;


public My() {

setSize(600,400);

f = new JFrame("My");

f.setSize(600, 400);

jmb = new JMenuBar();
selectMenu = new JMenu("Select");
quitItem = new JMenuItem("Quit");
againItem = new JMenuItem("Do it Again");
helloItem = new JMenuItem("Say Hello");
quitItem.addActionListener(this);
againItem.addActionListener(this);
helloItem.addActionListener(this);

selectMenu.add(quitItem);
selectMenu.add(againItem);
selectMenu.add(helloItem);
jmb.add(selectMenu);
f.getContentPane().add(jmb, BorderLayout.NORTH);
f.getContentPane().add(this, BorderLayout.CENTER);

horizontialDimension = getSize().width;
verticalDimension = getSize().height;

f.setVisible(true);

display = getGraphics();

font = new Font("TimesRoman", Font.BOLD, 36);


}


public void paint(Graphics g) {

g.setColor(Color.blue);
g.drawLine(220,100,220,120);
//left arrow



}
public void update(Graphics g) {
for (int x=220; x<300; x++){
int y1=(int)(x);
int y2=(int)(x);

g.drawLine(y1,100,y2,120);


}

}

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==quitItem) {

System.exit(0);

} else if(ae.getSource()==againItem) {

in = new InnerThread();

tr = new Thread(in);

tr.start();


System.out.println("Again!!!");

} else {

System.out.println("Hello!!!");

}


}




public static void main(String[] args){

My st = new My();

InnerThread stin = st.new InnerThread();

Thread t = new Thread(stin);

t.start();

}




public class InnerThread implements Runnable {


public void run() {



My.this.update(display);

try {

Thread.currentThread().sleep(100);

} catch(InterruptedException ie) {}

}


}



}

I think that the problem is in the class update. When i run the program i get a rectungle instead of a moving line. Please Help me! :(

KYOrange
Jun 1st, 2006, 09:53 AM
in your update() code, you were not updating the line, instead, you draw a new line everytime it loop, the for loop, looped 80 times, and u drew 80 blue lines, but you forgot to clear the old line, that's why all the new lines and old lines all piled up, forming a rectangle

maki
Jun 11th, 2006, 12:57 PM
The problem is that i do not know how to update the line :)