Sorry for the Title! don't know how else to ask this question..
I use the command revalidate()
, and the window gets clear.
if I want to add KeyListener
on window what should I put?
if the window gets windowDeiconified
that event, the KeyListener works fine.
So my main question is what else codes need to (re-work) the keyListener without re-opening it?
What should I use indeed of revalidate()
and not re-opening the window to get my keyListener work?
I tried to google it but I really don't know how to search t hat issue.. also haven't seen anyone talking about that... I don't know what I'm missing here.
Down below is the Code and the Classes that I'm using on.
Starting Class:
package MainPackage;
import javax.swing.JFrame;
public class Starting_Window {
public static JFrame frame = new JFrame("The Simple Game By Erick");
public static GameFrameMenu menuG = new GameFrameMenu();
public static PlayingClass game;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800/4*3);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.add(menuG);
}
}
Menu Class:
package MainPackage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameFrameMenu extends JPanel implements ActionListener {
public static Timer menuTimer;
public GameFrameMenu() {
setFocusable(true);
setLayout(null);
color = new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
menuTimer = new Timer(10,this);
menuTimer.start();
JButton btnStart = new JButton("Start Game!");
btnStart.setBounds(285, 180, 200, 40);
btnStart.setBackground(Color.BLACK);
btnStart.setForeground(Color.WHITE);
add(btnStart);
JButton btnOptions = new JButton("Options!");
btnOptions.setBounds(285, 240, 200, 40);
btnOptions.setBackground(Color.BLACK);
btnOptions.setForeground(Color.WHITE);
add(btnOptions);
JButton btnAboutUs = new JButton("About Us!");
btnAboutUs.setBounds(285, 300, 200, 40);
btnAboutUs.setBackground(Color.BLACK);
btnAboutUs.setForeground(Color.WHITE);
add(btnAboutUs);
JButton btnExit = new JButton("Exit!");
btnExit.setBounds(285, 360, 200, 40);
btnExit.setBackground(Color.BLACK);
btnExit.setForeground(Color.WHITE);
add(btnExit);
/////////////////////////////////////////////////
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Starting_Window.game = new PlayingClass();
menuTimer.stop();
Starting_Window.menuG.removeAll();
Starting_Window.menuG.repaint();
Starting_Window.frame.repaint();
Starting_Window.frame.add(Starting_Window.game);
Starting_Window.game.setFocusable(true);
Starting_Window.game.repaint();
Starting_Window.frame.remove(Starting_Window.menuG);
Starting_Window.frame.revalidate();
}
});
btnOptions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnAboutUs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
Font HEADER = new Font("Verdana", Font.BOLD, 35);
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(HEADER);
g2d.drawString("The Simple Game Here By Erick ", 90, 145);
setBackground(color);
pushTimer();
drawEffects(g2d);
}
public int limiterEffect = 0;
public void drawEffects(Graphics2D g2d) {
limiterEffect++;
if(limiterEffect<15) {
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
g2d.setColor(getRandomColor(color));
g2d.fillOval(rand.nextInt(1000), rand.nextInt(1000), 25, 25);
}
if(limiterEffect==30) {
limiterEffect = 0;
}
}
static int counterTimer = 0, counterTimerDelay = 50;
static int canDisplay = 0;
public void pushTimer() {
counterTimer++;
if(counterTimer==counterTimerDelay) {
color = getRandomColor(color);
counterTimer = 0;
counterTimerDelay = rand.nextInt(200)+50;
}
}
public Color getRandomColor(Color c) {
int R,G,B;
R = rand.nextInt(255);
G = rand.nextInt(255);
B = rand.nextInt(255);
c = new Color(R,G,B);
return c;
}
static Random rand = new Random();
static Color color;
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
Playing Class that has been called from MenuClass:
package MainPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PlayingClass extends JPanel implements ActionListener {
Timer gameTimer;
Player player;
public PlayingClass() {
setFocusable(true);
setBackground(new Color(GameFrameMenu.rand.nextInt(255),GameFrameMenu.rand.nextInt(255),GameFrameMenu.rand.nextInt(255)));
gameTimer = new Timer(10,this);
gameTimer.start();
player = new Player(200,200);
addKeyListener(new KeyAdapt(player));
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
player.draw(g2d);
}
@Override
public void actionPerformed(ActionEvent e) {
player.update();
repaint();
}
}
Entity class:
package MainPackage;
import java.awt.Graphics2D;
public class Entity {
int x, y;
public Entity(int x, int y) {
this.x = x;
this.y = y;
}
public void update() {
}
public void draw(Graphics2D g2d) {
}
}
Player class that extends Entity Class:
package MainPackage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Player extends Entity {
int velY = 0, velX = 0;
public Player(int x, int y) {
super(x, y);
}
public void update() {
x += velX;
y += velY;
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getPlayerImg(), x, y, null);
}
public Image getPlayerImg() {
ImageIcon i = new ImageIcon("C:\\Java Pictures\\baseball pictures\\mini_player.png");
return i.getImage();
}
public void keyPressed(KeyEvent e) {
System.out.println("Something happened");
}
public void keyReleased(KeyEvent e) {
}
}
and finally the KeyAdapt class the is (sync) with Player class:
package MainPackage;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyAdapt extends KeyAdapter {
Player p;
public KeyAdapt(Player player) {
p = player;
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
}
So the issue here is, when i press the Start Button, The Screen Gets clear, i can see whatever happens in PlayingClass, but the KeyListener is not working. To make that work I have to minimize and deiconified the window in order to enable the KeyListener. So i hope i explain it better and you can help me.