Java call function in a loop without stackoverflowerror - java

I know I have asked a question like this before, but none of the answers in the old question worked for me. I am trying to make a little single-player pong game (It is in a Java applet). I already have a moveBall() function, as you can see below. But I don't know where to call it. I can't call it in the paint() method because it is double buffered.
public class Main extends Applet implements KeyListener, MouseListener {
private Rectangle paddle;
private Rectangle ball;
private ArrayList<Integer> keysDown;
private Image dbImage;
private Graphics dbg;
public int time = 300000;
Random randomGenerator = new Random();
int speed = 10;
int level = 1; // change to 0 once start menu works
int xpos, ypos;
int ballx, bally;
int width = 1024;
int height = 768;
int paddleWidth = 96;
int ballSize = 16;
String version = "0.0.1";
public static final int START_X_POS = 160;
public static final int START_Y_POS = 160;
public static final int START_WIDTH = 256;
public static final int START_HEIGHT = 64;
boolean startClicked;
boolean falling = true;
public void init() {
setSize(width, height);
addKeyListener(this);
addMouseListener(this);
setBackground(Color.black);
Frame c = (Frame)getParent().getParent();
c.setTitle("Asteroid Attack - Version " + version);
keysDown = new ArrayList<Integer>();
paddle = new Rectangle(getWidth()/2-paddleWidth, getHeight()-96, paddleWidth, 12);
ball = new Rectangle(getWidth()/2-ballSize, 96, ballSize, ballSize);
}
public void update(Graphics g) {
dbImage = createImage(getSize().width, getSize().height);
dbg = dbImage.getGraphics ();
if (dbImage == null) {}
dbg.setColor(getBackground ());
dbg.fillRect(0, 0, getSize().width, getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
if (level != 0) {
g2.setPaint(Color.gray);
g2.fill(paddle);
g2.setPaint(Color.darkGray);
g2.fill(ball);
moveBall();
}
}
public void moveBall() {
bally = ball.y;
ballx = ball.x;
if (bally < paddle.y-32 && falling) {
bally += 12;
}
if (bally < paddle.y-32 && falling && paddle.x <= ballx && paddle.getMaxX() >= ball.x) { // collides with paddle
falling = false;
}
else { // does not collide with paddle
}
ball.setLocation(ballx, bally);
}
#Override
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode()))
keysDown.add(new Integer(e.getKeyCode()));
key();
}
#Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void key() {
if (level != 0) {
int x = paddle.x;
int y = paddle.y;
if (keysDown.contains(KeyEvent.VK_ESCAPE)) {System.exit(0);}
if (x > 0 && x+paddleWidth < this.getWidth()) {
if (keysDown.contains(KeyEvent.VK_LEFT)) {x -= speed;}
if (keysDown.contains(KeyEvent.VK_RIGHT)) {x += speed;}
}
else { // so paddle doesn't exit room
if (x <= 0) {x += 4;}
else {x -= 4;}
}
paddle.setLocation(x, y);
repaint();
}
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void mouseClicked(MouseEvent me) {
if (level == 0) {
xpos = me.getX();
ypos = me.getY();
if (xpos >= START_X_POS && ypos >= START_Y_POS && xpos <= START_X_POS + START_WIDTH && ypos <= START_X_POS + START_HEIGHT ) {
level = 1;
}
}
}
#Override
public void mouseEntered(MouseEvent me) {}
#Override
public void mouseExited(MouseEvent me) {}
#Override
public void mouseReleased(MouseEvent me) {}
#Override
public void mousePressed(MouseEvent me) {}
}
Any help would be greatly appreciated!

You'll probably want a separate Thread to move the ball which keeps track of time while in a in a loop - that way if frame rates drop, or speed up, you can try ensure consistency in the ball movement speed.
e.g. here on using a thread in a applet http://www.realapplets.com/tutorial/threadexample.html

Related

Implementing extended class or not

