Ask Experts Questions for FREE Help !
Ask
    DBello's Avatar
    DBello Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 6, 2013, 01:51 PM
    I am writing a java animation program that is just not working
    I am writing a java animation program that creates a frame and positions a ball at the top. When you click the start button the ball is supposed to move diagonally to the bottom. However it does not move. Can anybody tell me what I am doing wrong?


    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class BallControl2 implements ActionListener {

    JButton start;
    JButton left;
    JButton right;
    JButton up;
    JButton down;
    Display currentDisplay;//instance of class that overides paintComponent
    int motionMarker = 0;
    int x = 0;// x coordinate for animated ball
    int y = 0;// y coordinate for animated ball
    //Constructor

    public BallControl2() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    start = new JButton("Start");
    start.addActionListener(this);
    left = new JButton("Left");
    right = new JButton("Right");
    up = new JButton("Up");
    down = new JButton("down");
    BorderLayout border = new BorderLayout();
    frame.setLayout(border);
    FlowLayout flo = new FlowLayout();
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(flo);
    controlPanel.add(start);
    controlPanel.add(left);
    controlPanel.add(right);
    controlPanel.add(up);
    controlPanel.add(down);
    frame.add(controlPanel, BorderLayout.SOUTH);
    currentDisplay = new Display();
    frame.add(currentDisplay, BorderLayout.CENTER);



    frame.setVisible(true);
    }//End of constructor

    public static void main(String[] args) {
    try {//sleep to allow the ball to animate
    Thread.sleep(100);

    } catch (Exception ex) {
    }
    BallControl2 test = new BallControl2();


    }

    @Override
    public void actionPerformed(ActionEvent evt) {
    String t = evt.getActionCommand();
    if (t.equals("Start")) {
    motionMarker = 1;// ignore this for now
    animator();

    }
    }

    public class Display extends JPanel {// inner class that overrides paintComponent



    @Override
    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, this.getWidth(), this.getHeight());
    g2.setColor(Color.BLACK);
    g2.fillOval(x, y, 40, 40);

    }
    }
    public void animator(){
    while(true){
    x++;
    y++;
    currentDisplay.repaint();
    System.out.println(x);


    }
    }
    }
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Dec 1, 2013, 07:05 AM
    Adding a println of the values of x and y in paintComponent() indicates it only executes once.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Java Program [ 2 Answers ]

1) Code a java class for an Object SONG, that has as a minimum the following attributes:- SONG Title (a String) , SONG Group/artist (a String ), SONG genre (a String – “Pop”, “Jazz”, “Hard Rock” etc. SONG price (double), SONG duration (int number of seconds). Code all accessor and mutator methods...

Java program? [ 2 Answers ]

we have an assignment! but hold up! I would only ask what is the name of this code if I have fill up the blanks! code: puclic class problim{ public static void main(Stringarg){ int x=___; int y=___; while;(__<s){ x=____;

Java program error [ 1 Answers ]

Sir, program is for managing account details using jdbc but ,there error like some variable not accessible in specefic block . Please solve this problem. If you can.. //mypanel1-LogIn class mypanel1 extends JPanel { JTextField t1,t2; JButton b1,b2; mypanel1() { JLabel l1,l2; ...


View more questions Search