I am trying to make the sprite stop continuously moving. Right now, the code makes the sprite move in the direction indicated by the user through arrow keys without stopping. I want to press an arrow key and make the sprite move once (ex. 10 steps only) in that direction.
public class Contents extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(100, this);
private Image man;
int x=0, y=0;
public Contents() {
super.setDoubleBuffered(true);
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
#Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
super.setBackground(Color.white);
ImageIcon ii = new ImageIcon("guy.png");
man = ii.getImage();
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(man, x, y, this);
}
double xv = 0;
double yv = 0;
public void move() {
x += xv;
y += yv;
}
public void up() {
yv = -1;
xv = 0;
}
public void down() {
yv = 1;
xv = 0;
}
public void left () {
xv = -1;
yv = 0;
}
public void right () {
xv = 1;
yv = 0;
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
//Collision for left and right walls
if (x==0) {
xv = 1;
}
else if (x==900-240) {
xv = -1;
}
// Collision for top and bottom walls
if (y==0) {
yv = 1;
}
else if (y==600-320) {
yv = -1;
}
move();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
up();
}
if (code == KeyEvent.VK_DOWN) {
down();
}
if (code == KeyEvent.VK_LEFT) {
left();
}
if (code == KeyEvent.VK_RIGHT) {
right();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
It works when I delete the boundaries under ActionEvent. The boundaries prevent the sprite from going off screen. But I want to keep the boundaries and control the sprite too.
I figured it out. Under keyReleased, set xv=0. Then when you release the arrow key, the sprite will stop moving.
Related
As the title states, I am trying to detect a mouse hover over an object that is not a JComponent.
Right now I have a window with a green JPanel. When you left-click on this JPanel you create a point.
What I am trying to do is to have extra information displayed when I hover over these points. However, I have no idea how to even begin detecting if I am hovering my mouse over a point. I tried looking into the MouseListener interface but I could not find any examples of people using MouseListener with an object. I have only seen people use MouseListener with JComponents. I would preferably like to have this mouse hover detection code in my Point class if possible to keep my code clean.
JPanel Code
class Map extends JPanel implements MouseListener {
public static ArrayList<Point> points = new ArrayList<Point>(); //array for the points
public Map() {
this.setBackground(Color.green);
this.setPreferredSize(new Dimension(1280, 720));
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
drawPoints(graphics);
}
private void drawPoints(Graphics2D graphics) {
for(int i = 0; i < points.size(); i++) {
points.get(i).drawPoint(graphics);
}
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) { //Left Click
points.add(new Point(e.getX(), e.getY()));
repaint();
}
else if(e.getButton() == MouseEvent.BUTTON3) { //right click
for(int i = points.size() - 1; i >= 0; i--) { //loop backwards so if points overlap remove the one on top first
Point current = points.get(i);
if( Math.abs( e.getX() - current.x ) < current.size/2 && Math.abs( e.getY() - current.y ) < current.size/2 ) {
points.remove(i);
repaint();
break;
}
}
}
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
Point Code
public class Point {
public int x, y;
public int size = 10;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this.x = 0;
this.y = 0;
}
public void drawPoint(Graphics2D graphics) {
graphics.setPaint(Color.black);
graphics.setStroke(new BasicStroke(5));
graphics.drawOval(x - (size/2), y - (size/2), size, size);
graphics.setPaint(Color.red);
graphics.fillOval(x - (size/2), y - (size/2), size, size);
}
public void drawInfo(Graphics2D graphics) {
graphics.drawString("test", x, y);
}
}
I had the same issue with a Packet-Tracer-Like program, where I drew rects.
If they are just points, I would check if the mouse cords are the same as the point cords when the mouse is moved.
#Override
public void mouseMoved(MouseEvent e) {
entered = false;
if(point.x == e.getX() && point.y == e.getY()){
entered = true;
}
}
If although, like in my case, the drawn object has a width and a height, it gets messier.
#Override
public void mouseMoved(MouseEvent e) {
entered = false;
if((e.getX() <= point.x+width) && (e.getX() >= point.x)){
if((e.getY() <= point.y+height) && (e.getY() >= point.y)){
entered = true;
}
}
}
I am having an issue with the command super.paintComponents(g); when updating a JFrame. The issue I am having is that the image I am loading is blinking while the code is running. I am pretty sure that the code is double buffering and have not found any helpful resource online to solve this issue. This is my first question so please tolerate any formatting errors.
Here is my entire code (snippets below):
public class GAME extends JFrame implements KeyListener, MouseMotionListener {
int mouseX, mouseY;
public static ArrayList<Images> images = new ArrayList<Images>();
public static String lastKeyPressed = null;
public GAME() {
this.addMouseMotionListener(this);
this.addKeyListener(this);
}
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_LEFT) {
lastKeyPressed = "LEFT";
} else if (e.getKeyCode() == e.VK_RIGHT) {
lastKeyPressed = "RIGHT";
} else if (e.getKeyCode() == e.VK_UP) {
lastKeyPressed = "UP";
} else if (e.getKeyCode() == e.VK_DOWN) {
lastKeyPressed = "DOWN";
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
#Override
public void paint(Graphics g) {
super.paintComponents(g);
for (int i = 0; i < images.size(); i++) {
g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
}
}
public static void main(String[] args) throws IOException, InterruptedException {
GAME frame = new GAME();
Dimension dim = new Dimension(800, 600);
frame.setPreferredSize(dim);
frame.setSize(dim);
frame.setResizable(false);
frame.setTitle("GAME");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Images test = new Images("unnamed.png");
images.add(test);
while (true) {
if (lastKeyPressed == "LEFT") {
test.xpos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "RIGHT") {
test.xpos += 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "UP") {
test.ypos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "DOWN") {
test.ypos += 5;
lastKeyPressed = null;
}
frame.repaint();
Thread.sleep(100);
}
}
}
Here is the images class:
public class Images {
String name;
BufferedImage img;
int xpos;
int ypos;
public Images (String Name) throws IOException{
name = Name;
xpos = 40;
ypos = 90;
System.out.println(name);
System.out.println("PATH: " + GAME.class.getResource(name));
URL file = getClass().getClassLoader().getResource(name);
img = ImageIO.read(file);
}
}
What you probably only want to see is the following:
My paint method:
#Override
public void paint(Graphics g) {
super.paintComponents(g);
for (int i = 0; i < images.size(); i++) {
g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
}
}
And the block of code in the main class that is running the game:
Images test = new Images("unnamed.png");
images.add(test);
while (true) {
if (lastKeyPressed == "LEFT") {
test.xpos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "RIGHT") {
test.xpos += 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "UP") {
test.ypos -= 5;
lastKeyPressed = null;
} else if (lastKeyPressed == "DOWN") {
test.ypos += 5;
lastKeyPressed = null;
}
frame.repaint();
Thread.sleep(100);
}
(I added the Thread.sleep because it seemed to decrease the rate at which the blinking occurred)
Here is a gif of what is happening (I just used the first Google result for 'test' for the image):
http://i.imgur.com/WjLCSvu.gif?1
I appreciate any help and general suggestions to better my code. Thank you.
Don't override paint of top level containers like JFrame there aren't double buffered, instead, use a JPanel and override its paintComponent method. JPanel is double buffered by default.
Then, add the panel to what ever container you want
Don't forget to call it's super method (super.paintComponent) before you do any custom painting.
Take a closer look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
This is just one reason why you shouldn't (extend from JFrame and) override paint of top level containers, take a look at
How can I set in the midst?
Graphics rendering in title bar
Java JFrame .setSize(x, y) not working?
How to get the EXACT middle of a screen, even when re-sized
for some more
I've switched from KeyListeners to KeyBindings as instructed, however they still seem to do nothing. My keybinds are set up as to allow the left and right arrow keys to call a setDx() method in paddle.java which instructs the move() method to move the paddle.
gamePanel.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gamePanel extends JPanel implements ActionListener {
paddle Paddle;
boolean ingame = true;
int delay = 1000;
Timer timer;
JLabel text = new JLabel("stuff here");
InputMap im = this.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = this.getActionMap();
public gamePanel() {
setBackground(Color.WHITE);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
add(text);
text.setBounds(100, 100, 200, 300);
timer = new Timer(delay, this);
Paddle = new paddle();
timer.start();
}
#Override
public void paint(Graphics g) {
super.paint(g);
if (ingame) {
g.drawImage(Paddle.getImage(), Paddle.getX(), Paddle.getY(),
Paddle.getWidth(), Paddle.getHeight(), this);
}
}
#Override
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == timer) {
Paddle.move();
validate();
repaint();
}
}
public class ArrowAction extends AbstractAction {
private String cmd;
public ArrowAction(String cmd) {
this.cmd = cmd;
}
#Override
public void actionPerformed(ActionEvent e) {
if (cmd.equalsIgnoreCase("LeftArrow")) {
Paddle.setDx(-20);
} else if (cmd.equalsIgnoreCase("RightArrow")) {
Paddle.setDx(20);
}
}
}
/*
#Override
public void keyPressed(KeyEvent ke) {
int KeyCode = ke.getKeyCode();
if (KeyCode == KeyEvent.VK_LEFT) {
text.setText("key pressed");
Paddle.setDx(-20);
}
if (KeyCode == KeyEvent.VK_RIGHT) {
Paddle.setDx(20);
}
}
#Override
public void keyReleased(KeyEvent ke) {
int KeyCode = ke.getKeyCode();
if (KeyCode == KeyEvent.VK_LEFT) {
Paddle.setDx(0);
}
if (KeyCode == KeyEvent.VK_RIGHT) {
Paddle.setDx(0);
}
}
#Override
public void keyTyped(KeyEvent ke) {
}
*/
}
Paddle.java:
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class paddle {
int dx = 0;
int x, y;
int height, width;
Image image;
public paddle() {
ImageIcon ii = new ImageIcon("src/Paddle.png");
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
//dx = 20;
resetState();
}
public void setDx(int z) {
dx = z;
}
public void move() {
x += dx;
if (x <= 2) {
x = 2;
}
if (x >= (640 - getWidth())) {
x = (640 - getWidth());
}
}
public void resetState() {
x = 250;
y = 375;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
Image getImage() {
return image;
}
Rectangle getRect() {
return new Rectangle(x, y, image.getWidth(null), image.getHeight(null));
}
}
If your KeyListener methods are not being called I suspect it's because the correct component does not have focus. It's difficult sometimes to manage what component has focus especially in a game, so I would suggest switching over to using key bindings which doesn't require components to have focus.
You never add the corresponding actions you the ActionMap
am.put("LeftArrow", new ArrowAction("LeftArrow"));
am.put("RightArrow", new ArrowAction("RightArrow"));
Also, if you don't repaint() in the actioPerformed of the ArrowAction, you won't see it update immediately, until repaint() is called by the Timer, which isn't very long, but still a miniscule delay.
I want to creat a circle in on a panel which appears and dissapears every 2 seconds.
Here is that i have:
public class Board extends JPanel implements ActionListener {
private final int DELAY = 2000;
private Timer timer;
/*
* constructor
*/
public Board() {
setFocusable(true);
initGame();
}
/*
* initialize board
*/
public void initGame() {
timer = new Timer(DELAY, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.gray);
// draw an oval starting at 20,20 with a width and height of 100 and
// fill it
g.drawOval(20, 20, 100, 100);
g.fillOval(20, 20, 100, 100);
g.dispose();
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
thank you guys. Now I wanna control my circle. But again something is wrong and it is not moving as I want.
here are new methods:
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private Timer timer;
public int x = 20;
public int y = 20;
public int x2 = 100;
public int y2 = 100;
...
public void paint(Graphics g) {
super.paint(g);
if (drawCircle) {
g.setColor(Color.gray);
// draw an oval starting at 20,20 with a width and height of 100 and
// fill it
g.drawOval(x, y, x2, y2);
g.fillOval(x, y, x2, y2);
}
// removes native non-Java recourses
g.dispose();
}
public void move() {
if (left) {
x -= 5;
}
if (right) {
x += 5;
}
if (up) {
y -= 5;
}
if (down) {
y += 5;
}
}
#Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!right)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!left)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP) && (!down)) {
up = true;
right = false;
left = false;
}
if ((key == KeyEvent.VK_DOWN) && (!up)) {
down = true;
right = false;
left = false;
}
}
}
/****/
Solved , i just added
addKeyListener(new TAdapter());
to my Board constructor!
You just need to have your Timer modify some state. Try something like this:
private boolean drawCircle = false;
public void actionPerformed(ActionEvent e) {
drawCircle = !drawCircle;
repaint();
}
public void paintComponent(Graphics g) {
//...
if ( drawCircle ) {
g.setColor(Color.gray);
//...
}
}
Aren't you supposed to formulate the question... as a question?
Anyway, add a boolean to your class, toggle it on each action event, and paint the oval only if it is true.
Edit/Notes:
- Override paintComponent instead of paint
- Don't dispose of a Graphics you haven't created. Either drop the line, or use g.create() to make a copy. See http://java.sun.com/products/jfc/tsc/articles/swing2d/ for details.
I am trying to make a space ship (PlayerShip.gif) in my game move left, right, up, and down when the coresponding keys are pressed. I know i need an keyboardListener but im having issues figuring out where it goes and how it is actually implimented. my code is as follows.
public class GamePanel extends JPanel implements KeyListener {
Timer timer1;
Timer timer2;
ArrayList<Ships> ship;
int x;
int y;
double speed;
int size;
int shipxCoord;
int shipyCoord;
int shipHeight;
int shipWidth;
int shipRise;
int shipRun;
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
Image enemyShip1;
Image enemyShip2;
Image playerShip;
ImageIcon ic = new ImageIcon("Ship.gif");
ImageIcon ic2 = new ImageIcon("Ship2.gif");
int width = ic.getIconWidth();
int height = ic.getIconHeight();
Image background = Toolkit.getDefaultToolkit().createImage("background.jpg");
public GamePanel(int size, double speed) {
super();
x = 450;
y = 510;
ship = new ArrayList<Ships>();
// call timer for 1st type of enemy Ship with a delay of 5 seconds between ships
enemy1Timer(5);
// call timer for 2nd type of enemy Ship with a delay of 3 seconds between ships
enemy2Timer(3);
this.size = size;
this.speed = speed;
enemyShip1 = new ImageIcon("ship.gif").getImage();
enemyShip2 = new ImageIcon("ship2.gif").getImage();
playerShip = new ImageIcon("playerShip.gif").getImage();
// this.add(image);
} // end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
for (Ships s : ship) {
s.paintComponent(g);
}
g.drawImage(playerShip, x, y, this);
//g.drawImage(enemyShip2,x,y,this);
}// end method paintComponent
public void move() {
for (Ships s : ship) {
s.moveship(this);
// s.detectCollision(ship);
}
if (left) {
x -= speed;
}
if (right) {
x += speed;
}
if (up) {
y -= speed;
}
if (down) {
y += speed;
}
if (x > getWidth() - size) {
x = getWidth() - size;
}
if (x < 0) {
x = 0;
}
if (y > getHeight() - size) {
y = getHeight() - size;
}
if (y < 0) {
y = 0;
}
}// end method move
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == e.VK_RIGHT) {
right = true;
}
if (e.getKeyCode() == e.VK_UP) {
up = true;
}
if (e.getKeyCode() == e.VK_DOWN) {
down = true;
}
} // end method keyPressed
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == e.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == e.VK_UP) {
up = false;
}
if (e.getKeyCode() == e.VK_DOWN) {
down = false;
}
}
public void enemy1Timer(int seconds) {
timer1 = new Timer();
timer1.schedule(new ShipSwarm1(), 1000, seconds * 1000);
}// end method enemy1Timer
class ShipSwarm1 extends TimerTask {
public void run() {
for (int Wave = 1; Wave > 0; Wave--) {
Ships enemy = new Ships(ic, enemyShip1);
ship.add(enemy);
}// end for
}// end method run
}// end class Enemy1Swarm
public void enemy2Timer(int seconds) {
timer2 = new Timer();
timer2.schedule(new ShipSwarm2(), 3000, seconds * 1000);
}// end method enemy1Timer
class ShipSwarm2 extends TimerTask {
public void run() {
for (int Wave = 1; Wave > 0; Wave--) {
Ships enemy = new Ships(ic2, enemyShip2);
ship.add(enemy);
}// end for
}// end method run
}
}
Take a look at this link for a game impelemented in Java and it does exactly what you are looking for. Key presses - up left right and down keys.
Its the famous 15-game
An excerpt from that,
private void processKeys(){
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e){
if(e.getID() == KeyEvent.KEY_PRESSED){
handleKeyPress(e.getKeyCode());
if(areThingsInPlace() && !dialogShown){
dialogShown = true;
JOptionPane.showMessageDialog(null,"Congratulations!!! YOU WIN!!");
System.exit(1);
}
}
return false;
}
});
}
The handleKeyPress() method for handling the arrow keys
private void handleKeyPress(int keyCode) {
int emptyIndex = findEmptyIndex();
int x = emptyIndex/SIZE;
int y = emptyIndex%SIZE;
switch (keyCode) {
case 37://LEFT KEY
if(y==SIZE-1) return;
doSwap(x,y,x,y+1);
break;
case 38://UP KEY
if(x==SIZE-1) return;
doSwap(x,y,x+1,y);
break;
case 39://RIGHT KEY
if(y==0) return;
doSwap(x,y,x,y-1);
break;
case 40://DOWN KEY
if(x==0) return;
doSwap(x,y,x-1,y);
break;
}
}
Also consider adding conrol over all eight (semi-) cardinal directions, as shown in this game. For greater flexibility, consider actions and key bindings, discussed here.