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! :(
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! :(