I'm kind of new to java.im trying to write a simple game which it has a player and enemies and when you press some keys like "space", the player most shoot a bullet in right direction but i don't know how to render bullet in game. Actually it doesn't appear and doesn't move in Game.
Shooting system
package com.company;
import javax.swing.*;
import java.awt.*;
public class Bullet {
GamePanel panel;//Game panel
Rectangle hitBox;//Bullet
public int x,y,width,height;
boolean Shoot;
public Bullet(int x , int y ,GamePanel panel)
{
this.panel = panel;
this.x=x;
this.y=y;
height=15;
width=15;
hitBox = new Rectangle(x,y,width,height);
}
public void set()
{
if(Shoot) // where I have problem which Bullet doesn't move or doesn't appear
move();
}
public void move()
{
x=x+10;
}
public void draw(Graphics2D gtd)
{
gtd.setColor(Color.RED);
gtd.fillRect(x,y,width,height);
}
}
Game Panel which sets Game Objects
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class GamePanel extends JPanel implements ActionListener {
Player player;
Timer gameTimer;
Bullet bullet;
public GamePanel(){
player = new Player(400,300,this);
bullet = new Bullet(player.x, player.y, this);
gameTimer = new Timer();
gameTimer.schedule(new TimerTask() {
#Override
public void run() {
player.set();
bullet.move();
repaint();
}
},100,15);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D gtd = (Graphics2D) g;
player.draw(gtd);
bullet.draw(gtd);
}
void keyPressed(KeyEvent e)
{
if(e.getKeyChar()=='w')
player.keyUp=true;
if(e.getKeyChar()=='a')
player.keyLeft=true;
if(e.getKeyChar()=='s')
player.keyDown=true;
if(e.getKeyChar()=='d')
player.keyRight=true;
if(e.getKeyChar()=='t')
bullet.Shoot=true;
}
void keyReleased(KeyEvent e)
{
if(e.getKeyChar()=='w')
player.keyUp=false;
if(e.getKeyChar()=='a')
player.keyLeft=false;
if(e.getKeyChar()=='s')
player.keyDown=false;
if(e.getKeyChar()=='d')
player.keyRight=false;
if(e.getKeyChar()=='t')
bullet.Shoot=false;
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
I think the problem is that you're calling bullet.move(); continuously in the Timer in your GamePanel class. Apart from that I'm not entirely sure how you're fetching keyboard input. Your GamePanel class contains the methods a KeyListener usually would utilize, but the class does not implement a KeyListener, but an ActionListener, which isn't being used in your code.
So what you should be doing is:
Verify that your keyPressed and keyReleased functions are actually being called when a keyboard button is pressed, and if they aren't, you should implement a KeyListener instead of an ActionListener in the GamePanel class
Make sure that bullet.move(); is only being called when it should be, as it's currently being called on every iteration of your timer
Related
I tried few source codes of drawing in java and they were working fine, but when i tried to make one of my own I could not get the paint(Grahpics g) method to work! I looked again at the codes I have and checked some of the tutorials in Oracle's pages but i don't seem to be able to know why it would not work.
can someone please check it and tell me what is wrong here??
main method:
public class main
{
public static void main(String[] args)
{
new board();
}
}
board:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class board implements ActionListener
{
private JFrame f = new JFrame("Speedy");
private JPanel gamePanel = new JPanel();
private Image bg = new ImageIcon(this.getClass().getResource("road.png")).getImage();
private Timer t;
private car myCar = new car();
public board()
{
t = new Timer(50,this);
t.start();
gamePanel.setSize(600,400);
gamePanel.setDoubleBuffered(true);
gamePanel.setFocusable(true);
gamePanel.addKeyListener(new TAdapter());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(gamePanel,BorderLayout.CENTER);
//f.addKeyListener(new TAdapter());
f.setBounds(200,100,600,400);
f.setVisible(true);
f.revalidate();
f.repaint();
}
public void paint(Graphics g) {
gamePanel.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(bg,0,0,null);
g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);
System.out.println("Painted");
g.dispose();
}
public void actionPerformed(ActionEvent e)
{
gamePanel.repaint();
//System.out.println("Painting..");
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
myCar.keyPressed(e);
System.out.println("You pressed: "+e);
}
}
}
car.java:
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon
;
public class car
{
private Image image;
public int xPos,yPos;
public car()
{
image = new ImageIcon(this.getClass().getResource("car.png")).getImage();
xPos = 300;
yPos = 200;
System.out.println(image.getWidth(null));
}
public Image getImg() {return image;}
public void move() {}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) xPos -= 1;
if (key == KeyEvent.VK_RIGHT)xPos += 1;
if (key == KeyEvent.VK_UP) yPos -= 1;
if (key == KeyEvent.VK_DOWN) yPos += 1;
}
}
There are no errors, it shows me the width of the image which is right, also the timer triggers the ActionListener, also KeyListener is working, but the images would not draw! the paint(Graphics g) method just does not want to get triggered!
Googling it did not help.. I thought this would be a common problem but nobody has the problem I have, all solutions failed me.
help please?
If someone can explain it would be most appreciated!
Your class Board does not extend the JPanel class. So the paint() method is never called by the Swing.
Also, the statement gamePanel.repaint() will only execute the default JPanel paint() method of gamePanel. Instead you want the overridden paint method to be executed, so might want to do this:
public class Board extends JPanel implements ActionListener {
....
public void paint(Graphics g) {
this.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(bg,0,0,null);
g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);
System.out.println("Painted");
g2d.dispose();
}
....
}
Replace your action functionality with this:
public void actionPerformed(ActionEvent e) {
this.repaint();
}
Alternative solution:
If you do not want your Board class to extend JPanel, you can also override the paint() method of the gamePanel as you initialize it.
gamePanel = new JPanel() {
#Override
public void paint(Graphics g) {
this.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(bg,0,0,null);
g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);
g2d.dispose();
}
};
However, I would recommend the first solution rather than this one with anonymous classes.
When you call repaint on some container, then what happens is that Swing looks at all the components in that container and calls their paint method.
However, your board class (you should be calling it Board, by the way. Class names should always start with a capital) is not a component of your JFrame. When you call repaint, Swing will attempt to call the paint method of the JPanel that is a component of that JFrame. But you didn't override that method. You just added a paint method to your board, and board is not a component of the JFrame.
For this reason, usually you are supposed to create a class that extnds JPanel or some other component, and then add the current object of that class as a component to the JFrame. This way, your paint method will be called when the JFrame is repainted.
Your "main" class (board) should extend JPanel to work as expected.
With your way, paint would never be called. it acts like any normal self-written function.
If you want to keep things as they are, you can do something ike that:
gamePanel = new JPanel()
{
#Override
public void paint(Graphics g)
{
//your code here
}
};
Please keep in mind that a Class name should start with a capital letter. It won't make any errors but it is a naming convention as you can see here:
http://www.oracle.com/technetwork/java/codeconventions-135099.html
Im trying to write a very simple program that allows you to control a sprite with arrow keys and move around the screen. From what I understand, to do this I need a keyPressed() KeyListener. I believe Im doing this correctly based on the Java Doc but its not returning the correct output when I press the designated key. Can someone please tell me what is wrong with my code, and if possible, provide a simple example of the usage of this class? Thanks!
(Note, code is unfinished)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Display extends JPanel implements ActionListener, KeyListener {
Display() {
// super();
loadImages();
initTimer();
this.addKeyListener(this);
}
BufferedImage sprite;
Timer timer;
int up = 0;
public void loadImages() {
File spriteImage = new File("Pacman_sprite.png");
try {
sprite = ImageIO.read(spriteImage);
} catch (IOException e) {
System.out.println("Sprite import failed");
}
}
public void initTimer() {
timer = new Timer(100, this);
timer.start();
this.addKeyListener(this);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
System.out.println("Key press registered"); //does not print this?
if (key == KeyEvent.VK_UP) {
System.out.println("sucess"); // does not print this?
up++;
repaint();
}
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println("release");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(sprite, 500, 500 + up, null);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
repaint();
}
}
EDIT:
May have found the answer here. I moved the key handling code to a new class called KeyHandler then added these two lines to the constructor:
addKeyListener(new KeyHandler());
setFocusable(true);
It now appears to be working just fine (sort of, at least it is detecting when the up key is hit. My graphics aren't.)
You are not adding the listener to the JPanel.
Note: I would suggest you to change your design. Right now, Display class is both a JPanel and a Listener (which doesn't make sense in my opinion).
But to add the listener in your code do something like,
this.addKeyListener(this); // This looks awkward right. That's why you should change the design.
in your constructor.
I'm creating a "simple" 2D game engine for the block game Collapse for my college class. I am trying to make it such that the JFrame is resized any time a new screen is introduced, and I have made two attempts at it, one that resizes correctly, but doesn't show anything, and one that shows SOMETHING, but doesn't resize.
For this post, I will be showing the code for the second game engine that I have created, as my first attempt was a monolithic version that I ended up having trouble following myself.
First off, for some reason the window will not close with the X button, even though the setDefaultCLoseOperation(JFrame.EXIT_ON_CLOSE) is enacted.
Second (for the second attempt) the JFrame will not resize, but will show a sliver of the test StartMenu
(sorry for links, editor said I have to have rep of 10 before being able to post pictures... smart move, but still annoying)
http://i1148.photobucket.com/albums/o577/FoxnEagle/GraphicsWindow1_zpscqdgsjqh.png
but upon resizing;
http://i1148.photobucket.com/albums/o577/FoxnEagle/GraphicsWindow2_zpsckr4eohs.png
I guess the question of the post is what am I doing wrong to have the window become unresponsive, and be able to make modern art with it?
Collapse.java
import gameEngine.*;
import screens.*;
import javax.swing.*;
public class Collapse{
private Game game;
public Collapse(){
this.game = new Game("Test", null);
game.getScrMan().setScreen(new StartMenu(game.getScrMan()));
}
public static void main(String[] args){
new Collapse();
}
}
:||package screens||:
StartMenu.java
package screens;
import gameEngine.*;
import java.awt.*;
import javax.swing.*;
public class StartMenu extends Screen{
private final ScreenManager scrMan;
public StartMenu(ScreenManager scrMan){
super(scrMan);
this.scrMan = scrMan;
}
public void onCreate(){
JButton b1 = new JButton();
scrMan.getScrMan().setSize(200, 200);
scrMan.getScrMan().add(b1);
System.out.println("got here");
}
public void onUpdate(){
}
public void onDraw(Graphics2D g2d){
g2d.setColor(Color.BLACK);
g2d.fillOval(10, 10, 10, 10);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillOval(10,10,10,10);
}
}
:||package gameEngine||:
Game.java
package gameEngine;
import javax.swing.*;
import java.awt.*;
public class Game{
private final ImageIcon image;
private final String title;
private final GameLoop gameLoop;
private final ScreenManager scrMan;
private final InputManager inpMan;
private final EntityManager entMan;
private JFrame window;
private boolean running;
//constructor and initializer for gameLoop
public Game(String title, ImageIcon image){
this.title = title;
this.image = image;
window = new JFrame();
scrMan = new ScreenManager(this);
inpMan = new InputManager();
entMan = new EntityManager();
gameLoop = new GameLoop(this);
initialize();
SwingUtilities.invokeLater(gameLoop);
}
private void initialize(){
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close on exit
window.setTitle(title); //set the title
if(image != null){} //set the image icon NOT IMPLEMENTED YET
//window.setResizable(false); //user cannot resize
window.setFocusable(true); //
window.setLocationRelativeTo(null); //put in center of screen
window.add(scrMan); //add the
window.pack();
window.setVisible(true);
running = true;
}
public JFrame getWindow(){return window;}
public boolean getRunning(){return running;}
public ScreenManager getScrMan(){return scrMan;}
public InputManager getInput(){return inpMan;}
public EntityManager getEntMan(){return entMan;}
public void paused(){running = false;}
public void resume(){running = true;}
}
GameLoop.java
package gameEngine;
import java.lang.Runnable;
public class GameLoop implements Runnable{
private final Game game;
private long time;
private final int fps = 30;
public GameLoop(Game game){this.game = game;}
public void run(){
while(true){
while(game.getRunning()){
//System.out.println("running");//debugging
if(game.getScrMan().getScreen() != null){
game.getScrMan().getScreen().onUpdate();
//System.out.println("screen is updating");//debugging
}
//fps clock, commenting out does not fix problem
time = System.currentTimeMillis();
time = (1000/fps) - (System.currentTimeMillis() - time);
if(time > 0){try{Thread.sleep(time);}catch(Exception e){}}
}
}
}
}
ScreenManager.java
package gameEngine;
import javax.swing.JPanel;
import java.awt.*;
public class ScreenManager extends JPanel{
private final Game game;
private Screen screen;
public ScreenManager(Game game){this.game = game;}
public void setScreen(Screen screen){
this.screen = screen;
this.removeAll();
screen.onCreate();
game.getWindow().revalidate();
//game.getWindow().pack();
}
public Screen getScreen(){return screen;}
public Game getGame(){return game;}
public ScreenManager getScrMan(){return this;}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(0,0, 10, 10);
}
}
Screen.java (StartMenu extends and overrides all functions)
package gameEngine;
import java.awt.Graphics;
import java.awt.Graphics2D;
public abstract class Screen{
private final ScreenManager scrMan;
public Screen(ScreenManager scrMan){this.scrMan = scrMan;}
public abstract void onCreate();
public abstract void onUpdate();
public abstract void onDraw(Graphics2D g2d);
}
InputManager.java (nothing implemented yet, just a placeholder)
package gameEngine;
import java.awt.event.*;
public class InputManager implements KeyListener, MouseListener{
public void mouseClicked(MouseEvent event){
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public void keyPressed(KeyEvent event){
}
public void keyReleased(KeyEvent event){
}
public void keyTyped(KeyEvent event){
}
}
EntityManager.java and Entity.java are just blank classes
This,
SwingUtilities.invokeLater(gameLoop);
runs your game loop in the event dispatch thread, blocking it. So the repaint manager never has a change to draw the contents. You should instead create the GUI in the event dispatch thread, as modifying or creating swing components from other threads is not safe.
The game loop's timing needs to be done in a way that does not block the EDT. The easiest is using a swing Timer; if that proves later to be insufficient, you can switch to another solution, but then you need to pay close attention to thread safety. Swing timer runs the action listener code in the EDT so it's easy to be thread safe that way.
I made the following code to move a rectangle using arrowkeys of keyboard. The "keyPressed" function does not seem to be working properly.Infact, i don't think it is even getting called when a key is pressed bcz when i tried to print some text when a key is pressed, it was not getting printed.All i see in the output window is a stationary rectangle fixed at the top left corner of the window.Here is my code....pls help me...i need it desperately
import javax.swing.JFrame;
public class Main
{
public static void main(String args[])
{
JFrame window=new JFrame();
window.setSize(600,400);
window.setTitle("window");
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingComponent DC=new drawingComponent();
window.add(DC);
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.Timer;
public class drawingComponent extends JComponent implements ActionListener,KeyListener
{
Timer t=new Timer(2000,this);//moving after 5 milliseconds
static int x=0;
static int y=0;
private static int velx=0;
private static int vely=0;
public drawingComponent()
{
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
System.out.println("tr1");
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D) g;
Rectangle rect1=new Rectangle(x,y,50,30);
g2.setColor(Color.RED);
g2.fill(rect1);
System.out.println("tr2");
}
public void actionPerformed(ActionEvent e) //inbuilt fncn f actionListener(interface) which needs to be created
{
x+=velx; //changing values
y+=vely;
System.out.println("tr3");
repaint(); //inbuilt fncn to repeat the paintComponent method
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
if(code==KeyEvent.VK_UP)
{ velx=0; vely=-1;repaint(); }
if(code==KeyEvent.VK_DOWN)
{ velx=0; vely=1; repaint(); }
if(code==KeyEvent.VK_LEFT)
{vely=0; velx=-1; repaint(); }
if(code==KeyEvent.VK_RIGHT)
{vely=0; velx=1; repaint();}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
}
Welcome to the wonderful world of KeyListeners...
While you have set the component focusable, you've not requested that the component be focused.
You could try calling requestFocusInWindow, but he this raises the question of when to call it.
You could call it within the constructor, but because the component doesn't belong to a valid visible component yet, the call may fail. You could override the components addNotify method and add the call to it, after you call super.addNotify, but the requestFocusInWindow method doesn't gurentee that focus will be given the component
Instead, you could simply avoid all this hassle and use the key bindings API instead, which will give you control over the level of focus require for key events to be triggered
As a side note, you should call setVisible on your frame after you've set up the UI completely
I have created the following simple Java Swing program which outputs a 3*3 square in the window every time the user clicks their mouse. The squares remain in the window even if the user clicks more than once. The program compiles and runs just fine, however, when one clicks in the window the square is drawn far below where the mouse pointer is. I've been racking my brain over this one for a while -- what can I change here to get the square to appear exactly with the pointer on each click? Many thanks for any help!
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ClickCloud extends JComponent {
final ArrayList<Point2D> points = new ArrayList<Point2D>();
public void addPoint(Point2D a) {
points.add(a);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < points.size(); i++) {
Point2D aPoint = points.get(i);
g2.draw(new Rectangle2D.Double(aPoint.getX(), aPoint.getY(), 3, 3));
}
}
public static void main(String[] args) {
final ClickCloud cloud = new ClickCloud();
JFrame aFrame = new JFrame();
class ClickListen implements MouseListener {
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
cloud.addPoint(arg0.getPoint());
cloud.repaint();
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setSize(500, 500);
aFrame.add(cloud);
aFrame.addMouseListener(new ClickListen());
aFrame.setVisible(true);
}
}
You're adding the MouseListener to the JFrame, but displaying the results in the JComponent and relative to the JComponent. So the location of the Point clicked will be relative to the JFrame's coordinates, but then displayed relative to the JComponent's coordinates which will shift things down by the distance of the title bar. Instead simply add the MouseListener to the same component that is responsible for displaying the results so that the display and clicking coordinates match:
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setSize(500, 500);
aFrame.add(cloud);
//!! aFrame.addMouseListener(new ClickListen()); // !! Removed
cloud.addMouseListener(new ClickListen()); // !! added
aFrame.setVisible(true);
By the way: Thanks for creating and posting a decent SSCCE as this makes it so much easier to analyse and solve your problem.