I am a beginner programmer and I am trying to make a simple 2D game, Breakout. I have the logic for the game itself working but I am having trouble with getting the buttons to work with the game. I added a start and pause button to the top of the screen but whenever I pressed one of these buttons, the paddle at the bottom of the screen cannot move.
When I take out the buttons the game runs perfectly.
Any help would be greatly appreciated! Thank you in advance.
Below is the code for my game. I only included the Breakout class because this was were I ran into trouoble.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
public class Breakout extends JPanel implements KeyListener, ActionListener
{
boolean rightPressed;
boolean leftPressed;
boolean upPressed;
boolean downPressed;
boolean inGame;
int width = 1450;
int height = 900;
Timer tm = new Timer(5, this);
int numRows =5;
int numCols = 8;
int numBricks = 40;
int mouseX;
int mouseY;
Paddle paddle = new Paddle(width/2, height-40);
Ball ball = new Ball();
//BrickArray brick = new BrickArray(numRows, numCols, width/numCols, (height-100)/numRows);
Brick[][] bricks = new Brick[8][5];
JButton start = new JButton("Start");
JButton pause = new JButton("Pause");
JButton reset = new JButton("Reset");
JButton exit = new JButton("Exit");
JButton help = new JButton("Help");
public Breakout()
{
setDoubleBuffered(true);
//tm.start();
addKeyListener(this);
setFocusable(true);
add(start);
start.addActionListener(new StartAction());
add(pause);
pause.addActionListener(new PauseAction());
add(reset);
add(exit);
add(help);
ball.setMaxX(width);
ball.setMaxY(height);
ball.setWidth(width);
ball.setHeight(height);
ball.setX(width/2); //width/2
ball.setY(height/2); //height
paddle.setMaxX(width-paddle.getWidth());
int k=0;
//row 1
for(int a=0; a<bricks.length; a++)
{
bricks[a][0]=new Brick(120,40);
bricks[a][0].setX(a*180);
bricks[a][0].setY(50);
//System.out.println(k + ": " + a*80);
}
//row 2
for(int b=0; b<bricks.length; b++)
{
bricks[b][1]=new Brick(120,40);
bricks[b][1].setX(b*180);
bricks[b][1].setY(100);
}
//row 3
for(int c=0; c<bricks.length; c++)
{
bricks[c][2]=new Brick(120,40);
bricks[c][2].setX(c*180);
bricks[c][2].setY(150);
}
//row 4
for(int d=0; d<bricks.length; d++)
{
bricks[d][3]=new Brick(120,40);
bricks[d][3].setX(d*180);
bricks[d][3].setY(200);
}
for(int e=0; e<bricks.length; e++)
{
bricks[e][4]=new Brick(120,40);
bricks[e][4].setX(e*180);
bricks[e][4].setY(250);
}
}
public void actionPerformed(ActionEvent e)
{
tm.setRepeats(true);
if(rightPressed)
{
paddle.setX(paddle.getX()+5);
//System.out.println("Pressed");
}
if(leftPressed)
paddle.setX(paddle.getX()-5);
/*if(upPressed)
y--;
if(downPressed)
y++;*/
ball.checkCollision(paddle);
for(int i=0; i<bricks.length; i++)
{
for(int j=0; j<bricks[i].length; j++)
{
if(ball.checkCollision(bricks[i][j]))
{
bricks[i][j].destroy();
//System.out.println(bricks[i][j].isDestroyed());
numBricks--;
ball.move();
repaint();
}
}
}
//ball.checkCollision(brick);
ball.move();
repaint();
}
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT)
{
leftPressed=true;
}
/*if(c == KeyEvent.VK_UP)
{
upPressed=true;
}*/
if(c == KeyEvent.VK_RIGHT)
{
rightPressed=true;
//System.out.println("Pressed");
}
/*if(c == KeyEvent.VK_DOWN)
{
downPressed=true;
}*/
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
int c = e.getKeyCode();
if(c == KeyEvent.VK_LEFT)
{
leftPressed=false;
}
if(c == KeyEvent.VK_UP)
{
upPressed=false;
}
if(c == KeyEvent.VK_RIGHT)
{
rightPressed=false;
}
if(c == KeyEvent.VK_DOWN)
{
downPressed=false;
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int a=0; a<bricks.length; a++)
{
g.setColor(Color.RED);
bricks[a][0].paintComponent(g);
}
for(int b=0; b<bricks.length; b++)
{
g.setColor(Color.ORANGE);
bricks[b][1].paintComponent(g);
}
for(int c=0; c<bricks.length; c++)
{
g.setColor(Color.YELLOW);
bricks[c][2].paintComponent(g);
}
for(int d=0; d<bricks.length; d++)
{
g.setColor(Color.GREEN);
bricks[d][3].paintComponent(g);
}
for(int e=0; e<bricks.length; e++)
{
g.setColor(Color.BLUE);
bricks[e][4].paintComponent(g);
}
ball.paintComponent(g);
paddle.paintComponent(g);
g.setColor(Color.BLACK);
g.drawString("Bricks left: " + numBricks, 10, 10);
}
public static void main(String[] args)
{
Breakout obj = new Breakout();
JFrame jf = new JFrame();
jf.setTitle("Breakout");
jf.setSize(1450,900);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(obj);
}
class StartAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
tm.start();
System.out.println("Start");
}
}
class PauseAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
tm.stop();
System.out.println("Pause");
}
}
}
I don't have the other classes implemented so I cannot confirm if this will work.
start.setFocusable(false);
pause.setFocusable(false);
Alternatively
start.addKeyListener(this);
pause.addKeyListener(this);
Related
I am making Conway's Game of Life using Java Swing. I am using a 2D arrays of buttons to paint the cells. The problem I have right now is that I implemented a button which will clear the pattern or patterns drew manually o randomly and also while the timer is running. I want to clear or restart the 2D array of buttons but when I try to draw it doesn't work.
Here I have the Action Listeners for each button
//Starts the timer
Starter.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
cells = new JButton[size][size];
}
});
//Stops the timer
Stop.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.stop();
}
});
//Clears de buttons which are painted
clear.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(Univers[i][j]){
cells[i][j].setBackground(Color.BLACK);
}
}
}
Arrays.fill(cells, null);
}
});
All the code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class GameOfLife extends JFrame{
int size=15;
int time=0;
boolean Univers[][];
JButton cells[][];
//int countentry=0;
//List<boolean[][]> Record;
JComboBox combobox;
public GameOfLife() {
//Record = new ArrayList<>();
setPreferredSize(new Dimension(600, 600));
pack();
setLayout(null);
/*combobox= new JComboBox();
combobox.addItem("Records");
combobox.addItem(time+"s");
*/
Random rnd=new Random();
Univers=new boolean[size][size];
cells=new JButton[size][size];
JPanel UniversPanel=new JPanel();
UniversPanel.setBounds(0,0,500,500);
UniversPanel.setLayout(new GridLayout(size,size));
////////////////////////////////////////////////////////////
//manual cell adding
MouseListener mouseListener = new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) { }
#Override
public void mouseReleased(MouseEvent e) { }
#Override
public void mousePressed(MouseEvent e) { }
#Override
public void mouseExited(MouseEvent e) { }
#Override
public void mouseEntered(MouseEvent e) {
JButton ch = (JButton) e.getSource();
//System.out.println(ch.getLocation(getLocation()));
int sizegrid = UniversPanel.getSize(getSize()).width / size;
int i = (ch.getLocation(getLocation()).x) / sizegrid;
int z = (ch.getLocation(getLocation()).y) / sizegrid;
//System.out.println("x= "+i);
//System.out.println("y= "+z);
if (!Univers[z][i] && SwingUtilities.isLeftMouseButton(e)) {
Univers[z][i] = true;
cells[z][i].setBackground(Color.GREEN);
}
if(Univers[z][i] && SwingUtilities.isRightMouseButton(e)) {
Univers[z][i]=false;
cells[z][i].setBackground(Color.BLACK);
}
}
};
///////////////////////////////////////////////////////////////////
for(int x=0;x<size;x++) {
for(int y=0;y<size;y++) {
/*if((x==0 && y==1 )|| (x==1 && y==1) || (x==2 && y==1)) {
Univers[x][y]=true;
}else {
Univers[x][y]=false;
}*/
Univers[x][y]=false;
//Univers[x][y]=rnd.nextInt(100)<20;
JButton btn=new JButton();
if(Univers[x][y]) {
btn.setBackground(Color.GREEN);
}else {
btn.setBackground(Color.BLACK);
}
UniversPanel.add(btn);
cells[x][y]=btn;
}
}
//Record.add(Univers);
//countentry+=1;
//System.out.print(Record.size());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int delay=100;//milliseconds
//int Timeup=5000/delay;
Timer timer=new Timer(delay,new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean[][] tempUnivers=new boolean[size][size];
//countentry+=1;
for(int x=0;x<size;x++) {
for(int y=0;y<size;y++) {
int count=Neigbours(x,y);
if(Univers[x][y]) {
if(count<2) {
tempUnivers[x][y]=false;
}
if(count==3 ||count==2) {
tempUnivers[x][y]=true;
}
if(count>3) {
tempUnivers[x][y]=false;
}
}else{
if(count==3)
tempUnivers[x][y]=true;
}
}
}
Univers=tempUnivers;
//Record.add(Univers);
//System.out.print(Record.size());
for(int x=0;x<size;x++) {
for(int y=0;y<size;y++) {
if(Univers[x][y]) {
cells[x][y].setBackground(Color.GREEN);
}else {
cells[x][y].setBackground(Color.BLACK);
}
}
}
/* if(countentry==Timeup) {
time+=5;
combobox.addItem(time+"s");
countentry=0;
}
*/
}
});
for(int x=0;x<size;x++) {
for(int y=0;y<size;y++) {
cells[x][y].addMouseListener(mouseListener);
}
}
//Slider
/*JPanel PanelSlider =new JPanel();
PanelSlider.setLayout(new GridLayout(0,2));
PanelSlider.setBounds(0,510,300,50);
JSlider slider = new JSlider(0,5,0);
slider.setPaintTrack(true);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(1);
slider.setMinorTickSpacing(1/10);
slider.setBounds(51, 88, 277, 52);
PanelSlider.add(combobox);
PanelSlider.add(slider);
*/
//Buttons
JPanel PanelButton =new JPanel();
PanelButton.setLayout(new GridLayout(0,3));
PanelButton.setBounds(0,510,200,50);
JButton Starter=new JButton("Start");
PanelButton.add(Starter);
JButton Stop=new JButton("Stop");
PanelButton.add(Stop);
JButton clear = new JButton("Clear");
PanelButton.add(clear);
//Starts the timer
Starter.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
cells = new JButton[size][size];
}
});
//Stops the timer
Stop.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.stop();
}
});
//Clears de buttons which are painted
clear.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(Univers[i][j]){
cells[i][j].setBackground(Color.BLACK);
}
}
}
Arrays.fill(cells, null);
}
});
//add(PanelSlider);
add(UniversPanel);
add(PanelButton);
setLocationRelativeTo(null);
setVisible(true);
}
int Neigbours(int i, int z) {
int count=0;
for(int x=i-1;x<=i+1;x++) {
for(int y=z-1;y<=z+1;y++) {
try {
if(Univers[x][y]) {
count++;
}
}catch(Exception e){
//System.out.println(e);
}
}
}
if(Univers[i][z])
count--;
return count;
}
public static void main(String[] args) {
new GameOfLife();
}
//Slider info https://docs.oracle.com/javase/tutorial/uiswing/components/slider.html
//Manual insert void ActionPerformed(ActionEvent ae){add jbutton ch = (Jbutton) ae.getSource();}
}
Please read the comment within the code to follow the changes made in the code and the recommendations:
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class GameOfLife extends JFrame{
//declare constants
private final int BOARD_SIZE=15,
ANIMATION_DELAY=100;//milliseconds;
private final Cell cells[][];
public GameOfLife() {
//setPreferredSize(new Dimension(600, 600)); let layout manager work out the size derived from children
//setLayout(null); avoid using null layouts and seting bounds manually
cells=new Cell[BOARD_SIZE][BOARD_SIZE];
JPanel universPanel=new JPanel();
//universPanel.setBounds(0,0,500,500); let the layout manager set bounds
universPanel.setLayout(new GridLayout(BOARD_SIZE,BOARD_SIZE));
//initialize all cells
for(int x=0;x<BOARD_SIZE;x++) {
for(int y=0;y<BOARD_SIZE;y++) {
Cell cell=new Cell();
universPanel.add(cell);
cells[x][y]=cell;
}
}
//move the animation code to a method for better readability
Timer timer=new Timer(ANIMATION_DELAY,e -> animate());
//Buttons
JPanel panelButton =new JPanel();
panelButton.setLayout(new GridLayout(0,3));
//panelButton.setBounds(0,510,200,50); let the layout manager set bounds
JButton starter=new JButton("Start");
panelButton.add(starter);
JButton stop=new JButton("Stop");
panelButton.add(stop);
JButton clear = new JButton("Clear");
panelButton.add(clear);
//Starts the timer
starter.addActionListener(e -> timer.start());
//Stops the timer
stop.addActionListener(e -> timer.stop());
//Resets all cells
clear.addActionListener(e -> {
for(int i = 0; i < BOARD_SIZE; i++){
for(int j = 0; j < BOARD_SIZE; j++){
cells[i][j].setUniverse(false);
}
}
//Arrays.fill(cells, null); no need to set to null.
});
//add(PanelSlider);
//add to appropriate BorderLayout positions
add(universPanel, BorderLayout.CENTER);
add(panelButton, BorderLayout.SOUTH);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); //pack after you added all components
setVisible(true);
}
private void animate(){
for(int x1=0;x1<BOARD_SIZE;x1++) {
for(int y1=0;y1<BOARD_SIZE;y1++) {
int count=numberOfNeigboursAround(x1,y1);
if(cells[x1][y1].isUniverse()) {
if(count<2) {
cells[x1][y1].setUniverse(false);
}else if//process next if only if previous one was false
(count==3 ||count==2) {
cells[x1][y1].setUniverse(true);
}else if(count>3) {
cells[x1][y1].setUniverse(false);
}
}else if(count==3) {
cells[x1][y1].setUniverse(true);
}
}
}
}
private int numberOfNeigboursAround(int x, int y) {
int count=0;
for(int xIndex=x-1;xIndex<=x+1;xIndex++) {
for(int yIndex=y-1;yIndex<=y+1;yIndex++) {
//make sure index is valid
if(xIndex >= BOARD_SIZE || xIndex < 0 || yIndex >= BOARD_SIZE || yIndex < 0) {
continue;
}
//don't process center cell
if(xIndex == x&&yIndex ==y) {
continue;
}
if(cells[xIndex][yIndex].isUniverse()) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
new GameOfLife();
}
}
//Introduce a Cell class with the needed properties. Since button functionality i snot
//use a JLabel can be used to represent a cell
class Cell extends JLabel{
private static Color BOARD = Color.BLACK, UNIVERSE = Color.GREEN, BORDER = Color.GRAY;
private static Dimension CELL_SIZE = new Dimension(30,30);
private boolean universe;
public Cell() {
super();
universe = false;
setOpaque(true);
setBorder(BorderFactory.createLineBorder(BORDER, 2));
setBackground(BOARD);
//add mouse listener. use MouseInputAdapter to implement only needed methods
addMouseListener( new MouseInputAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
mouseClicksHandler(e);
}
});
}
public boolean isUniverse() {
return universe;
}
public void setUniverse(boolean isUniverse) {
if (universe == isUniverse) return; //no change
universe = isUniverse;
setBackground(isUniverse ? UNIVERSE : BOARD);
repaint();
}
private void mouseClicksHandler(MouseEvent e){
if (SwingUtilities.isLeftMouseButton(e)) {
setUniverse(true);
}else //if first if is true no need to evaluate the second if
if( SwingUtilities.isRightMouseButton(e)) {
setUniverse(false);
}
}
//preferred size used by layout managers
#Override
public Dimension getPreferredSize() {
return CELL_SIZE;
}
}
I am making a simple maze game, and am trying to make a main menu for it. Basically, whenever a JButton is pressed, an ActionListener is called, and creates a new window with the game inside of it. inside the game, there is a KeyListener so that you can move around with the arrow keys. However, when I run it, the arrow keys do not work, and the button stays pressed. Here is the code for the MainMenu:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MainMenu extends JFrame
{
private int timer;
private JLabel Timer;
public MainMenu(String title, int width, int height){
JButton maze1 = new JButton("Maze 1");
JButton maze2 = new JButton("Maze 2");
JButton maze3 = new JButton("Maze 3");
JButton maze4 = new JButton("Maze 4");
JButton maze5 = new JButton("Maze 5");
JLabel instructions = new JLabel();
instructions.setText("Pick a maze!");
Timer = new JLabel();
Timer.setBounds(0, 150, 220, 20);
Timer.setText("0.0");
maze1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{playMaze(1);}catch(FileNotFoundException e){}
}
});
maze2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{playMaze(2);}catch(FileNotFoundException e){}
}
});
maze3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{playMaze(3);}catch(FileNotFoundException e){}
}
});
maze4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{playMaze(4);}catch(FileNotFoundException e){}
}
});
maze5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{playMaze(5);}catch(FileNotFoundException e){}
}
});
add(instructions);
add(maze1);
add(maze2);
add(maze3);
add(maze4);
add(maze5);
setTitle(title);
setLayout(new FlowLayout());
setSize(width, height);
timer = 0;
}
public void playMaze(int i) throws FileNotFoundException{
MazeCreator creator = new MazeCreator();
creator.createMaze("MAZE" + i + ".txt");
MazeRunner runner = new MazeRunner(creator.getMaze(), 0, 5, timer);
runner.run();
timer = runner.getTime();
Timer.setText("" + timer/10.0);
}
}
The MazeRunner:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class MazeRunner implements Runnable
{
private JFrame frame;
private Canvas canvas;
private BufferStrategy bufferStrategy;
private String[][] maze;
private int x;
private int y;
private boolean win;
private int timer;
public MazeRunner(String[][] Maze, int Y, int X, int t){
frame = new JFrame("Maze Game");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(220, 250));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 220, 250);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
canvas.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent evt) {
move(evt);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
maze = Maze;
x = X;
y = Y;
maze[y][x] = "#";
win = false;
timer = t;
}
public boolean checkWin(){return win;}
public void run(){
while(!win){
Paint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
timer++;
}
maze[y][x] = "#";
Paint();
try{Thread.sleep(1000);}catch(InterruptedException e){}
frame.dispose();
}
public void move(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
moveDown();
break;
case KeyEvent.VK_UP:
moveUp();
break;
case KeyEvent.VK_LEFT:
moveLeft();
break;
case KeyEvent.VK_RIGHT:
moveRight();
break;
}
}
public void moveUp(){
maze[y][x] = "-";
if(!(y > 0)){
}else if(maze[y - 1][x].equals("*")){
}else if(maze[y - 1][x].equals("#")){
y -= 1;
win = true;
}else{
y -= 1;
}
maze[y][x] = "#";
}
public void moveDown(){
maze[y][x] = "-";
if(!(y < 10)){
}else if(maze[y + 1][x].equals("*")){
}else if(maze[y + 1][x].equals("#")){
y += 1;
win = true;
}else{
y += 1;
}
maze[y][x] = "#";
}
public void moveLeft(){
maze[y][x] = "-";
if(!(x > 0)){
}else if(maze[y][x - 1].equals("*")){
}else if(maze[y][x - 1].equals("#")){
x -= 1;
win = true;
}else{
x -= 1;
}
maze[y][x] = "#";
}
public void moveRight(){
maze[y][x] = "-";
if(!(x < 10)){
}else if(maze[y][x + 1].equals("*")){
}else if(maze[y][x + 1].equals("#")){
x += 1;
win = true;
}else{
x += 1;
}
maze[y][x] = "#";
}
public void printMaze(){
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(int i = 0; i < 11; i++){
for(int e = 0; e < 11; e++){
System.out.print(maze[i][e] + " ");
}
System.out.println();
}
}
public void Paint() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, 500, 500);
Paint(g);
bufferStrategy.show();
}
protected void Paint(Graphics2D g) {
for(int i = 0; i < 11; i++){
for(int e = 0; e < 11; e++){
if(maze[i][e].equals("*")){
g.setColor(Color.BLACK);
g.fillRect(e*20, i*20, 20, 20);
}else if(maze[i][e].equals("#")){
g.setColor(Color.RED);
g.fillOval(e*20, i*20, 20, 20);
}else if(maze[i][e].equals("#")){
g.setColor(Color.BLUE);
g.fillRect(e*20, i*20, 20, 20);
}
}
}
g.setColor(Color.BLACK);
g.drawString("" + (timer / 10.0), 190, 240);
if(win){
g.setFont(new Font("Dialouge", Font.BOLD, 20));
g.drawString("YOU WIN!", 10, 240);
}
}
public String mazeToString(int y){
String toString = "";
for(int e = 0; e < 11; e++){
toString += maze[y][e] + " ";
}
return toString;
}
public int getTime(){return timer;}
}
And the MazeCreator:
import java.util.*;
import java.io.*;
public class MazeCreator
{
private String[][] maze;
private int xPos;
private int yPos;
private File file;
private Scanner input;
public MazeCreator(){
maze = new String[11][11];
}
public String[][] getMaze(){return maze;}
public void createMaze(String File) throws FileNotFoundException{
input = new Scanner(new File("C:/Users/Owner/Maze/" + File));
for(int i = 0; i < 11; i++){
for(int e = 0; e < 11; e++){
maze[i][e] = input.next();
//System.out.print(maze[i][e] + " ");
}
input.nextLine();
//System.out.println();
}
}
}
Any help that anyone could give would be great.
I'm attempting to make Frogger in java for a school project but I'm having a lot of difficulties setting up KeyListener for the actual frog character.
I've tried setting up key bindings, requesting focus for the JPanel and JFrame, moving around where the character is initiated but nothing has seemed to work. This is the remnants of my attempts.
This is the program that runs my game.
import javax.swing.*;
public class Frogger
{
JFrame frame = new JFrame("Frogger");
CPT c = new CPT();
public Frogger()
{
frame.setBounds(0,0,700,500);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(c);
}
public static void main(String[] args){
new Frogger();
}
}
The main game
public CPT() {
setLayout(new BorderLayout());
label = new JLabel("Frogger");
frame1 = new JFrame("Main");
label.setFont(new Font("Serif", Font.BOLD,50));
label.setBounds(275,10,250,250);
button1 = new JButton("PLAY");
button1.setBounds(300,350,100,50);
button1.setOpaque(false);
button1.setVisible(true);
this.setOpaque(false);
this.setLayout(null);
this.add(label);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame1.setSize(700,500);
frame1.setResizable(false);
frame1.setFocusable(false);
button1.setVisible(false);
frame1.add(new TrainCanvas());
frame1.add(p1);
p1.requestFocus();
}
});
this.add(button1); }
This is the TrainCanvas class that Draws the cars in the games as well as the frog
class TrainCanvas extends JComponent
{
private int lastX = 0;
private int lastX_1 = 0;
private int lastX_2 = 0;
public TrainCanvas() {
Thread animationThread = new Thread(new Runnable() {
public void run() {
while (true) {
repaint();
try {Thread.sleep(10);} catch (Exception ex) {}
}
}
});
animationThread.start();
}
public void paintComponent(Graphics g) {
Graphics2D gg = (Graphics2D) g;
//Draws Train 1
int w = getWidth();
int h = getHeight();
int trainW_1 = 100;
int trainH_1 = 5;
int trainSpeed_1 = 3;
int x = lastX + trainSpeed_1;
if (x > w + trainW_1) {
x = -trainW_1;
}
gg.setColor(Color.BLACK);
gg.fillRect(x, h/2 + trainH_1, trainW_1, trainH_1);
lastX = x;
Graphics2D g3 = (Graphics2D) g;
frog = new Rectangle(f_x,f_y,25,25);
g3.fill(frog);
g3.setColor(Color.GREEN);
}
}
Finally, the Key Listener
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()== KeyEvent.VK_UP)
{
CPT.f_y -= 100;
repaint();
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT)
{
CPT.f_x += 100;
repaint();
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
CPT.f_x -= 100;
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
CPT.f_y += 100;
repaint();
}
else {}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
The program runs perfectly fine without giving me any errors, which is making this troublesome. Whenever it gets to the main game window, none of the keys seem to work.
I am trying to make a basic map where pressing a button moves a character.
I am using Netbeans and so far it's going smoothly! Except for trying to remove a JLabel from a JPanel and then add a new JLabel to it.
Here is my full code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class startMap extends JFrame {
public int locX = 1;
public int locY = 1;
ImageIcon tile = new ImageIcon("src/tile.png");
JLabel tileLabel = new JLabel(tile);
ImageIcon berry = new ImageIcon("src/berry.png");
JLabel berryLabel = new JLabel(berry);
ImageIcon blank = new ImageIcon("src/blank.png");
JLabel blankLabel = new JLabel(blank);
JLabel testL = new JLabel("LOL");
JPanel[][] map = new JPanel[7][7];
public startMap() {
setBackground(Color.BLACK);
setFocusable(true);
keyHandler kh = new keyHandler();
addKeyListener(kh);
mapGUI();
}
public void mapGUI() {
JPanel mainP = new JPanel();
mainP.setBackground(Color.BLACK);
mainP.setLayout(new FlowLayout());
for (int x = 0; x < 7; x++) {
for (int i = 0; i < 7; i++) {
map[x][i] = new JPanel();
System.out.println("1");
blankLabel = new JLabel(blank);
map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
map[x][i].add(blankLabel);
}
}
for (int x = 1; x < 6; x++) {
for (int i = 1; i < 6; i++) {
System.out.println("2");
map[x][i].removeAll();
revalidate();
repaint();
tileLabel = new JLabel(tile);
map[x][i].add(tileLabel);
revalidate();
repaint();
}
}
System.out.println("3");
map[locX][locY].removeAll();
revalidate();
repaint();
map[locX][locY].add(berryLabel);
revalidate();
repaint();
for (int x = 0; x < 7; x++) {
for (int i = 0; i < 7; i++) {
mainP.add(map[x][i]);
}
}
add(mainP);
}
#Override
public void paint(Graphics g) {
super.paint(g);
}
private class keyHandler extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
locY+=1;
map[locX][locY].removeAll();
revalidate();
repaint();
map[locX][locY].add(berryLabel);
revalidate();
repaint();
}
}
}
}
Here is what changes the squares when the user clicks Right.
Here is the KeyAdapter code:
private class keyHandler extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
locY+=1;
map[locX][locY].removeAll();
revalidate();
repaint();
map[locX][locY].add(berryLabel);
revalidate();
repaint();
}
}}
NOTE: the system out is just a debugging method I use to check what's being called when.
So when I run it looks like this
Move to the right and revalidate $ repaint:
Why does the box located in 1,1 go to being gray?
Help figuring out how to make the boxes stay with a white square instead of turning back to gray.
----------------------------SSCCE----------------------------------
Use the full code above
and this is the main class:
import javax.swing.*;
public class TestGame extends JFrame {
public static void main(String[] args) {
startMap sm = new startMap();
sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sm.setVisible(true);
sm.setSize(380,415);
sm.setResizable(false);
}
}
Fixed Version:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class startMap extends JFrame {
public int locX = 1;
public int locY = 1;
ImageIcon tile = new ImageIcon("src/tile.png");
JLabel tileLabel = new JLabel(tile);
ImageIcon berry = new ImageIcon("src/berry.png");
JLabel berryLabel = new JLabel(berry);
ImageIcon blank = new ImageIcon("src/blank.png");
JLabel blankLabel = new JLabel(blank);
JLabel testL = new JLabel("LOL");
JPanel[][] map = new JPanel[7][7];
public startMap() {
setBackground(Color.BLACK);
setFocusable(true);
keyHandler kh = new keyHandler();
addKeyListener(kh);
mapGUI();
}
public void mapGUI() {
JPanel mainP = new JPanel();
mainP.setBackground(Color.BLACK);
mainP.setLayout(new FlowLayout());
for (int x = 0; x < 7; x++) {
for (int i = 0; i < 7; i++) {
map[x][i] = new JPanel();
System.out.println("1");
blankLabel = new JLabel(blank);
map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
map[x][i].add(blankLabel);
}
}
for (int x = 1; x < 6; x++) {
for (int i = 1; i < 6; i++) {
System.out.println("2");
map[x][i].removeAll();
tileLabel = new JLabel(tile);
map[x][i].add(tileLabel);
revalidate();
repaint();
}
}
System.out.println("3");
map[locX][locY].removeAll();
map[locX][locY].add(berryLabel);
revalidate();
repaint();
for (int x = 0; x < 7; x++) {
for (int i = 0; i < 7; i++) {
mainP.add(map[x][i]);
}
}
add(mainP);
}
#Override
public void paint(Graphics g) {
super.paint(g);
}
private class keyHandler extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
berryLabel = new JLabel(berry); //THIS
tileLabel = new JLabel(tile); //And THIS had to be RE initialized idk why.
map[locX][locY].removeAll();
map[locX][locY].add(tileLabel);
locY += 1;
map[locX][locY].removeAll();
map[locX][locY].add(berryLabel);
revalidate();
repaint();
}
}
}
}
Should be called after adding / removing
revalidate();
repaint();
Don't call repaint() from paint()
I've built a simple animation program that moves a box to a direction depending on the
arrow key pressed (similar to snake), in JFrame using KeyListener and ActionListener. but I've noticed that if I start the application and I move the mouse the application wont continue to check what arrow key was pressed and which direction to move to.
Can some one explain this to me, and is it because I need to disable something involving mouse events?
heres the code:
public class gui extends JPanel implements ActionListener, KeyListener{
Timer tm = new Timer(5, this);
int x = 300, y = 178, velx = 0, vely = 0;
public gui() {
tm.start();
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(x, y, 50, 30);
}
#Override
public void keyPressed(KeyEvent e) {
keys(e);
}
public void keys(KeyEvent e) {
int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT)
{
velx = -1;
vely = 0;
}
if (c == KeyEvent.VK_UP)
{
velx = 0;
vely = -1;
}
if (c == KeyEvent.VK_RIGHT)
{
velx = 1;
vely = 0;
}
if (c == KeyEvent.VK_DOWN)
{
velx = 0;
vely = 1;
}
}
public void borders(ActionEvent e) {
if (x < 0) {
velx = 0;
x = 0;
JOptionPane
.showMessageDialog(null, "you hit the borders you lost!");
System.exit(0);
}
if (x > 530) {
velx = 0;
x = 530;
JOptionPane
.showMessageDialog(null, "you hit the borders you lost!");
System.exit(0);
}
if (y < 0) {
velx = 0;
y = 0;
JOptionPane
.showMessageDialog(null, "you hit the borders you lost!");
System.exit(0);
}
if (y > 330) {
velx = 0;
y = 330;
JOptionPane
.showMessageDialog(null, "you hit the borders you lost!");
System.exit(0);
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
x += velx;
y += vely;
repaint();
borders(e);
}
}
I don't know what your problem is. I used your code and had no problem.
Here is what I used:
public gui()
{
addKeyListener(this);
setFocusable(true);
}
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
if (c == KeyEvent.VK_LEFT)
{
velx = -1;
vely = 0;
}
if (c == KeyEvent.VK_UP)
{
velx = 0;
vely = -1;
}
if (c == KeyEvent.VK_RIGHT)
{
velx = 1;
vely = 0;
}
if (c == KeyEvent.VK_DOWN)
{
velx = 0;
vely = 1;
}
}
public void actionPerformed(ActionEvent e)
{
x += velx;
y += vely;
repaint();
borders(e);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("gui");
frame.add(new gui());
frame.setVisible(true);
frame.setSize(600, 400);
}
}
I clicked the mouse and moved it before I started the program, then I used the keys; it works fine. I would switch to KeyBindings though.