Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So, I am using a Java Swing Timer because putting the animation code in a run() method of a Thread subclass caused an insane amount of flickering that is really a terrible experience for any video game player.

Can anyone give me any tips on:

  • Why there is no animation...
  • Why the JFrame will not close when it is coded to Exit_On_Close 2 times

My code is here:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.net.URL;


//////////////////////////////////////////////////////////////// TFQ
public class TFQ extends JFrame {

DrawingsInSpace dis;

//========================================================== constructor
    public TFQ() {

        dis = new DrawingsInSpace();

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());

       this.setContentPane(dis);

       this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("Plasma_Orbs_Off_Orion");
        this.setSize(500,500);
        this.pack();



        //... Create timer which calls action listener every second..
        //    Use full package qualification for javax.swing.Timer
        //    to avoid potential conflicts with java.util.Timer.
        javax.swing.Timer t = new javax.swing.Timer(500, new TimePhaseListener());
        t.start();
    }



    /////////////////////////////////////////////// inner class Listener thing
    class TimePhaseListener implements ActionListener, KeyListener {

        //  counter
        int total;

        // loop control
        boolean Its_a_go = true;


        //position of our  matrix
        int tf = -400;

        //sprite directions
        int Sprite_Direction;
        final int RIGHT = 1;
        final int LEFT = 2;

        //for obstacle
        Rectangle mega_obstacle = new Rectangle(200, 0, 20, HEIGHT);

        public void actionPerformed(ActionEvent e) {
            //... Whenever this is called, repaint the screen 
         dis.repaint();

        addKeyListener(this);
        while (Its_a_go) { 
            try { 
            dis.repaint();

            if(Sprite_Direction == RIGHT) { 


            dis.matrix.x += 2;


            } // end if i think
            if(Sprite_Direction == LEFT) { 

            dis.matrix.x -= 2;

            }

            } catch(Exception ex) { 
                System.out.println(ex);
            }
        } // end while i think
        } // end actionPerformed

        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyTyped(KeyEvent event) {
            // TODO Auto-generated method stub
            if (event.getKeyChar()=='f'){
                Sprite_Direction = RIGHT;
                System.out.println("matrix should be animating now ");
                System.out.println("current matrix position = " + dis.matrix.x);
            }
            if (event.getKeyChar()=='d') { 
                Sprite_Direction = LEFT;
                System.out.println("matrix should be going in reverse");
                System.out.println("current matrix position = " + dis.matrix.x);
            }

        }


    }



    //================================================================= main
    public static void main(String[] args) {
        JFrame SafetyPins = new TFQ();

        SafetyPins.setVisible(true);
        SafetyPins.setSize(500,500);
        SafetyPins.setResizable(true);
        SafetyPins.setLocationRelativeTo(null);
    SafetyPins.setDefaultCloseOperation(EXIT_ON_CLOSE);


    }
}

class DrawingsInSpace extends JPanel { 

 URL url1_plasma_orbs;
 URL url2_matrix;
 Image img1_plasma_orbs;
 Image img2_matrix;

// for the plasma_orbs
Rectangle bbb = new Rectangle(0,0, 0, 0);
// for the matrix
Rectangle matrix = new Rectangle(-400, 60, 430, 200);

public DrawingsInSpace() {
    //load URLs
    try { 
        url1_plasma_orbs = this.getClass().getResource("plasma_orbs.png");
        url2_matrix = this.getClass().getResource("matrix.png");
    } catch(Exception e) { 
        System.out.println(e);
    }
    // attach the URLs to the images
    img1_plasma_orbs = Toolkit.getDefaultToolkit().getImage(url1_plasma_orbs);
    img2_matrix = Toolkit.getDefaultToolkit().getImage(url2_matrix);
}

public void paintComponent(Graphics g) { 
    super.paintComponent(g);

    // draw the plasma_orbs
    g.drawImage(img1_plasma_orbs, bbb.x, bbb.y,this);


    //draw the matrix

    g.drawImage(img2_matrix, matrix.x, matrix.y, this);

    }

} // end class
share|improve this question
1  
I did not even know you could get scroll bars within scroll bars... Anyway, there's way too much code here. You seem to be asking us to find where the problem is in your code. This is too localized a question for the site. You need to step through your code (learn to use the debugger) and find where things are going wrong. When you find where things are going wrong, and you don't know how to fix it, you should ask here. See the FAQ about what types of questions to ask here. –  Byte56 Dec 5 '12 at 23:29
add comment

closed as too localized by Byte56, Trevor Powell, Laurent Couvidou, Josh Petrie, Sean Middleditch Dec 28 '12 at 8:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Your timer event has an infinite loop. This blocks the event dispatch thread, blocking any other GUI events like painting or the frame closing from occurring.

Learn the Java code conventions, they make your programs much easier to read. Variables should not start with an uppercase letter.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.