Another question about inheritance in java: I was wondering about two things:
1) how can I set my program to switch between using an inherited class or not?
2) I'm not sure why my extended class glidingObject.java is not responding to my key presses
Here's my Game.java (which runs the game; I should be passing in some parameter that allows the user to choose which class to use right - either flying object or gliding object? I've also included my two classes for flying object and gliding object)
public class Game extends JPanel {
private static final long serialVersionUID = 1L;
public static int WIDTH = 800, HEIGHT = 750;
public static ArrayList<Rectangle> columns;
public static Random rand;
public static double score;
public static boolean gameOver, started; //two modes, started and gameover
public static String username;
public static String currTime;
public static Timer timer;
public static flyingObject obj;
private PropertyChangeSupport mPcs = new PropertyChangeSupport(this);
public Game(flyingObject object){
obj = object;
timer = new Timer(20, new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tick();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
columns = new ArrayList<Rectangle>();
rand = new Random();
Background bk = new Background();
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
started = true;
}
});
PropertyChangeListener listener = new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
try {
scoreBoard.record();
} catch (IOException e) {
e.printStackTrace();
}
}
};
this.addPropertyChangeListener(listener);
getDate(); //get the date of the game played
mPcs.firePropertyChange("gameOver",false, true); //alert record() method when the game is over
timer.start();
}
//adding the column
public static void addColumn(boolean start) {
int space = 300;
int width = 100;
int height = 50 + rand.nextInt(300);
if (start) {
//add top and bottom
columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height + 100));
columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
}
else
{
columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height + 100));
columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
}
}
//paint the columns
public void paintColumn(Graphics g, Rectangle column) {
g.setColor(Color.white);
g.fillRect(column.x, column.y, column.width, column.height);
}
public static void reset() {
obj = new glidingObject(WIDTH / 2 - 10, HEIGHT / 2 - 10);
columns.clear();
score = 0.0;
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
gameOver = false;
}
public void tick() throws IOException {
if (started) {
int speed = 10;
glidingObject.move();
for (int i = 0; i < columns.size(); i ++) {
Rectangle column = columns.get(i);
column.x -= speed;
if (column.x + column.width < 0) {
columns.remove(column);
if (column.y == 0) {
addColumn(false);
}
}
}
for (Rectangle column: columns) {
if (column.x == glidingObject.X) {
score += 0.5;
}
if (column.intersects(glidingObject.getBounds())) {
gameOver = true;
//when the object crashes, it does not go through the column
if (glidingObject.X <= column.x) {
glidingObject.X = column.x - glidingObject.DIAMETER;
}
else if (glidingObject.Y < column.height) {
glidingObject.Y = column.height;
}
Main.gameOver();
}
}
if (glidingObject.Y > HEIGHT || glidingObject.Y < 0) {
gameOver = true;
//timer.stop();
Main.gameOver();
}
if (glidingObject.Y + glidingObject.YMotion >= HEIGHT) {
gameOver = true;
//timer.stop();
Main.gameOver();
}
}
//update the display
repaint();
}
public void getDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
currTime = dateFormat.format(date);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Background.paint(g);
glidingObject.paint(g);
for (Rectangle column: columns) {
paintColumn(g, column);
}
g.setColor(Color.white);
g.setFont(new Font("Manaspace", 1, 60));
if (!started) {
g.drawString("Click to start!", 75, HEIGHT / 2 - 50);
}
if (gameOver) {
g.drawString("Game Over!", 200, HEIGHT / 2 - 50);
}
if (!gameOver && started) {
g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
}
}
}
class flyingobject.java
public class flyingObject implements ActionListener, MouseListener, KeyListener {
static int DIAMETER = 25;
static int Y; // Y position of the unicorn
static int X; // X position of the unicorn
static int YMotion; // Y movement of the unicorn
//parameters are the initial positions
public flyingObject(int xpos, int ypos) {
X = xpos;
Y = ypos; // this changes
}
//getters
public int getX() {
return X;
}
public int getY() {
return Y;
}
//setters
public void setX(int newX) {
X = newX;
}
public void setY(int newY) {
Y = newY;
}
//the bounds of the object (rectangle)
public static Rectangle getBounds() {
return new Rectangle(X, Y, DIAMETER, DIAMETER);
}
//the visible component of the object - this can get overriden by subclasses
public static void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(X, Y, DIAMETER, DIAMETER);
}
//the movement component of the object
public static void jump() {
if (Game.gameOver) {
Game.reset();
Game.gameOver = false;
}
if (!Game.started) {
Game.started = true;
}
else if (!Game.gameOver) {
if (YMotion > 0) {
YMotion = 0;
}
YMotion -= 14;
}
}
public static void move() {
if ((Y > 0) && (Y < Game.HEIGHT)) {
YMotion += 1.5; // gravity
Y += YMotion;
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
jump();
}
}
#Override
public void mouseClicked(MouseEvent e) {
jump();
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
Class glidingobject.java (the game.java should allow the user to choose between just using flying object, or the extended class gliding object)
public class glidingObject extends flyingObject{
//velocity of the object
private static int vx;
private static int vy;
private static int lives;
public glidingObject(int xpos, int ypos) {
super(xpos, ypos);
vx = 0;
vy = 0;
lives = 3;
}
//getter methods
public int getVx() {
return vx;
}
public int getVy() {
return vy;
}
//setter methods
public void setVx(int newVx) {
vx = newVx;
}
public void setVy(int newVy) {
vy = newVy;
}
//moves the object
public static void jump() {
X += vx;
Y += vy;
}
public static void paint(Graphics g) {
g.setColor(Color.CYAN);
g.fillOval(X, Y, DIAMETER, DIAMETER);
}
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
vx = 10;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
vx = -10;
}
if (e.getKeyCode() == KeyEvent.VK_UP){
vy = 10;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN){
vy = -10;
}
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
I should be passing in some parameter that allows the user to choose which class to use right - either flying object or gliding object?
This is where interfaces become so powerful. Both the "flying" and "gliding" objects are going to share some common properties/functionality, these should be described by the interface. You Game should then only accept instances of this interface, it shouldn't care what the implementation, only that they adhere to the agreed interface.
This is principle of "code to interface, not implementation"
How complicated this gets is up to you, for example, you could have abstract implementations which describe "air" based entities and "ground" based entities, from which you could have "powered" and "unpowered" implementations of the abstract "air" class, all of which would be tied back to the "game entity" interface
I'm not sure why my extended class glidingObject.java is not responding to my key presses
This is because you're using KeyListener, this well known for it's focus related issues. See How to Use Key Bindings for the recommended solution

How to fix multiple threads running in java awt graphics

I am programming a game for a school project and when I run the code, it works sometimes and then ill run it again and the paddle or ball won't move and the print statements in my keylistener dont show up.
Now I am running two timers, one for the animation and one for a countdown, could this be an issue? It seems like multiple threads are running or when I close the jframe and then rerun the program is picking up where it left off?
public class LobPong extends JPanel implements KeyListener {
public static double xCoordinate;
public static double yCoordinate;
public static Timer timer;
static int xPaddleLeft;
private static int yPaddle = 800;
private static int score = 0;
private static int level = 1;
public static JLabel scoreLabel = new JLabel(" Score: 0 ");
public static JLabel timeLabel = new JLabel(" ");
public static JLabel levelLabel = new JLabel(" Level: ");
private static int life = 3;
private static int levelTime = 30000;
public static int dx, dy;
private static double times = 0;
public static void main(String[] args) {
JFrame LobPongApp = new JFrame();
LobPong canvas = new LobPong();
canvas.setBackground(Color.BLUE);
LobPongApp.add(canvas);
LobPongApp.setSize(800, 900);
LobPongApp.setTitle("Lob Pong");
LobPongApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LobPongApp.setVisible(true);
canvas.setFocusable(true);
LobPongApp.addKeyListener(canvas);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval((int) xCoordinate, (int) yCoordinate, 50, 50);
g.fillRect(xPaddleLeft, yPaddle, 100, 10);
drawLife(g, getLife());
}
public LobPong() {
timer = new Timer(1, new timerCallBack());
timer.start();
setLayout(new FlowLayout());
scoreLabel.setOpaque(true);
timeLabel.setOpaque(true);
levelLabel.setOpaque(true);
scoreLabel.setBackground(Color.black);
timeLabel.setBackground(Color.black);
levelLabel.setBackground(Color.black);
scoreLabel.setForeground(Color.WHITE);
timeLabel.setForeground(Color.WHITE);
levelLabel.setForeground(Color.WHITE);
levelLabel.setText("Level: " + 1);
add(scoreLabel);
add(timeLabel);
add(levelLabel);
xPaddleLeft = 375; //debug
xCoordinate = 0;
yCoordinate = 0;
setTime(levelTime); //sets current level time (increases by 10 seconds every 2 levels)
}
public static void nextLevel(Graphics g, int levels) {
level += 1;
g.drawString("NEXT LEVEL!", 400, 400);
timer.stop();
//TODO call run() ?
}
public static void updateScore(int scoreAdd) {
score += scoreAdd;
scoreLabel.setText("Score: " + score);
}
public static int getScore() {
return score;
}
public static void updateLife(int x) {
life += x;
}
public static int getLife() {
return life;
}
public static void drawLife(Graphics g, int x) {
g.setColor(Color.RED);
for(int i = 0; i < (x * 10); i += 10) { //for loop to offset lives and draw enough balls per lives
g.fillOval(30 + i, 10, 10, 10);
}
}
public static void extraLife(boolean x) {
if (x == true) {
updateLife(1);
}
}
Timer timerDisplay = new Timer(1000, new TimerListener());
private static int totalTime;
public static void setTime(int time) {
totalTime = time;
}
public static int getTime() {
return totalTime;
}
protected class TimerListener implements ActionListener { //handles countdown timer
#Override
public void actionPerformed(ActionEvent arg0) {
setTime(getTime() - 1000);
LobPong.timeLabel.setText("Time Remaining: " + getTime()/1000 + " ");
if(getTime() <= 0) {
timerDisplay.stop();
timer.stop();
}
}
}
public void keyPressed(KeyEvent arg0) {
int key = arg0.getKeyCode();
System.out.println("testing");
if(key == 37) {
if (xPaddleLeft > 0) {
xPaddleLeft -= 50;
System.out.println("TEST"); //debug
}
}
if(key == 39) {
if (xPaddleLeft < (getWidth() - 50)) {
xPaddleLeft += 50;
}
}
if(key == KeyEvent.VK_ENTER) {
dx = 2;
timer.start();
timerDisplay.start();
System.out.println("start"); //debug
}
repaint();
}
boolean horizontal = true; //handles horizontal movement
boolean vertical = true; //handles vertical
double upwards;
public class timerCallBack implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
times += .01;
if (horizontal == true) { //check
xCoordinate += dx;
}
if(xCoordinate <= 0) { //check
horizontal = true;
}
if(xCoordinate >= getWidth()) { //check
horizontal = false;
}
if(horizontal == false) { //check
xCoordinate -= dx;
}
if(vertical == true) { //check
dy = (int) times;
yCoordinate += dy;
}
if (vertical == false) { //check
dy = (int) (upwards - times);
yCoordinate -= dy;
}
if(dy == 0) { //check
vertical = true;
times = 0;
}
if(yCoordinate == getHeight()) { //check
updateLife(-1);
if (getLife() == 0) {
timer.stop();
timerDisplay.stop();
//TODO you lose and print high score
}
extraLife(false);
xCoordinate = 0;
yCoordinate = 0;
dy = 0;
dx = 0;
vertical = true;
}
if(xCoordinate <= (xPaddleLeft + 100) && xCoordinate >= xPaddleLeft && (yCoordinate + 50) == yPaddle) {
upwards = times;
times = 0;
vertical = false;
updateScore(1);
repaint();
//TODO plus one point, paddle bounce physics, direction
}
repaint();
}
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}

