Results 1 to 4 of 4
  1. #1
    pyler is offline Member
    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0

    Default Programming problem

    I would like to make an object follow a path in java?
    Any hints/help?

    Java 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
    Java 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);
       }
    }
    Java 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);
        }
    
    }
    Last edited by pyler; 12-08-2012 at 03:50 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Programming problem

    1. What is this DisplayWindow class that you're using?
    2. You already appear to have code that uses a Swing Timer for an animation. Why not simply adapt this code for your needs?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Programming problem

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Simple Problem with Object-Oriented Programming...
    By CuppaCoffee in forum New To Java
    Replies: 12
    Last Post: 01-04-2012, 10:12 PM
  2. Problem with Graphics OO programming Java.
    By Martino in forum Java 2D
    Replies: 2
    Last Post: 04-27-2011, 02:33 PM
  3. A challenging Engineering Programming problem
    By datastructures in forum New To Java
    Replies: 4
    Last Post: 12-03-2010, 10:36 PM
  4. Problem on server side (Socket Programming)
    By ersachinjain in forum Networking
    Replies: 9
    Last Post: 05-06-2010, 04:21 PM
  5. Simple Programming problem loop in array
    By katon in forum New To Java
    Replies: 1
    Last Post: 12-20-2009, 03:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •