Reputation: 11
I'm relatively new to Java, and I was making a game where you dash up and down to avoid obstacles. I was almost done with all the main features, and had to do the score system and the background. Thinking the score would be easier, I wrote out a JLabel and added it to my JFrame. It flashed and disappeared. I'm fairly certain this is because I add the whole graphics method directly after, or something like that. But, if I add the JLabel after, that's all that gets added, and it takes up the whole screen.
package dash;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JPanel implements ActionListener {//ignore yellow line
//setting up class
Variables var = new Variables();
Timer timer = new Timer(5,this);
boolean spacePressed;
boolean gameActive = false;
boolean creatingObstacles = true;
int arrayLength;
int creationStep = 50;
int backgroundx;
int speed = 30;
int x;
int y;
int playery = 700;
int highscore;
int score = 1;
static int[] obstacles = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//making label
JLabel label = new JLabel("score: " + score);
//setting up graphics method
public void paintComponent(Graphics g) {
super.paintComponent(g);
//player
ImageIcon player = new ImageIcon("C:\\Users\\myname\\Documents\\Eclipse Projects\\dash\\player.png");
player.paintIcon(this,g,300,playery);
//label properties
label.setBounds(300,900,100,100);
//Some really cool code that took a while to write, this is going to generate random obstacles so I don't
//have to make all the levels since I'm lazy. This also makes graphics easier, since now I don't have
//to draw out the map taking up a lot of memory, at least several gigs probably more, for small levels.
int min = 500;
int max = 1500;
arrayLength = obstacles.length - 1;
//creating random array, im really proud of this
//code. it took me hours to write and looks badass
//except i have no clue how to label anything now T-T
while (creationStep > 0) {
if (creatingObstacles == true) {
obstacles[arrayLength] = (int)Math.floor(Math.random()*(max-min+1)+min) + var.previousNumber;
var.previousNumber = obstacles[arrayLength];
arrayLength--;
creationStep--;
if (arrayLength == -1) {
creatingObstacles = false;
arrayLength = obstacles.length - 1;
}}}
//drawing with the array
//ps i have no clue why this works, but it does
boolean onTop = true;
while (arrayLength >= 0) {
//drawing obstacles
ImageIcon obstacle = new ImageIcon("C:\\Users\\myname\\Documents\\Eclipse Projects\\dash\\obstacle.png");
obstacle.paintIcon(this,g,obstacles[arrayLength] + backgroundx,y);
//checking collision
if (obstacles[arrayLength] + backgroundx >= 300 && obstacles[arrayLength] + backgroundx <= 400 && y == playery) {
spacePressed = false;
gameActive = false;
backgroundx = 0;
}
//finishing loop
arrayLength--;
//deciding if it puts the obstacle on top or bottom
if (onTop) {
y = 200;
} else {
y = 700;
}
//toggling onTop boolean
onTop = !onTop;
}
//when to make a new array
if (obstacles[0] <= -backgroundx) {
var.previousNumber = -backgroundx + 2000;
creationStep = 50;
creatingObstacles = true;
speed = speed + 5;
}
//pause
if (gameActive == false) {
ImageIcon pauseScreen = new ImageIcon("C:\\Users\\myname\\Documents\\Eclipse Projects\\dash\\pausescreen.png");
pauseScreen.paintIcon(this,g,0,0);
}
//timer, this starts physics
timer.start();
}
//setting up keys
KeyListener key = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_ESCAPE) {
gameActive = !gameActive;
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == KeyEvent.VK_SPACE && gameActive == true) {
spacePressed = !spacePressed;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
};
//physics go here
public void actionPerformed(ActionEvent e) {
if (spacePressed == true) {
playery = 200;
} else {
playery = 700;
}
//movement
if (gameActive == true) {
backgroundx = backgroundx - speed;
}
repaint();
}
//main method
public static void main(String[] args) {
//implementing method
Main main = new Main();
//creating frame
JFrame frame = new JFrame();
frame.setFocusable(true);
frame.setSize(1920,1080);
frame.setVisible(true);
frame.addKeyListener(main.key);
frame.add(main.label);
frame.add(main);
}
}
class Variables{
int previousNumber;
}
Upvotes: 0
Views: 287
Reputation: 324088
frame.add(main.label);
frame.add(main);
The default layout manager for the frame is the BorderLayout.
You can't add two components to the "CENTER" of the BorderLayout, which is the default if you don't specify a constraint.
Try the following to see the difference:
frame.add(main.label, BorderLayout.PAGE_START);
frame.add(main, BorderLayout.CENTER);
Read the section from the Swing tutorial on Layout Manager for more information on how the BorderLayout and other layout managers work.
Upvotes: 1