bullets creation in a simple game

I am creating a simple game where shapes fall and the player shoots them, but I am having problems creating bullet at every click of the mouse. I have tried various logic with no help, so am just going to put the code up here so you guys can take a look at it and help me out.
The bullet I created is not been created on every click just one is created and it moves on every click which is wrong........I want one bullet to be created per click.
// My main class: mousework2
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;
public class mousework2 extends JFrame
{
public static int Width = 300;
public static int Height = 400;
private JPanel p1;
private Image pixMage,gunMage;
public mousework2()
{
super("shoot-em-up");
this.setSize(Width, Height);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension pos = Toolkit.getDefaultToolkit().getScreenSize();
int x = (pos.width - Width) / 2;
int y = (pos.height - Height) / 2;
this.setLocation(x, y);
p1 = new CreateImage();
this.add(p1);
this.getContentPane();
Thread t = new recMove(this);
t.start();
}
class recMove extends Thread
{
JFrame b;
public recMove(JFrame b)
{
this.b = b;
}
public void run()
{
while (true) {
b.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}
class CreateImage extends JPanel implements MouseListener
{
ArrayList<fallShape> rect = new ArrayList<fallShape>();
int x_pos = mousework.Width / 2;
int y_pos = mousework.Height - 50;
int bx_pos = mousework.Width / 2;
int by_pos = mousework.Height;
int y_speed = -10;
boolean clicked;
public CreateImage()
{
for (int i = 0; i < 10; i++) {
rect.add(new fallShape(15, 15, rect));
}
Toolkit picx = Toolkit.getDefaultToolkit();
gunMage = picx.getImage("gunner.jpg");
gunMage = gunMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
Toolkit pic = Toolkit.getDefaultToolkit();
pixMage = pic.getImage("ballfall3.jpg");
pixMage = pixMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
addMouseListener(this);
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseMoved(MouseEvent e)
{
x_pos = e.getX() - 5;
}
});
}
public void mousePressed(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = true;
}
}
public void mouseReleased(MouseEvent e)
{
if (e.getButton() == 1) {
clicked = false;
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(pixMage, 0, 0, Width, Height, null);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(gunMage,x_pos,y_pos,10,20,null);
if (clicked) {
by_pos += y_speed;
Shape bullet = new Rectangle2D.Float(bx_pos, by_pos, 3, 10);
g2.setColor(Color.BLACK);
g2.fill(bullet);
g2.draw(bullet);
}
g2.setColor(Color.RED);
for (fallShape b : rect) {
b.move();
g2.fill(b);
}
}
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run()
{
new mousework2().setVisible(true);
}
});
}
}
And:
// My falling shapes class: fallShape
import java.awt.geom.*;
import java.util.*;
public class fallShape extends Rectangle2D.Float
{
public int x_speed, y_speed;
public int l, b;
public int height = mousework.Height;
public int width = mousework.Width;
public ArrayList<fallShape> fall;
public fallShape(int breadth, int length, ArrayList<fallShape> fall)
{
super((int) (Math.random() * (mousework.Width - 20) + 1), 0, breadth, length);
this.b = breadth;
this.l = length;
this.x_speed = (int) Math.random() * (10) + 1;
this.y_speed = (int) Math.random() * (10) + 1;
this.fall = fall;
}
public void move()
{
Rectangle2D rec = new Rectangle2D.Float(super.x, super.y, b, l);
for (fallShape f : fall) {
if (f != this && f.intersects(rec)) {
int rxspeed = x_speed;
int ryspeed = y_speed;
x_speed = f.x_speed;
y_speed = f.y_speed;
f.x_speed = rxspeed;
f.y_speed = ryspeed;
}
}
if (super.x < 0) {
super.x =+ super.x;
//super.y =+ super.y;
x_speed = Math.abs(x_speed);
}
if (super.x> mousework.Width - 30) {
super.x =+ super.x;
super.y =+ super.y;
x_speed =- Math.abs(x_speed);
}
if (super.y < 0) {
super.y = 0;
y_speed = Math.abs(y_speed);
}
super.x += x_speed;
super.y += y_speed;
}
}
if(clicked){
by_pos+=y_speed;
This code only draws the bullet when the mouse is down. This is because you are setting clicked to false in your mouseReleased method:
public void mouseReleased(MouseEvent e){
if(e.getButton()==1)
clicked=false;
}
If you were to remove the body of the mouseReleased method, your bullet would move properly.
However, say you wanted to have more than just one bullet. Currently, your paint method only draws one bullet at a time. To draw multiple bullets, you would need to create a list of the coordinates of the bullets, and add a new coordinate pair to the list whenever you click. Then, in the paint method, just update each position in a for loop.
ArrayList<Integer> by_poss = new ArrayList<>();
by_poss is the list of all the y-positions of your bullets.
public void mousePressed(MouseEvent e){
if(e.getButton() == 1)
by_poss.add(mousework.Height);
}
The mousePressed method adds a new "bullet", in the form of a y-position, to the coordinates.
public void mouseReleased(MouseEvent e){
//do nothing
}
Nothing needs to happen in the mouseReleased method.
//update the bullets
public void paint(Graphics g){
...
g2.setColor(Color.BLACK);
Shape bullet;
for(int i = 0; i < by_poss.size(); i++){
by_poss.set(i, by_poss.get(i) + y_speed); //move the bullet
bullet = new Rectangle2D.Float(bx_pos, by_poss.get(i), 3, 10);
g2.fill(bullet);
g2.draw(bullet);
}
...
}
The for loop in your paint method draws all the bullets, one by one, usin g the y-positions from the by_poss list.

Java Applet MouseListener not working

I thought I programmed it so that when I click on the 'Start' button that appears when it is not level 1 or higher, it would go to level 1. But nothing happens when I click it.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Font;
public class Move extends Applet implements KeyListener, MouseListener {
private Rectangle rect;
private ArrayList<Integer> keysDown;
Random randomGenerator = new Random();
int speed = 4;
int level = 0;
int xpos;
int ypos;
boolean startClicked;
Image block;
Image start;
URL base;
MediaTracker mt;
int randomx = randomGenerator.nextInt(560);
int randomy = randomGenerator.nextInt(360);
public void init() {
this.addKeyListener(this);
this.addMouseListener(this);
keysDown = new ArrayList<Integer>();
rect = new Rectangle(0, 0, 50, 50); ///////////////////////////////////you can use rect.getX();
mt = new MediaTracker(this);
try {
base = getDocumentBase();
}
catch (Exception e) {}
block = getImage(base, "block.gif");
start = getImage(base, "start_button.png");
mt.addImage(block, 1);
try {
mt.waitForAll();
}
catch (InterruptedException e) {}
}
public void paint(Graphics g) {
setSize(600, 400);
Graphics2D g2 = (Graphics2D)g;
if (level != 0) {
g2.fill(rect);
//g.drawImage(block, randomx, randomy, this); ###############################fhjvhfjvkjerhgvgf
Font font = new Font("Arial", Font.BOLD, 18);
g.setFont(font);
String text = "Speed: " + speed;
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) + fm.getAscent();
g.drawString(text, x, y);
}
else { // start menu
g.drawImage(start, 160, 160, this);
}
}
#Override
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode()))
keysDown.add(new Integer(e.getKeyCode()));
moveRect();
}
#Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect() {
if (level != 0) {
int x = rect.x;
int y = rect.y;
if (keysDown.contains(KeyEvent.VK_UP)) {
y -= speed;
}
if (keysDown.contains(KeyEvent.VK_DOWN)) {
y += speed;
}
if (keysDown.contains(KeyEvent.VK_LEFT)) {
x -= speed;
}
if (keysDown.contains(KeyEvent.VK_RIGHT)) {
x += speed;
}
rect.setLocation(x, y);
repaint();
if (x-32 > randomx-72 && y+64 > randomy && x-32 < randomx+72 && y-64 < randomy) { /// will be flag
randomx = randomGenerator.nextInt(560);
randomy = randomGenerator.nextInt(360);
speed += 4;
}
}
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void mouseClicked(MouseEvent me) {
if (level == 0) {
xpos = me.getX();
ypos = me.getY();
if (xpos > 160 && ypos > 96 && xpos < 400 && ypos < 160) {
level = 1;
}
}
}
#Override
public void mouseEntered(MouseEvent me) {
}
#Override
public void mouseExited(MouseEvent me) {
}
#Override
public void mouseReleased(MouseEvent me) {
}
#Override
public void mousePressed(MouseEvent me) {
}
}
Thanks again for all the help! This will HOPEFULLY turn out to be a maze game...
PS: ignore the pointless comments.
You are drawing at
g.drawImage(start, 160, 160, this);
But checking for
if (xpos > 160 && ypos > 96 && xpos < 400 && ypos < 160) {
Change that to
if (xpos >= 160 && ypos > 159 && xpos < 400 && ypos < 190) {
Better yet make constants of these numbers or use an ImageIcon and add a listener to it.
public static final int START_X_POS = 160;
public static final int START_Y_POS = 160;
public static final int START_WIDTH = 30;
public static final int START_HEIGHT = 150;
//...
if (xpos >= (START_X_POS ) && ypos >= START_Y_POS && xpos <= ( START_X_POS + START_WIDTH) && ypos <= (START_X_POS + START_HEIGHT )) {

Why does the height of the rectangles jump vary?

Why does the height of the rectangle's jump vary? It seems to go in a cycle. First it jumps low then it doesn't jump at all then it jumps high then it doesn't jump at all. i can't figure out why as the same code is used and it is triggered by the same event.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
class Game extends JPanel{
Square square = new Square(this);
Ground ground = new Ground (this);
public Game() {
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
square.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
square.keyPressed(e);
}
});
setFocusable(true);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("My Mario");
Game game = new Game();
frame.add(game);
frame.setSize(600, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(30);
}
}
private void move() {
square.move();
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
square.paint(g2d);
ground.paint(g2d);
}
}
public class Square {
Square square;
int x,xa;
static int y;
int ya;
private Game game;
public static int fistX,fistY;
static int d = 60;
int wide;
boolean onGround;
public Square(Game game) {
this.game = game;
x = 100;
y = 631;
xa = 0;
ya = 0;
onGround = false;
wide = game.getWidth();
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-30)
x = x + xa;
if (y + ya > 0 && y + ya < game.getHeight()-60){
for(int i=12; i< 0; i--);
ya+=10;
y = y + ya;
}
if ( collision() ) {
y-=10;
onGround = true;
}
Square.y+=10;
}
public void paint(Graphics2D g) {
g.setColor(Color.RED);
g.fillRoundRect(x, y-d, 30, d, 10, 10);
}
private boolean collision() {
return game.ground.getBounds().intersects(getBounds());
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 60);
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa=0;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa=0;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
d = 60;
if(e.getKeyCode() == KeyEvent.VK_UP);
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = xa -3;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = xa + 3;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
d = 30;
if(e.getKeyCode() == KeyEvent.VK_UP)
ya -= 60;
}
}
class Ground {
int y,x,h,w;
public Ground(Game game){
x = 0;
y = game.getHeight()-30;
w = game.getWidth();
h = 30;
}
public void paint(Graphics2D g){
g.setColor(Color.BLACK);
g.fillRect(0, 700, 99999999, 30);
}
public Rectangle getBounds() {
return new Rectangle(0, 700, 99999999, 30);
}
}
Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeated tasks like animation. See Concurrency in Swing for more details.
frame.setSize(600, 700); I recommend instead setting a preferred size for the content and packing the frame around it. This suggests a constant size for the game on different PLAFs or OS'.
For Swing components other than top-level containers (e.g JFrame or JWindow), override paintComponent(Graphics) rather than paint(Graphics).
Look into key bindings instead of key listeners for Swing.
The first 3 are implemented, the last one (and other one commented in the code) is TODO - BNI.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SuperMarioGame extends JPanel {
private static final long serialVersionUID = 1L;
Square square = new Square(this);
Ground ground = new Ground (this);
public SuperMarioGame() {
// TODO Update to Key Bindings
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
square.keyReleased(e);
}
#Override
public void keyPressed(KeyEvent e) {
square.keyPressed(e);
}
});
setFocusable(true);
// Use a listener/timer combo.
ActionListener gameAnimation = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
};
Timer timer = new Timer(30,gameAnimation);
timer.start();
// Set a preferred size for the panel.
Dimension preferred = new Dimension(600,700);
this.setPreferredSize(preferred);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("My Mario");
SuperMarioGame game = new SuperMarioGame();
frame.add(game);
// Pack the frame to the preferred size.
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void move() {
square.move();
}
#Override
// for Swing components, generally override
// paintComponent rather than paint
//public void paint(Graphics g) {
public void paintComponent(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
square.paint(g2d);
ground.paint(g2d);
}
}
class Square {
Square square;
int x,xa;
static int y;
int ya;
private SuperMarioGame game;
public static int fistX,fistY;
static int d = 60;
int wide;
boolean onGround;
public Square(SuperMarioGame game) {
this.game = game;
x = 100;
y = 631;
xa = 0;
ya = 0;
onGround = false;
wide = game.getWidth();
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-30)
x = x + xa;
if (y + ya > 0 && y + ya < game.getHeight()-60){
for(int i=12; i< 0; i--);
ya+=10;
y = y + ya;
}
if ( collision() ) {
y-=10;
onGround = true;
}
Square.y+=10;
}
public void paint(Graphics2D g) {
g.setColor(Color.RED);
g.fillRoundRect(x, y-d, 30, d, 10, 10);
}
private boolean collision() {
return game.ground.getBounds().intersects(getBounds());
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 60);
}
public void keyReleased(KeyEvent e) {
// TODO Else-if would be better here..
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa=0;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa=0;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
d = 60;
if(e.getKeyCode() == KeyEvent.VK_UP);
}
public void keyPressed(KeyEvent e) {
// TODO Else-if would be better here..
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = xa -3;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = xa + 3;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
d = 30;
if(e.getKeyCode() == KeyEvent.VK_UP)
ya -= 60;
}
}
class Ground {
int y,x,h,w;
public Ground(SuperMarioGame game){
x = 0;
y = game.getHeight()-30;
w = game.getWidth();
h = 30;
}
public void paint(Graphics2D g){
g.setColor(Color.BLACK);
g.fillRect(0, 700, 99999999, 30);
}
public Rectangle getBounds() {
return new Rectangle(0, 700, 99999999, 30);
}
}
Don't block the Event Dispatching Thread any way, it will prevent the EDT from processing repaint requests (and other events) which will make it look like your program has crashed. Have a read of Concurrency in Swing for more information.
Only modify the UI from the EDT, NEVER from any other thread, this includes creating UI elements
Favor key bindings over KeyListeners, they are more capable of resolving focus issues amongst other things. Have a read of How to Use Key Bindings for more details
Favor overriding paintComponent rather then paint. Paint does a lot of important work which you should avoid messing with if you can. Apart from any thing else paintComponent is going to be included in the double buffering of the component where as paint isn't (as super.paint sets it up)
Avoid using static state variables where possible
I don't know if this was deliberate or not, but, for (int i = 12; i < 0; i--); isn't going to achive anything, as the semi colen at the end means, do nothing for a count of 1
Personally, try not to use absolute values for things like width and height, which is actually reliant on parent container. You should also, where possible, provide sizing hints to allow the parent container to make better decisions on how much space it actually needs
UPDATED
Fixed bug in my movement code :P
I had a look at your collision detection code (in move) and, frankly, couldn't make heads or tails of it. I corrected it as well as changed in the way the paint method works so that x,y is always the top, left corner
public class TestGame {
public static void main(String[] args) throws InterruptedException {
new TestGame();
}
public TestGame() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
Game game = new Game();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GameThread extends Thread {
private Game game;
public GameThread(Game game) {
setDaemon(false);
this.game = game;
}
#Override
public void run() {
while (true) {
game.move();
try {
long startedAt = System.currentTimeMillis();
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
game.repaint();
}
});
long completedAt = System.currentTimeMillis();
long sleepFor = 30 - (completedAt - startedAt);
if (sleepFor < 0) {
sleepFor = 30;
}
Thread.sleep(sleepFor);
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
}
public class Game extends JPanel {
Square square = new Square(this);
Ground ground = new Ground(this);
public Game() {
// addKeyListener(new KeyListener() {
// #Override
// public void keyTyped(KeyEvent e) {
// }
//
// #Override
// public void keyReleased(KeyEvent e) {
// square.keyReleased(e);
// }
//
// #Override
// public void keyPressed(KeyEvent e) {
// square.keyPressed(e);
// }
// });
setFocusable(true);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "press-left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "press-right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "press-down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "press-up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "release-left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "release-right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "release-down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "release-up");
am.put("press-left", new PressLeftAction(square));
am.put("press-right", new PressRightAction(square));
am.put("press-down", new PressDownAction(square));
am.put("press-up", new PressUpAction(square));
am.put("release-left", new ReleaseLeftAction(square));
am.put("release-right", new ReleaseRightAction(square));
am.put("release-down", new ReleaseDownAction(square));
am.put("release-up", new ReleaseUpAction(square));
new GameThread(this).start();
// public void keyReleased(KeyEvent e) {
//
//
//
// if (e.getKeyCode() == KeyEvent.VK_LEFT) {
// xa = 0;
// }
//
// if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
// xa = 0;
// }
// if (e.getKeyCode() == KeyEvent.VK_DOWN) {
// d = 60;
// }
//
// if (e.getKeyCode() == KeyEvent.VK_UP);
// }
//
// public void keyPressed(KeyEvent e) {
//// TODO Auto-generated method stub
// if (e.getKeyCode() == KeyEvent.VK_LEFT) {
// xa = xa - 3;
// }
//
// if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
// xa = xa + 3;
// }
//
// if (e.getKeyCode() == KeyEvent.VK_DOWN) {
// d = 30;
// }
//
// if (e.getKeyCode() == KeyEvent.VK_UP) {
// ya -= 60;
// }
//
//
//
//
//
// }
}
public void move() {
square.move();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
// Don't override paint, use paintComponent instead
// #Override
// public void paint(Graphics g) {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
square.paint(g2d);
ground.paint(g2d);
g2d.dispose();
}
}
public class Square {
Square square;
private int x, xa;
// static int y;
private int y;
private int ya;
private Game game;
// public static int fistX, fistY;
private int fistX, fistY;
// static int d = 60;
private int d = 60;
private int wide;
private boolean onGround;
public Square(Game game) {
this.game = game;
x = 100;
y = 100;
xa = 0;
ya = 0;
onGround = false;
wide = game.getWidth();
}
public void move() {
y += ya;
x += xa;
if (x < 0) {
x = 0;
} else if (x + 30 > game.getWidth()) {
x = game.getWidth() - 30;
}
if (y < 0) {
y = 0;
} else if (collision()) {
onGround = true;
y = game.ground.getBounds().y - d;
}
// if (x + xa > 0 && x + xa < game.getWidth() - 30) {
// x = x + xa;
// }
//
// if (y + ya > 0 && y + ya < game.getHeight() - 60) {
// // This was never going to do anything, look at the
// // end of the line...the `;` is going to prevent the
// // statemt ya += 10 from begin called within the loop
//// for (int i = 12; i < 0; i--);
// for (int i = 12; i < 0; i--) {
// ya += 10;
// }
// y = y + ya;
// }
// if (collision()) {
// y -= 10;
// onGround = true;
//
// }
//
// y += 10;
}
public void paint(Graphics2D g) {
g.setColor(Color.RED);
System.out.println(x + "x" + (y - d));
g.fillRoundRect(x, y, 30, d, 10, 10);
}
private boolean collision() {
return game.ground.getBounds().intersects(getBounds());
}
public Rectangle getBounds() {
return new Rectangle(x, y, 30, 60);
}
}
public class Ground {
private Game game;
public Ground(Game game) {
this.game = game;
}
public void paint(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, game.getHeight() - 30, game.getWidth(), 30);
}
public Rectangle getBounds() {
return new Rectangle(0, game.getHeight() - 30, game.getWidth(), 30);
}
}
public abstract class AbstractSquareAction extends AbstractAction {
private Square square;
public AbstractSquareAction(Square square) {
this.square = square;
}
public Square getSquare() {
return square;
}
}
public class PressLeftAction extends AbstractSquareAction {
public PressLeftAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().xa = -3;
System.out.println("pressLeft");
}
}
public class PressRightAction extends AbstractSquareAction {
public PressRightAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().xa = 3;
}
}
public class PressDownAction extends AbstractSquareAction {
public PressDownAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().ya = 30;
}
}
public class PressUpAction extends AbstractSquareAction {
public PressUpAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().ya -= 30;
}
}
public class ReleaseLeftAction extends AbstractSquareAction {
public ReleaseLeftAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().xa = 0;
}
}
public class ReleaseRightAction extends AbstractSquareAction {
public ReleaseRightAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().xa = 0;
}
}
public class ReleaseDownAction extends AbstractSquareAction {
public ReleaseDownAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().ya = 0;
}
}
public class ReleaseUpAction extends AbstractSquareAction {
public ReleaseUpAction(Square square) {
super(square);
}
#Override
public void actionPerformed(ActionEvent e) {
getSquare().ya = 0;
}
}
}

Categories

Resources