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
enter code here