How to add a object into a JPanel - java

I am trying to add a ball into a JPanel, however, I am unsure how to do this Below is the code I currently have for adding the ball into the JPanel. I am trying to add multiple balls in a random location onto the screen. The first appears as expected but any more than that don't appear on the screen.
Ball class;
public class Ball extends Component {
public static double ballX = rand.nextInt(500) + 40;
public static double ballY = rand.nextInt(300) + 10;
public static Ball[] balls = new Ball[20];
public Ball() {
}
public void draw(Graphics g2) {
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}
Method trying to add a ball into the JPanel;
public class BallFrames extends JFrame {
/* creates static varibles privite for use withing class and public for
use out side of the class */
public static final JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
public static final JPanel ballPanel = new JPanel();
public static JButton button;
public static JPanel displayPanel = new JPanel(new BorderLayout());
private static int FRAME_WIDTH = 600;
private static int FRAME_HEIGHT = 600;
/**
* constructor called method to create the components
*/
public BallFrames() {
setSize(FRAME_WIDTH,FRAME_HEIGHT);
createComponents(); // creates all compononents
}
/*
* calls helper methods to create all of the componanats needed
*/
private void createComponents() {
ActionListener listener = new BallListener();
createPanel(); // creates a panel
createButtons(listener); // Creates a button
createSlider();
}
/**
* method to create panels
*/
public void createPanel() {
BallComponent component = new BallComponent(); // creates a new BallCompoenet
displayPanel.add(component, BorderLayout.CENTER);
displayPanel.add(controlPanel, BorderLayout.SOUTH); // adds thecontrol panel
add(displayPanel); // adds the dice to the JFrame
}
public static void addBall() throws InterruptedException {
Ball.balls[BallListener.balls] = new Ball();
System.out.println(BallListener.balls); // testing
ballPanel.add(Ball.balls[BallListener.balls]);
// ballPanel.add(ball);
Runnable r = new BallRunnable();
Thread t = new Thread(r);
t.start();
t.join();
ballPanel.repaint();
}
How I am drawing them;
public class BallComponent extends JComponent {
/**
* calls paintComponenet to render images on the screen
*
* #param g
*/
#Override
public void paintComponent(Graphics g) {
int delay = 1000;
super.paintComponent(g); // call to super
Graphics2D g2 = (Graphics2D) g; // recover the graphic
TimerListener listener = new TimerListener();
Timer t = new Timer(delay, listener);
if (BallListener.gameOn == true) {
for(int i = 0; i < BallListener.balls; i++){
Ball.balls[i].draw(g2);
}
BallFrames.displayPanel.repaint();
}
}
ActionListener for the buttons;
public class BallListener implements ActionListener {
public static boolean gameOn = false;
public static int balls = 0;
/**
* gets the which button has been pressed and responds appriately
*
* #param event based on the event it will preform selected actions
*/
#Override
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
switch (action) {
case "start":
gameOn = true;
{
try {
BallFrames.addBall();
} catch (InterruptedException ex) {
Logger.getLogger(BallListener.class.getName()).log(Level.SEVERE, null, ex);
}
balls++;
}
BallFrames.displayPanel.repaint();
break;
case "pause":
break;
}
}
Ball Runnable;
public class BallRunnable implements Runnable {
private static Random rand = new Random();
public static boolean goLeft = true;
public static boolean goRight = false;
public static boolean goUp = false;
public static boolean goDown = false;
public static boolean diagonalUp = false;
public static boolean diagonalDown = false;
public static double speed = 0.2f;
public BallRunnable() {
}
public void run() {
boundsCheckY1();
boundsCheckX1();
boundsCheckY2();
boundsCheckX2();
}
public void boundsCheckX1() {
int temp;
if (Ball.ballX < 1) {
temp = rand.nextInt(5) + 1;
if(temp == 1) {
goRight = true;
goLeft = false;
goUp = false;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = false;
goDown = false;
goLeft = false;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = true;
goUp = false;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = true;
}
}
}
public void boundsCheckY1(){
int temp;
if (Ball.ballY < 1) {
temp = rand.nextInt(5) + 1;
if(temp == 1) {
goRight = false;
goLeft = false;
goUp = false;
goDown = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = false;
goDown = true;
goLeft = false;
diagonalUp = false;
diagonalDown = true;
} else if (temp == 5) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = true;
}
}
}
public void boundsCheckX2 () {
int temp;
if (Ball.ballX > 559) {
temp = rand.nextInt(5) + 1;
if(temp == 1) {
goRight = false;
goLeft = true;
goUp = false;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = false;
goUp = false;
goDown = true;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = false;
goUp = false;
goDown = false;
goLeft = true;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = false;
goUp = false;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = true;
}
}
}
public void boundsCheckY2() {
int temp;
if (Ball.ballY > 470) {
temp = rand.nextInt(5) + 1;
if(temp == 1) {
goRight = false;
goLeft = false;
goUp = true;
goDown = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 2) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 3) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = false;
diagonalDown = false;
} else if (temp == 4) {
goRight = true;
goUp = true;
goDown = false;
goLeft = false;
diagonalUp = true;
diagonalDown = false;
} else if (temp == 5) {
goRight = false;
goUp = true;
goDown = false;
goLeft = true;
diagonalUp = true;
diagonalDown = false;
}
}
}
}
The viewer class;
public class BallViewer {
/**
* creates the main frame
*
* #param args
*/
public static void main(String[] args) throws InterruptedException {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
// new BallFrames().setVisible(true);
JFrame frame = new BallFrames(); // creates new JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Multi-Treaded Balls");// Sets the Title
frame.setVisible(true); // Makes the frame visible
frame.setResizable(false);
}
});
}
}
the timer listener;
public class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
try {
movement();
} catch (InterruptedException ex) {
Logger.getLogger(TimerListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void movement() throws InterruptedException {
if(goLeft == true) {
Ball.ballX -= speed;
}
if(goRight == true) {
Ball.ballX += speed;
}
if(goUp == true) {
Ball.ballY -= speed;
}
if(goDown == true) {
Ball.ballY += speed;
}
if(diagonalUp == true) {
Ball.ballY += speed / 2;
}
if(diagonalDown == true) {
Ball.ballY -= speed / 2;
}
}
}
I want to be able to add more than one ball on the screen at a given time, which also moves around like the first ball.

Your definition of the ball class is behaving as expected:
public class Ball extends Component {
public static double ballX = rand.nextInt(500) + 40;
public static double ballY = rand.nextInt(300) + 10;
public static Ball[] balls = new Ball[20];
public Ball() {
}
public void draw(Graphics g2) {
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}
All of the static fields are shared between all instances of the class. That's what static means. All Balls have the same ballX and ballY coordinates.
What you are looking for is non-static fields that belong to each individual instance. You also have to initialize these fields every time you make a new ball. static fields only get initialized once when the class is loaded. There are a couple more changes that I would recommend:
If your x- and y-coordinates belong only to the one ball, it is better to make them private.
Extend JComponent instead of Component, since you are clearly using swing here.
Implement paintComponent instead of draw. It will make your drawing code require less maintenance if nothing else.
Here is a version of the class that should work a bit better:
public class Ball extends JComponent
{
private double ballX;
private double ballY;
public static Ball[] balls = new Ball[20];
public Ball()
{
ballX = rand.nextInt(500) + 40;
ballY = rand.nextInt(300) + 10;
}
public void paintComponent(Graphics g2)
{
Color color = new Color(r, g, b);
g2.setColor(color);
g2.fillOval((int) ballX, (int) ballY, 30, 30);
}
}

Painting code is for painting only.
You should NEVER create/start a Timer in a painting method. Get rid of all that code.
You should never invoke repaint(), this will cause an infinite loop.
Also, get rid of all you animation code. Your first task is to paint multiple balls on the panel. Once you get this working, then you worry about trying to do animation.
There is no need to extend JComponent, since you class is NOT a component. That is you never add the component to a panel. All you do is paint the object in another class.
So your BallComponent class (which is a component) needs to have an ArrayList which keeps track of the ball objects you want to paint. So you need a method to add a ball object to this ArrayList. Then the paintComponent() method simply iterates through the list and paints all the balls.
Check out the DrawOnComponent example found in Custom Painting Approaches.

Related

Snake game in Java Swing - My snake only grows

I'm trying to do the classic Snake game in Swing and I managed to make Snake move, but when it moves, it becomes infinitely long because it never erases its tail.
I have been using validate() and repaint() but nothing works.
Problem
This is my code:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class controls extends JPanel implements KeyListener, ActionListener { // Amb aquestes dues implementacions, fem que el programa pugui rebre per teclat
// Mida serp
private int[] longXserp = new int[750];
private int[] longYserp = new int[750];
// Longitut inicial de la serp
private int serplong = 3;
// Moviments que fem
private int moviments = 0;
// Controls
private boolean esquerra = false;
private boolean dreta = false;
private boolean amunt = false;
private boolean avall = false;
// Gràfics de moviment
private ImageIcon serpesquerra;
private ImageIcon serpdreta;
private ImageIcon serpamunt;
private ImageIcon serpavall;
private Timer timer;
private int velocitatserp = 100;
private ImageIcon serp;
public controls() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(velocitatserp, this);
timer.start();
}
public void paint(Graphics g) { // Mètode amb el que imprimim per pantalla. Ha d'anomenar-se "paint", o si no, no funcionarà
if(moviments == 0) {
longXserp[2] = 50;
longXserp[1] = 75;
longXserp[0] = 100;
longYserp[2] = 100;
longYserp[1] = 100;
longYserp[0] = 100;
}
serpdreta = new ImageIcon("src/grafics/serpdreta.png");
serpdreta.paintIcon(this, g, longXserp[0], longYserp[0]);
for(int a = 0; a < serplong; a++) {
if(a == 0 && esquerra) {
serpesquerra = new ImageIcon("src/grafics/serpesquerra.png");
serpesquerra.paintIcon(this, g, longXserp[a], longYserp[a]);
}
if(a == 0 && dreta) {
serpdreta = new ImageIcon("src/grafics/serpdreta.png");
serpdreta.paintIcon(this, g, longXserp[a], longYserp[a]);
}
if(a == 0 && amunt) {
serpamunt = new ImageIcon("src/grafics/serpamunt.png");
serpamunt.paintIcon(this, g, longXserp[a], longYserp[a]);
}
if(a == 0 && avall) {
serpavall = new ImageIcon("src/grafics/serpavall.png");
serpavall.paintIcon(this, g, longXserp[a], longYserp[a]);
}
if(a != 0) {
serp = new ImageIcon("src/grafics/serp.png");
serp.paintIcon(this, g, longXserp[a], longYserp[a]);
}
}
g.dispose();
}
#Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_RIGHT){ // Si polses la tecla X, la seva variable boolean es posa en true
moviments++;
dreta = true;
if(!esquerra) {
dreta = true;
}
else {
dreta = false;
esquerra = true;
}
amunt = false;
avall = false;
}
if(ke.getKeyCode() == KeyEvent.VK_LEFT){
moviments++;
esquerra = true;
if(!dreta) {
esquerra = true;
}
else {
esquerra = false;
dreta = true;
}
amunt = false;
avall = false;
}
if(ke.getKeyCode() == KeyEvent.VK_UP){
moviments++;
amunt = true;
if(!avall) {
amunt = true;
}
else {
amunt = false;
avall = true;
}
esquerra = false;
dreta = false;
}
if(ke.getKeyCode() == KeyEvent.VK_DOWN){
moviments++;
avall = true;
if(!amunt) {
avall = true;
}
else {
amunt = true;
avall = false;
}
esquerra = false;
dreta = false;
}
}
#Override
public void keyReleased(KeyEvent ke) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent ke) {
// TODO Auto-generated method stub
}
#Override
public void actionPerformed(ActionEvent ae) {
timer.start();
if(dreta) {
for(int d = serplong-1; d>=0; d--) {
longYserp[d+1] = longYserp[d];
}
for(int e = serplong; e>= 0; e--) {
if(e==0) {
longXserp[e] = longXserp[e] + 25;
}
else {
longXserp[e] = longXserp[e-1];
}
if(longXserp[e] > 850) {
longXserp[e] = 25;
}
}
repaint(); // Mètode per refrescar els gràfics tornant a cridar al mètode paint(). Es usa quan es realitzan canvis sobre els gràfics
}
if(esquerra) {
for(int d = serplong-1; d>=0; d--) {
longYserp[d+1] = longYserp[d];
}
for(int d = serplong; d>= 0; d--) {
if(d==0) {
longXserp[d] = longXserp[d] - 25;
}
else {
longXserp[d] = longXserp[d-1];
}
if(longXserp[d] < 25) {
longXserp[d] = 850;
}
}
repaint();
}
if(amunt) {
for(int d = serplong-1; d>=0; d--) {
longXserp[d+1] = longXserp[d];
}
for(int d = serplong; d>= 0; d--) {
if(d==0) {
longYserp[d] = longYserp[d] - 25;
}
else {
longYserp[d] = longYserp[d-1];
}
if(longYserp[d] < 75) {
longYserp[d] = 625;
}
}
repaint();
}
if(avall) {
for(int d = serplong-1; d>=0; d--) {
longXserp[d+1] = longXserp[d];
}
for(int d = serplong; d>= 0; d--) {
if(d==0) {
longYserp[d] = longYserp[d] + 25;
}
else {
longYserp[d] = longYserp[d-1];
}
if(longYserp[d] > 625) {
longYserp[d] = 75;
}
}
repaint();
}
}
}
I've been trying to figure out what's wrong for days. What is wrong with the above code?
public void paint(Graphics g) { ..
This should be:
public void paintComponent(Graphics g) {
super.paintComponent(g); ..
Note two important differences:
Override paintComponent for any class that is or extends JComponent.
Call the super method when custom painting to erase the previous drawings.
Other tips now I look more closely at the code:
serpdreta = new ImageIcon("src/grafics/serpdreta.png"); This type of resource loading should not be done in a paint method, which are expected to be completed in a minimal time. Instead load the image(s) when the class is constructed and store them as fields of the class, to then be used whenever needed.
But that path will likely fail when the app. is turned into a Jar. The src directory is typically not included.
Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.
If the app. needs to paint images, create an Image rather than an ImageIcon.
For better feedback when loading images, use ImageIO.read(..) rather than creating an ImageIcon as it will fail silently while ImageIO will provide useful information as to the cause of any problems.
For Swing, we typically use key bindings rather than the lower level KeyListener.

Java 2D Game Programming : Missile shooting is too fast?

Hello I'm fairly new to programming and this is my first time posting here so any help would be appreciated so:
my problem is that I"m trying to create some kind of 2D shooter game in java but I don't know if my simple game loop is good because when i shoot a missile it shoots a one every 20 ms and it's too fast and shoots a ton of missiles at once so is there any way to adjust it ? Like to keep some delay between every missile and the other??
and please tell me if i have problems or bad programming in my code !!
this is my game panel where most of the game happens and where my loop and adding missiles method in
public class GamePanel extends JPanel implements KeyListener {
Measurments mesure = new Measurments();
int panel_width = mesure.getUniversalWidth();
int panel_height = mesure.getUniversalHeight();
Timer timer;
Random rand = new Random();
ArrayList<Enemy> enemies = new ArrayList<>();
ArrayList<Missile> missiles = new ArrayList<>();
Player player = new Player(0, 0);
boolean up = false;
boolean down = false;
boolean right = false;
boolean left = false;
boolean isShooting = false;
boolean isRunning = true;
public boolean gameRunning() {
return isRunning;
}
int count = 5;
int missilesCount = 6;
public GamePanel() {
timer = new Timer(20, new ActionListener() {
public void actionPerformed(ActionEvent e) {
StartGame();
repaint();
}
});
setSize(panel_width, panel_height);
addKeyListener(this);
timer.start();
for (int i = 0; i < count; i++) {
addEnemy(new Enemy(rand.nextInt(750), rand.nextInt(500)));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
player.paint(g2d);
for (int i = 0; i < enemies.size(); i++) {
Enemy temp = enemies.get(i);
temp.paint(g2d);
}
for (int i = 0; i < missiles.size(); i++) {
Missile mis = missiles.get(i);
mis.paint(g2d);
mis.behave();
}
}
public void StartGame() {
if (isRunning) {
runGame();
setBackground(Color.YELLOW);
} else {
setBackground(Color.BLACK);
}
}
public void runGame() {
update();
};
public void update() {
player.checkBorders();
checkColls();
if (up) {
player.updateUp();
}
if (down) {
player.updateDown();
}
if (right) {
player.updateRight();
}
if (left) {
player.updateLeft();
}
if (isShooting) {
for (int i = 0; i < 5; i++) {
missiles.add(new Missile(player.getX() + 16, player.getY() + 16));
}
}
for (int i = 0; i < missiles.size(); i++) {
Missile temp = missiles.get(i);
if (temp.getX() == panel_width) {
RemoveMissile(temp);
}
}
}
public void addEnemy(Enemy e) {
enemies.add(e);
}
public void removeEnemy(Enemy e) {
enemies.remove(e);
}
public void addMissile(Missile e) {
missiles.add(e);
}
public void RemoveMissile(Missile e) {
missiles.add(e);
}
public void checkColls() {
for (int i = 0; i < enemies.size(); i++) {
Enemy tempEnm = enemies.get(i);
for (int e = 0; e < missiles.size(); e++) {
Missile tempMis = missiles.get(e);
if (tempMis.missileRect().intersects(tempEnm.enemyRect())) {
enemies.remove(tempEnm);
missiles.remove(tempMis);
}
}
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == e.VK_UP) {
up = true;
}
if (key == e.VK_DOWN) {
down = true;
}
if (key == e.VK_RIGHT) {
right = true;
}
if (key == e.VK_LEFT) {
left = true;
}
if (key == e.VK_ENTER) {
isRunning = true;
}
if (key == e.VK_SPACE) {
isShooting = true;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == e.VK_UP) {
up = false;
}
if (key == e.VK_DOWN) {
down = false;
}
if (key == e.VK_RIGHT) {
right = false;
}
if (key == e.VK_LEFT) {
left = false;
}
if (key == e.VK_SPACE) {
isShooting = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
Thanks in advance !!
private long fired = 0L;
public void update() {
...
// firing missiles: only if the missile count is less than the max., and the elapsed
// time is more than a limit (100 ms)
if ( isShooting && missiles.size() < missilesCount &&
( System.currentTimeMilis() - this.fired ) > 100 ) {
missiles.add( new Missile( player.getX() + 16, player.getY() + 16 ) );
// time of last firing
this.fired = System.currentTimeMilis();
}
...
}
public void RemoveMissile(Missile e) {
// as Guest is asked in another answer, this method should remove, not add...
missiles.remove(e);
}

How to add icon to JFrame [duplicate]

This question already has answers here:
How to change JFrame icon [duplicate]
(8 answers)
Closed 6 years ago.
How do I add an icon to this snake game and where do I put it, also how do I increase speed of the game after so many points? The code below is the class in which I believe these two pieces of code should go.
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class SnakeGame extends JFrame {
private static final long FRAME_TIME = 1000L / 50L;
private static final int MIN_SNAKE_LENGTH = 5;
private static final int MAX_DIRECTIONS = 3;
private BoardPanel board;
private SidePanel side;
private Random random;
private Clock logicTimer;
private boolean isNewGame;
private boolean isGameOver;
private boolean isPaused;
private LinkedList<Point> snake;
private LinkedList<Direction> directions;
private int score;
private int foodsEaten;
private int nextFoodScore;
private SnakeGame() {
super("Snake");
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
this.board = new BoardPanel(this);
this.side = new SidePanel(this);
add(board, BorderLayout.CENTER);
add(side, BorderLayout.EAST);
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
case KeyEvent.VK_UP:
if(!isPaused && !isGameOver) {
if(directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if(last != Direction.South && last != Direction.North) {
directions.addLast(Direction.North);
}
}
}
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
if(!isPaused && !isGameOver) {
if(directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if(last != Direction.North && last != Direction.South) {
directions.addLast(Direction.South);
}
}
}
break;
case KeyEvent.VK_A:
case KeyEvent.VK_LEFT:
if(!isPaused && !isGameOver) {
if(directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if(last != Direction.East && last != Direction.West) {
directions.addLast(Direction.West);
}
}
}
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
if(!isPaused && !isGameOver) {
if(directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if(last != Direction.West && last != Direction.East) {
directions.addLast(Direction.East);
}
}
}
break;
case KeyEvent.VK_P:
if(!isGameOver) {
isPaused = !isPaused;
logicTimer.setPaused(isPaused);
}
break;
case KeyEvent.VK_ENTER:
if(isNewGame || isGameOver) {
resetGame();
}
break;
}
}
});
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void startGame() {
this.random = new Random();
this.snake = new LinkedList<>();
this.directions = new LinkedList<>();
this.logicTimer = new Clock(10.0f);
//////////////////////////////////////////////////////////////////////////////////////////////////
this.isNewGame = true;
logicTimer.setPaused(true);
while(true) {
long start = System.nanoTime();
logicTimer.update();
if(logicTimer.hasElapsedCycle()) {
updateGame();
}
board.repaint();
side.repaint();
long delta = (System.nanoTime() - start) / 1000000L;
if(delta < FRAME_TIME) {
try {
Thread.sleep(FRAME_TIME - delta);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
private void updateGame() {
TileType collision = updateSnake();
if(collision == TileType.Food) {
foodsEaten++;
score += nextFoodScore;
spawnFood();
} else if(collision == TileType.SnakeBody) {
isGameOver = true;
logicTimer.setPaused(true);
} else if(nextFoodScore > 10) {
}
}
private TileType updateSnake() {
Direction direction = directions.peekFirst();
Point head = new Point(snake.peekFirst());
switch(direction) {
case North:
head.y--;
break;
case South:
head.y++;
break;
case West:
head.x--;
break;
case East:
head.x++;
break;
}
if(head.x < 0 || head.x >= BoardPanel.COL_COUNT || head.y < 0 || head.y >= BoardPanel.ROW_COUNT) {
return TileType.SnakeBody;
}
TileType old = board.getTile(head.x, head.y);
if(old != TileType.Food && snake.size() > MIN_SNAKE_LENGTH) {
Point tail = snake.removeLast();
board.setTile(tail, null);
old = board.getTile(head.x, head.y);
}
if(old != TileType.SnakeBody) {
board.setTile(snake.peekFirst(), TileType.SnakeBody);
snake.push(head);
board.setTile(head, TileType.SnakeHead);
if(directions.size() > 1) {
directions.poll();
}
}
return old;
}
private void resetGame() {
this.score = 0;
this.foodsEaten = 0;
this.isNewGame = false;
this.isGameOver = false;
Point head = new Point(BoardPanel.COL_COUNT / 2, BoardPanel.ROW_COUNT / 2);
snake.clear();
snake.add(head);
board.clearBoard();
board.setTile(head, TileType.SnakeHead);
directions.clear();
directions.add(Direction.North);
logicTimer.reset();
spawnFood();
}
public boolean isNewGame() {
return isNewGame;
}
public boolean isGameOver() {
return isGameOver;
}
public boolean isPaused() {
return isPaused;
}
private void spawnFood() {
this.nextFoodScore = 10;
int index = random.nextInt(BoardPanel.COL_COUNT * BoardPanel.ROW_COUNT - snake.size());
int freeFound = -1;
for(int x = 0; x < BoardPanel.COL_COUNT; x++) {
for(int y = 0; y < BoardPanel.ROW_COUNT; y++) {
TileType type = board.getTile(x, y);
if(type == null || type == TileType.Food) {
if(++freeFound == index) {
board.setTile(x, y, TileType.Food);
break;
}
}
}
}
}
public int getScore() {
return score;
}
public int getFoodsEaten() {
return foodsEaten;
}
public int getNextFoodScore() {
return nextFoodScore;
}
public Direction getDirection() {
return directions.peek();
}
public static void main(String[] args) {
SnakeGame snake = new SnakeGame();
snake.startGame();
}
}
Create a new ImageIcon object like this:
ImageIcon img = new ImageIcon(pathToFileOnDisk);
Then set it to your JFrame with setIconImage():
myFrame.setIconImage(img.getImage());
Also checkout setIconImages() which takes a List instead.
How to change JFrame icon
It 's not my answer !!!!

Why is my simple java2d Space Invaders game lagging?

I'm currently making a space invaders-esque game for my software engineering course. I've already got everything working that satisfies the requirements, so this isn't a 'solve my homework' kind of question. My problem is that the game will lag (at what seems like random times & intervals) to the point where it becomes too frustrating to play. Some things I think might be causing this - though I'm not positive - are as follows:
Problem with timer event every 10 ms (I doubt this because of the very limited resources required for this game).
Problem with collision detection (checking for collision with every visible enemy every 10 ms seems like it would take up a large chunk of resources)
Problem with repainting? This seems unlikely to me however...
#SuppressWarnings("serial")
public class SIpanel extends JPanel {
private SIpanel panel;
private Timer timer;
private int score, invaderPace, pulseRate, mysteryCount, distanceToEdge;
private ArrayList<SIthing> cast;
private ArrayList<SIinvader> invaders, dead;
private ArrayList<SImissile> missileBase, missileInvader;
private SIinvader[] bottomRow;
private SIbase base;
private Dimension panelDimension;
private SImystery mysteryShip;
private boolean gameOver, left, right, mysteryDirection, space, waveDirection;
private boolean runningTimer;
private Music sound;
private void pulse() {
pace();
processInputs();
if (gameOver) gameOver();
repaint();
}
private void pace() {
// IF invaders still live
if (!invaders.isEmpty()) {
invaderPace++;
// Switch back manager
if (distanceToEdge <= 10) {
switchBack();
pulseRate = (pulseRate >= 16) ? (int) (pulseRate*(0.8)) : pulseRate;
waveDirection = !waveDirection;
distanceToEdge = calculateDistanceToEdge();
}
// Move invaders left/right
else if (invaderPace >= pulseRate) {
invaderPace = 0;
distanceToEdge = calculateDistanceToEdge();
moveAI();
invadersFire();
if (!dead.isEmpty()) removeDead();
if (mysteryCount < 1) tryInitMysteryShip();
}
// All invaders are kill, create new wave
} else if (missileBase.isEmpty() && missileInvader.isEmpty() && !cast.contains(mysteryShip)) {
// System.out.println("New Wave!");
newWave();
}
// Every pace
if (!missileBase.isEmpty()) moveMissileBase();
// Every two paces
if (invaderPace % 2 == 0) {
if (!missileInvader.isEmpty()) moveMissileInvader();
if (mysteryCount > 0) moveMysteryShip();
}
}
private void processInputs() {
if (left) move(left);
if (right) move(!right);
if (space) fireMissile(base, true);
}
protected void fireMissile(SIship ship, boolean isBase) {
if(isBase && missileBase.isEmpty()) {
base.playSound();
SImissile m = new SImissile(ship.getX()+(ship.getWidth()/2), ship.getY()-(ship.getHeight()/4));
missileBase.add(m);
cast.add(m);
} else if (!isBase && missileInvader.size()<3) {
base.playSound();
SImissile m = new SImissile(ship.getX()+(ship.getWidth()/2), ship.getY()+(ship.getHeight()/4));
missileInvader.add(m);
cast.add(m);
}
}
private void newWave() {
pulseRate = 50;
int defaultY=60, defaultX=120, defaultWidth=30, defaultHeight=24;
for(int i=0; i<5; i++) {
for(int j=0; j<10; j++) {
if (i<1) invaders.add(new SItop((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
else if (i<3) invaders.add(new SImiddle((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
else if (i<5) invaders.add(new SIbottom((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
}
}
for (SIinvader s: invaders) {
cast.add(s);
}
if (!cast.contains(base)) {
cast.add(base);
}
bottomRow = getBottomRow();
}
private void tryInitMysteryShip() {
Random rand = new Random();
int x=rand.nextInt(1000);
if (x<=3) {
mysteryCount = 1;
if (rand.nextBoolean()) {
mysteryDirection = true;
}
if (mysteryDirection) {
mysteryShip = new SImystery(0, 60, 36, 18);
} else {
mysteryShip = new SImystery(480, 60, 36, 18);
}
cast.add(mysteryShip);
}
}
private void moveMysteryShip() {
int distance = 0;
if (mysteryDirection) {
mysteryShip.moveRight(5);
distance = getWidth() - mysteryShip.getX();
} else {
mysteryShip.moveLeft(5);
distance = 30+mysteryShip.getX()-mysteryShip.getWidth();
}
if (distance <= 5) {
dead.add(mysteryShip);
mysteryShip = null;
mysteryCount = 0;
}
}
private void removeDead() {
#SuppressWarnings("unchecked")
ArrayList<SIinvader> temp = (ArrayList<SIinvader>) dead.clone();
dead.clear();
for (SIinvader s : temp) {
invaders.remove(s);
cast.remove(s);
}
bottomRow = getBottomRow();
}
private void invadersFire() {
int[] p = new int[bottomRow.length];
for (int i=0; i<p.length; i++) {
for (int j=0; j<p.length; j++) {
p[j] = j;
}
Random rand = new Random();
int a=rand.nextInt(101);
if (a>=20) {
int b=rand.nextInt(p.length);
fireMissile(bottomRow[b], false);
}
}
}
private int calculateDistanceToEdge() {
int distance = 0;
SIinvader[] outliers = getOutliers();
if (waveDirection) {
distance = getWidth() - outliers[0].getX()-outliers[0].getWidth();
} else {
distance = outliers[1].getX();
}
return distance;
}
private SIinvader[] getOutliers() {
SIinvader leftMost = invaders.get(0), rightMost = invaders.get(0);
for (SIinvader s : invaders) {
if (s.getX() < leftMost.getX()) {
leftMost = s;
}
if (s.getX() > rightMost.getX()) {
rightMost = s;
}
}
return new SIinvader[] { rightMost, leftMost };
}
private SIinvader[] getBottomRow() {
SIinvader[] x = new SIinvader[(invaders.size()>10)?10:invaders.size()];
for (int i=0; i<x.length; i++) {
x[i] = invaders.get(i);
for (SIinvader s:invaders) {
if (s.getX() == x[i].getX()) {
if (s.getY() > x[i].getY()) {
x[i] = s;
}
}
}
}
return x;
}
private void move(boolean b) {
int defaultX = 5;
if (b) base.moveLeft(defaultX);
else base.moveRight(defaultX);
}
private void moveAI() {
for(SIinvader s : invaders) {
s.changeImage();
int defaultX = 5;
if (waveDirection) s.moveRight(defaultX);
else s.moveLeft(defaultX);
}
}
private void moveMissileBase() {
if (invaders.isEmpty()) return;
int movement = -5, bound = 0;
SImissile missile = missileBase.get(0);
missile.moveDown(movement);
SIinvader lowestInvader = getLowestInvader();
if (missile.getY() < (lowestInvader.getY() + lowestInvader.getHeight())) {
for (SIinvader s:bottomRow) {
if (checkCollision(missile, s)) {
s.setHit();
dead.add(s);
cast.remove(missile);
missileBase.clear();
score += s.value;
return;
}
}
if (mysteryCount > 0) {
if (checkCollision(missile, mysteryShip)) {
mysteryShip.setHit();
dead.add(mysteryShip);
cast.remove(missile);
missileBase.clear();
score += mysteryShip.value;
return;
}
}
if (missile.getY() < bound) {
missileBase.remove(missile);
cast.remove(missile);
}
}
}
private SIinvader getLowestInvader() {
SIinvader lowest = bottomRow[0];
for (SIinvader invader : bottomRow) {
if (invader.getY() > lowest.getY()) {
lowest = invader;
}
}
return lowest;
}
private void moveMissileInvader() {
int movement = 5, bound = (int) panelDimension.getHeight();
for (SImissile missile : missileInvader) {
missile.moveDown(movement);
if(missile.getY() >= base.getY()) {
if (checkCollision(missile, base)) {
base.setHit();
gameOver = true;;
missileInvader.remove(missile);
cast.remove(missile);
return;
} else if (missile.getY() >= bound-25) {
missileInvader.remove(missile);
cast.remove(missile);
return;
}
}
}
}
private boolean checkCollision(SIthing missile, SIthing ship) {
Rectangle2D rect1 = new Rectangle2D.Double(
missile.getX(),
missile.getY(),
missile.getWidth(),
missile.getHeight()
);
Rectangle2D rect2 = new Rectangle2D.Double(
ship.getX(),
ship.getY(),
ship.getWidth(),
ship.getHeight()
);
return rect1.intersects(rect2);
}
private void switchBack() {
int defaultY = 12;
for (SIinvader s : invaders) {
if (s.getY() > getHeight()) {
gameOver = true;
return;
}
s.moveDown(defaultY);
}
}
private void gameOver() {
pause(true);
SI.setGameOverLabelVisibile(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
Font font = new Font("Arial", 0, 20);
setFont(font);
String score = "Score: "+this.score;
Rectangle2D rect = font.getStringBounds(score, g2.getFontRenderContext());
int screenWidth = 0;
try { screenWidth = (int) panelDimension.getWidth(); }
catch (NullPointerException e) {}
g2.setColor(Color.GREEN);
g2.drawString(score, (int) (screenWidth - (10 + rect.getWidth())), 20);
for(SIthing a:cast) {
a.paint(g);
}
}
public SIpanel() {
super();
setBackground(Color.BLACK);
cast = new ArrayList<SIthing>();
missileBase = new ArrayList<SImissile>();
score = invaderPace = mysteryCount = pulseRate = 0;
sound = new Music("AmbientMusic.wav");
panel = this;
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : left = true; break;
case KeyEvent.VK_RIGHT : right = true; break;
case KeyEvent.VK_SPACE : space = true; break;
}
}
#Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : left = false; break;
case KeyEvent.VK_RIGHT : right = false; break;
case KeyEvent.VK_SPACE : space = false; break;
}
}
});
setFocusable(true);
timer = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pulse();
}
});
}
public void reset() {
SI.setGameOverLabelVisibile(false);
score = invaderPace = mysteryCount = 0;
pulseRate = 50;
cast = new ArrayList<SIthing>();
invaders = new ArrayList<SIinvader>();
dead = new ArrayList<SIinvader>();
missileBase = new ArrayList<SImissile>();
missileInvader = new ArrayList<SImissile>();
base = new SIbase(230, 370, 26, 20);
waveDirection = true;
gameOver = false;
sound.stop();
sound.loop();
panelDimension = SI.getFrameDimensions();
bottomRow = getBottomRow();
newWave();
timer.start();
runningTimer=true;
}
public SIpanel getPanel() {
return this.panel;
}
public void pause(boolean paused) {
if (paused) timer.stop();
else timer.start();
}
}
I believe that collision detection may be the reason for lagging and you should simply investigate it by trying to increase and decrease count of enemies or missiles drastically to see if that makes a difference.
Consider garbage collector your enemy. In your checkCollision method you are instantiating two (very simple) objects. It may not seem like a lot, but consider that your might be creating them for each collision check, and that at 60fps it adds up until it may reach critical mass when GC says "stop the world" and you see noticeable lag.
If that is the case, possible solution to that would be to not instantiate any objects in a method called so frequently. You may create Rectangle2D once, and then update its position, instead of creating a new one each time, so you will avoid unnecessary memory allocation.

Mouse move event in gxt grid

i want to select rows in grid on mouse drag for that i have following piece of code :
my mouse event listener is DragMultiSelectListener.java
import java.util.List;
import com.myexample.client.ui.items.AbstractContentItem;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
public class DragMultiSelectListener implements Listener<BaseEvent> {
Presenter presenter = null;
/* multiple select on drag */
private boolean mousePressed = false;
private boolean dragEnable = false;
private boolean selected = false;
private List<AbstractContentItem> currentSelected = null;
private int startIndex = -1;
private int endIndex = -1;
private int startX=0;
private int startY=0;
private int endX=0;
private int endY=0;
/** mouseMovedStarted used for preventing calling Grid onClick event for IE while using multiple select on DND **/
private boolean mouseMovedStarted = false;
public DragMultiSelectListener(Presenter pPresenter) {
this.presenter = pPresenter;
}
#Override
public void handleEvent(BaseEvent be) {
if(be.getType() == Events.OnMouseDown){
GridEvent ge = (GridEvent) be;
/* Multiple Row Select */
mouseMovedStarted = false;
mousePressed = true;
startIndex = ge.getRowIndex();
/* Deselect All */
if(ge.getTarget().getClassName().equals("x-grid3-scroller")){
if(ge.getTarget().getClientWidth() < ge.getTarget().getScrollWidth()){
if(ge.getTarget().getClientHeight() < (ge.getClientY() - ge.getTarget().getAbsoluteTop())){
mousePressed = false;
}
if(ge.getTarget().getClientWidth() < (ge.getClientX() - ge.getTarget().getAbsoluteLeft())){
mousePressed = false;
}
}
presenter.deselectAllContentItem();
}
/* Select Record */
if(currentSelected != null && (!presenter.getSelectedContent().isEmpty())){
selected = false;
for(AbstractContentItem item : currentSelected){
AbstractContentItem clickedItem = (AbstractContentItem) ge.getGrid().getStore().getAt(ge.getRowIndex());
if(clickedItem != null && item.getDTO().getId() == clickedItem.getDTO().getId()){
selected = true;
}
}
}else{
currentSelected = presenter.getSelectedContent();
selected = false;
}
dragEnable = false;
if(ge.getTarget().getNodeName().equalsIgnoreCase("SPAM")){
dragEnable = true;
}
if(selected){
dragEnable = true;
}
/* draw box */
if(mousePressed){
startX = ge.getClientX();
startY = ge.getClientY();
}
}else if(be.getType() == Events.OnMouseMove){
GridEvent ge = (GridEvent) be;
if(mousePressed && !dragEnable && (ge.getRowIndex() != -1) ){
if(startIndex == -1){
startIndex = ge.getRowIndex();
}
endIndex = ge.getRowIndex();
presenter.deselectAllContentItem();
if((startIndex - endIndex) >= 0){
for(int i = endIndex; i<=startIndex; i++ ){
presenter.selectContentItem(i, true);
}
}else{
for(int i = startIndex; i<=endIndex; i++ ){
presenter.selectContentItem(i, true);
}
}
/* Show selection box */
endX = ge.getClientX();
endY = ge.getClientY();
}else if(mousePressed && !dragEnable){
/* Show selection box */
endX = ge.getClientX();
endY = ge.getClientY();
if(ge.getTarget().getClassName().equals("x-grid3-scroller")){
if(startY > endY){
presenter.deselectAllContentItem();
}
}
displaySelectionBox(ge.getGrid().getAbsoluteLeft(), ge.getGrid().getAbsoluteTop());
}
mouseMovedStarted = true;
}else if(be.getType() == Events.OnMouseUp){
GridEvent ge = (GridEvent) be;
mousePressed = false;
currentSelected = presenter.getSelectedContent();
}
}
public boolean isMouseMovedStarted() {
return mouseMovedStarted;
}
public boolean isDragEnable() {
return dragEnable;
}
public void setEndX(int endX) {
this.endX = endX;
}
public void setEndY(int endY) {
this.endY = endY;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public List<AbstractContentItem> getCurrentSelected() {
return currentSelected;
}
public void setCurrentSelected(List<AbstractContentItem> currentSelected) {
this.currentSelected = currentSelected;
}
public void setMousePressed(boolean mousePressed) {
this.mousePressed = mousePressed;
}
}
Register grid event by following code
dragMultiSelectListener = new DragMultiSelectListener(presenter);
grid.addListener(Events.OnMouseDown, dragMultiSelectListener);
grid.addListener(Events.OnMouseMove, dragMultiSelectListener);
grid.addListener(Events.OnMouseUp, dragMultiSelectListener);
problem is that when i move mouse fast than it will skip selecting some of row.
i don't understand, how to improve execution speed to prevent of skipping rows.
Thanks
You can select all rows from dragStart x y, till your current x y. Only thing, you will need row hight to calculate rows then.

Categories