-
Programming problem
I would like to make an object follow a path in java?
Any hints/help? :(emo):
Code:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Roll extends JPanel implements ActionListener{
int width = 700; int height = 500;
Timer ticker = new Timer(20,this); // 20 ms between signals
double x = 0; double y = 250;double r=50;double h;
public Roll(){
setPreferredSize(new Dimension(width,height));
ticker.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval((int)x,(int)y,50,50);
}
public int i=0;
public void actionPerformed(ActionEvent e){
if (e.getSource() == ticker) {
x += 2;
y-=2;
h=400-y;
// this.getWidth/Height()();
// this.getx();
// this.gety();
repaint();
}
}}
Here's the driver
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RollDriver{
public static void main(String[] args){
DisplayWindow d = new DisplayWindow();
Roll p = new Roll();
//Color blue = new Color();
d.addPanel(p);
d.showFrame();
//System.out.print(p.getX());
//System.out.print(p.getY());
//d.setBackGround(blue);
}
}
Code:
import java.awt.*;
import javax.swing.*;
public class DisplayWindow extends JFrame {
private final static int width_ = 800;
private final static int height_ = 400;
private final static String defaultDisplay_ = "Default";
private Container c;
public DisplayWindow() {
super(defaultDisplay_);
c = this.getContentPane();
}
public DisplayWindow(String title) {
super(title);
c = this.getContentPane();
}
public void addPanel(JPanel p) {
c.add(p);
}
public void addPanel(JPanel p, boolean useDefault) {
if(useDefault)
p.setPreferredSize(new Dimension(width_, height_));
c.add(p);
}
public void showFrame() {
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
-
Re: Programming problem
- What is this DisplayWindow class that you're using?
- You already appear to have code that uses a Swing Timer for an animation. Why not simply adapt this code for your needs?
-
Re: Programming problem
Please go through the Forum Rules, particularly the third paragraph.
db
-
Re: Programming problem
OK, I guess you've chosen to ignore my suggestions. If one confuses you, rather than ignore it, better to ask for clarification.