Java a moving animation (sprite) - java

Hello i`ve got a problem: when i run this code i get this:
on some java forum they said i need to add Graphics2DObject.clearRect(x1, y1, x2, y2);
(where`s x1 and y1 is coordinates of the image and x2 y2 is width height of the image.) when i add it to the code i get this:
The code(with added function):
Main:
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String []args){
Main b = new Main();
b.run();
}
private Sprite sprite;
private Animation a;
private ScreenManager s;
public double sk = 0;
private static final DisplayMode modes1[] = {
new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN),
};
//load images and add scenes
public void loadImages() {
Image face1 = new ImageIcon("C:\\1.jpg").getImage();
Image face2 = new ImageIcon("C:\\2.jpg").getImage();
a = new Animation();
a.addScene(face1, 50);
a.addScene(face2, 50);
sprite = new Sprite(a);
sprite.setVelocityX(0.3f);
sprite.setVelocityY(0.3f);
}
//main method called from main
public void run() {
s = new ScreenManager();
try {
DisplayMode dm = s.findFirstCompatibleMode(modes1);
s.setFullScreen(dm);
loadImages();
movieLoop();
}finally {
s.restoreScreen();
}
}
//play movie
public void movieLoop() {
long startingTime = System.currentTimeMillis();
long cumTime = startingTime;
while(cumTime - startingTime < 5000) {
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
//draw and update the screen
Graphics2D g = s.getGraphics();
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(20);
}catch(Exception ex) {
System.err.println("Error: " + ex);
}
}
}
// Graphics with new function
public void draw(Graphics g) {
g.drawImage(sprite.getImage(), Math.round(sprite.getX()), Math.round(sprite.getY()), null);
if(sk != 1){
g.clearRect(Math.round(sprite.getoX()),Math.round(sprite.getoY()),Math.round(sprite.getWidth()),Math.round(sprite.getHeight()));
}else{
sk = 1;
}
}
//update sprite
public void update(long timePassed) {
if(sprite.getX() < 0) {
sprite.setVelocityX(Math.abs(sprite.getVelocityX()));
} else if (sprite.getX() + sprite.getWidth() >= s.getWidth()) {
sprite.setVelocityX(-Math.abs(sprite.getVelocityX()));
}
if(sprite.getY() < 0) {
sprite.setVelocityY(Math.abs(sprite.getVelocityY()));
} else if (sprite.getY() + sprite.getHeight() >= s.getHeight()) {
sprite.setVelocityY(-Math.abs(sprite.getVelocityY()));
}
sprite.oldX();
sprite.oldY();
sprite.update(timePassed);
}
}
The sprite (movement class):
import java.awt.Image;
public class Sprite {
private Animation a;
private float x;
private float y;
private float vx;
private float vy;
private float ox;
private float oy;
//Constructor
public Sprite(Animation a) {
this.a = a;
}
// Get old x and y to delete them later
public void oldX(){
this.ox = x;
}
public void oldY(){
this.oy = y;
}
public float getoX(){
return ox;
}
public float getoY(){
return oy;
}
//Change position
public void update(long timePassed) {
x += vx * timePassed;
y += vy * timePassed;
a.update(timePassed);
}
//get x position
public float getX() {
return x;
}
//get y position
public float getY() {
return y;
}
//set x position
public void setX(float x) {
this.x = x;
}
//set y position
public void setY(float y) {
this.y = y;
}
// get sprite width
public int getWidth() {
return a.getImage().getWidth(null);
}
// get sprite height
public int getHeight() {
return a.getImage().getHeight(null);
}
//get horizontal velocity
public float getVelocityX() {
return vx;
}
//get vertical velocity
public float getVelocityY() {
return vy;
}
//set horizontal velocity
public void setVelocityX(float vx) {
this.vx = vx;
}
//set vertical velocity
public void setVelocityY(float vy) {
this.vy = vy;
}
//get sprite/image
public Image getImage() {
return a.getImage();
}
}
Screen manager class:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class ScreenManager {
private GraphicsDevice vc;
//Constructor // give vc access to monitor screen
public ScreenManager() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
//get all compatible DM's
public DisplayMode[] getCompatibleDisplayModes() {
return vc.getDisplayModes();
}
//compares DM passed in to vc and see if they match
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
DisplayMode goodModes[] = vc.getDisplayModes();
for(int x = 0; x <modes.length; x++){
for(int y = 0; y < goodModes.length; y++){
if(displayModesMatch(modes[x], goodModes[y])) {
return modes[x];
}
}
}
return null;
}
//get current DM
public DisplayMode getCurrentDisplayMode() {
return vc.getDisplayMode();
}
//checks if two modes match each other
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
return false;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
return false;
}
if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
return false;
}
return true;
}
//make frame full screen
public void setFullScreen(DisplayMode dm) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
if(dm != null && vc.isDisplayChangeSupported()) {
try{
vc.setDisplayMode(dm);
}catch(Exception e) {System.out.println("");}
}
f.createBufferStrategy(2);
}
//we will set Graphics object = to this
public Graphics2D getGraphics() {
Window w = vc.getFullScreenWindow();
if(w != null) {
BufferStrategy s = w.getBufferStrategy();
return (Graphics2D)s.getDrawGraphics();
}
else {
return null;
}
}
//updates display
public void update() {
Window w = vc.getFullScreenWindow();
if(w != null) {
BufferStrategy s = w.getBufferStrategy();
if(!s.contentsLost()) {
s.show();
}
}
}
//returns full screen window
public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}
//get width
public int getWidth() {
Window w = vc.getFullScreenWindow();
if(w != null) {
return w.getWidth();
}
else {
return 0;
}
}
//get height
public int getHeight() {
Window w = vc.getFullScreenWindow();
if(w != null) {
return w.getHeight();
}
else {
return 0;
}
}
//get out of full screen
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null) {
w.dispose();
vc.setFullScreenWindow(null);
}
}
//create image compatible with monitor
public BufferedImage createCompatibleImage(int w, int h, int t) {
Window win = vc.getFullScreenWindow();
if(win != null) {
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, t);
}
return null;
}
}/////////END///////////
Animation class
import java.util.ArrayList;
import java.awt.Image;
public class Animation {
private ArrayList scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
//Constructor
public Animation() {
scenes = new ArrayList();
totalTime = 0;
start();
}
//add scenes to the array list and set time for each scene
public synchronized void addScene(Image i, long t) {
totalTime += t;
scenes.add(new Onescene(i, totalTime));
}
//start animation from beginning
public synchronized void start() {
movieTime = 0;
sceneIndex = 0;
}
//change scenes
public synchronized void update(long timePassed) {
if(scenes.size() > 1 ) {
movieTime += timePassed;
if(movieTime >= totalTime) {
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime) {
sceneIndex++;
}
}
}
//get animations current scene
public synchronized Image getImage() {
if(scenes.size() == 0) {
return null;
}
else {
return getScene(sceneIndex).pic;
}
}
//get scene
private Onescene getScene(int x) {
return (Onescene)scenes.get(x);
}
/////PRIVAT INNER CLASS/////
private class Onescene{
Image pic;
long endTime;
public Onescene(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
}////END/////
EDIT:
Could someone try running this code ?
Maybe You'll find the mistakes I made...

clearRect goes before drawImage.

I also had a problem with this and you need a "do while loop" in stead of while loop
like this:
do{
Graphics2D g = s.getGraphics();
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
//draw and update
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(20);
}catch(Exception ex){
System.out.println("Can't sleep :(");
}
}
while(cumTime - startingTime < 20000); //the Time
And the clearRect must be ABOVE the drawImage

Your loop looks a bit incorrect
Graphics2D g = s.getGraphics();
while(cumTime - startingTime < 5000) {
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
//draw and update the screen
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(20);
}catch(Exception ex) {
System.err.println("Error: " + ex);
}
}
}
also try this for your draw method.
// Graphics with new function
public void draw(Graphics g) {
g.clearRect(0,0,800,600);
g.drawImage(sprite.getImage(), Math.round(sprite.getX()), Math.round(sprite.getY()), null);
}

Related

Can't seem to get a missile to bounce of the walls on a simple java programming game

Okay, so I am trying to create a simple game where you shoot a missile and if it hits a wall it has to bounce back expect for the bottom wall were it will inactivate the missile to allow me to shoot a new missile.
import java.awt.*;
public class Project2{
public static final int PANEL_WIDTH = 300;
public static final int PANEL_HEIGHT = 300;
public static final int SLEEP_TIME = 50;
public static final Color SHOOTER_COLOR = Color.RED;
public static final Color BACKGROUND_COLOR = Color.WHITE;
public static final int SHOOTER_SIZE = 20; //diameter of the shooter
public static final int GUN_SIZE = 10; //length og the gun
public static final int SHOOTER_POSITION_Y = PANEL_HEIGHT - SHOOTER_SIZE;
public static final int SHOOTER_INITIAL_POSITION_X = 150;
public static int shooterPositionX;
public static int gunPositionX;
public static int gunPositionY;
public static int targetPositionX;
public static final Color TARGET_COLOR = Color.BLUE;
public static final int TARGET_POSITION_Y = 50;
public static final int TARGET_SIZE = 20;
public static final int KEY_SPACE =32;
public static final int KEY_PAGE_UP = 33;
public static final int KEY_HOME = 36;
public static final int KEY_LEFT_ARROW = 37;
public static final int KEY_UP_ARROW = 38;
public static final int KEY_RIGHT_ARROW = 39;
public static final int KEY_DOWN_ARROW = 40;
public static double missilePositionX;
public static double missilePositionY;
public static double missileDeltaX;
public static double missileDeltaY;
public static boolean missileActive;
public static final Color MISSILE_COLOR = Color.BLACK;
public static final int MISSILE_SIZE =2;
public static final double MISSILE_SPEED =1.0;
public static int hitCount;
public static final int TARGET_DELTA_X= 1;
public static int targetDeltaX;
// public static final Random.nextDouble();
public static void initialize(){
shooterPositionX = SHOOTER_INITIAL_POSITION_X;
gunPositionX = 0;
gunPositionY = GUN_SIZE;
targetPositionX = 150;
missileActive = false;
hitCount = 0;
targetDeltaX = 0;
}
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(PANEL_WIDTH, PANEL_HEIGHT);
Graphics g = panel.getGraphics( );
initialize();
startGame(panel, g);
}
public static void drawAll(Graphics g){
g.setColor(Color.BLACK);
g.drawString("Project 2 by JR", 10, 15); // fix ""
g.drawString("Hits: " + hitCount, 10, 30);
// for(int i=0, i<=hitCount; i++){
//
// }
// Hits: followed by a number of stars corresponding to the hit count.
//Do not do this by calling g.drawString in a loop. Instead, make a single string with a
//for loop, appending the appropriate number of stars. Then call g.drawString once.
drawShooter(g, SHOOTER_COLOR);
drawTarget(g, TARGET_COLOR);
}
public static void startGame(DrawingPanel panel, Graphics g) {
for (int i = 0; i <= 10000; i++) {
panel.sleep(SLEEP_TIME);
handleKeys(panel, g);
// moveTarget(g);
drawAll(g);
moveMissile(g);
}
}
public static void drawShooter(Graphics g, Color c){
g.setColor(c);
g.fillOval(shooterPositionX-SHOOTER_SIZE/2, SHOOTER_POSITION_Y-SHOOTER_SIZE, SHOOTER_SIZE, SHOOTER_SIZE);
g.drawLine(shooterPositionX, SHOOTER_POSITION_Y-SHOOTER_SIZE, shooterPositionX+gunPositionX, SHOOTER_POSITION_Y-SHOOTER_SIZE - GUN_SIZE);
}
public static void drawTarget(Graphics g, Color c){
g.setColor(c);
g.fillOval(targetPositionX-TARGET_SIZE/2, TARGET_POSITION_Y-TARGET_SIZE, TARGET_SIZE, TARGET_SIZE);
g.drawLine(targetPositionX-TARGET_SIZE, TARGET_POSITION_Y+TARGET_SIZE/2, targetPositionX+TARGET_SIZE, TARGET_POSITION_Y+TARGET_SIZE/2);
}
public static void moveShooter(Graphics g, int deltaX){
drawShooter(g, BACKGROUND_COLOR);
shooterPositionX += deltaX;
drawShooter(g, SHOOTER_COLOR);
//does not allow you to move any part of the shooter off the screen.
}
public static void handleKeys(DrawingPanel panel, Graphics g){
int keyCode = panel.getKeyCode();
if (keyCode == KEY_SPACE)
reset(g);
else if (keyCode == KEY_RIGHT_ARROW)
moveShooter(g,1);
else if (keyCode == KEY_LEFT_ARROW)
moveShooter(g,-1);
else if (keyCode == KEY_HOME)
moveGun(g,1);
else if (keyCode == KEY_PAGE_UP)
moveGun(g,-1);
else if(keyCode == KEY_UP_ARROW)
shootMissile(g);
}
public static void reset(Graphics g){
g.setColor(BACKGROUND_COLOR);
g.fillRect(0,0, PANEL_WIDTH, PANEL_HEIGHT);
initialize();
}
public static void moveGun(Graphics g, int deltaX){
drawShooter(g, BACKGROUND_COLOR);
gunPositionX += deltaX;
drawShooter(g, SHOOTER_COLOR);
//Do not let gunPositionX have an absolute value larger than SHOOTER_SIZE.
}
public static void shootMissile(Graphics g){
if(missileActive == false){
missileActive = true;
missilePositionX = shooterPositionX;
missilePositionY = SHOOTER_POSITION_Y-SHOOTER_SIZE;
missileDeltaX = 0;
missileDeltaY = 0;
missilePositionX += missileDeltaX;
missilePositionY += missileDeltaY;
}
}
public static void moveMissile(Graphics g){
if(missileActive){
drawMissile(g, BACKGROUND_COLOR);
missilePositionX += missileDeltaX;
missilePositionY += missileDeltaY;
drawMissile(g, MISSILE_COLOR);
if(missilePositionY <= 0){
missileDeltaY = -Math.abs(missileDeltaY);
missilePositionY += missileDeltaY;
}
else if(missilePositionX >= 300){
missilePositionX += missileDeltaX;
}
else if(missilePositionX <=0){
missileDeltaX = -Math.abs(missileDeltaX);
missilePositionX += missileDeltaX;
}
else if(missilePositionY >= 300){
drawMissile(g, BACKGROUND_COLOR);
missileActive = false;
}
if( detectHitTarget((int)missilePositionX, (int)missilePositionY, (int)MISSILE_SIZE/2, targetPositionX, TARGET_POSITION_Y, TARGET_SIZE/2)){
hitCount ++;
drawMissile(g, BACKGROUND_COLOR);//erase the missile;
missileActive = false;
}
}
}
public static void drawMissile(Graphics g, Color c){
g.setColor(c);
g.fillOval((int)missilePositionX, (int)missilePositionY, MISSILE_SIZE, MISSILE_SIZE);
missileDeltaY = - gunPositionY * MISSILE_SPEED;
missileDeltaX = gunPositionX * MISSILE_SPEED;
}
public static boolean detectHitTarget(int x1, int y1, int r1, int x2, int y2, int r2){
if(Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)) <= r1 + r2){
return true;
}else{
return false;}
}
public static void moveTarget(Graphics g){
// drawTarget(g, BACKGROUND_COLOR);
// targetPositionX += targetDeltaX;
// if(targetPositionX >= 0){
// targetPositionX = +Math.abs(targetDelta);
// }
// else if(targetPositionX <=300){
// targetPositionX = -Math.abs(targetDelta);
// }
// if(Random >= .98){
// targetDeltaX = 0;
// }
// else if(Random >= .96){
// targetDeltaX =1;
// }
// else if(Random >= .94){
// targetDleta = -1;
// }
}
}
so far the ball is just getting 'stuck' to the top wall or not even getting 'stuck' in the case of the side walls just going off the panel, instead of bouncing off and I can't seem to figure it out.
Drawing Panel
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.ArrayList;
public class DrawingPanel implements ActionListener {
private static final String versionMessage =
"Drawing Panel version 1.1, January 25, 2015";
private static final int DELAY = 100; // delay between repaints in millis
private static final boolean PRETTY = false; // true to anti-alias
private static boolean showStatus = false;
private static final int MAX_KEY_BUF_SIZE = 10;
private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position
private volatile MouseEvent click; // stores the last mouse click
private volatile boolean pressed; // true if the mouse is pressed
private volatile MouseEvent move; // stores the position of the mouse
private ArrayList<KeyInfo> keys;
// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
keys = new ArrayList<KeyInfo>();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
statusBar = new JLabel(" ");
statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
statusBar.setText(versionMessage);
panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width, height));
panel.add(new JLabel(new ImageIcon(image)));
click = null;
move = null;
pressed = false;
// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
pressed = false;
move = e;
if (showStatus)
statusBar.setText("moved (" + e.getX() + ", " + e.getY() + ")");
}
public void mousePressed(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("pressed (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseDragged(MouseEvent e) {
pressed = true;
move = e;
if (showStatus)
statusBar.setText("dragged (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseReleased(MouseEvent e) {
click = e;
pressed = false;
if (showStatus)
statusBar.setText("released (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {
// System.out.println("mouse entered");
panel.requestFocus();
}
};
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);
new DrawingPanelKeyListener();
g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.BLACK);
if (PRETTY) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.1f));
}
frame = new JFrame("Drawing Panel");
frame.setResizable(false);
try {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that this works in an applet
} catch (Exception e) {}
frame.getContentPane().add(panel);
frame.getContentPane().add(statusBar, "South");
frame.pack();
frame.setVisible(true);
toFront();
frame.requestFocus();
// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}
public void showMouseStatus(boolean f) {
showStatus = f;
}
public void addKeyListener(KeyListener listener) {
panel.addKeyListener(listener);
panel.requestFocus();
}
// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return g2;
}
// set the background color of the drawing panel
public void setBackground(Color c) {
panel.setBackground(c);
}
// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
panel.repaint();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}
// close the drawing panel
public void close() {
frame.dispose();
}
// makes drawing panel become the frontmost window on the screen
public void toFront() {
frame.toFront();
}
// return panel width
public int getWidth() {
return width;
}
// return panel height
public int getHeight() {
return height;
}
// return the X position of the mouse or -1
public int getMouseX() {
if (move == null) {
return -1;
} else {
return move.getX();
}
}
// return the Y position of the mouse or -1
public int getMouseY() {
if (move == null) {
return -1;
} else {
return move.getY();
}
}
// return the X position of the last click or -1
public int getClickX() {
if (click == null) {
return -1;
} else {
return click.getX();
}
}
// return the Y position of the last click or -1
public int getClickY() {
if (click == null) {
return -1;
} else {
return click.getY();
}
}
// return true if a mouse button is pressed
public boolean mousePressed() {
return pressed;
}
public synchronized int getKeyCode() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyCode;
}
public synchronized char getKeyChar() {
if (keys.size() == 0)
return 0;
return keys.remove(0).keyChar;
}
public synchronized int getKeysSize() {
return keys.size();
}
private synchronized void insertKeyData(char c, int code) {
keys.add(new KeyInfo(c,code));
if (keys.size() > MAX_KEY_BUF_SIZE) {
keys.remove(0);
// System.out.println("Dropped key");
}
}
private class KeyInfo {
public int keyCode;
public char keyChar;
public KeyInfo(char keyChar, int keyCode) {
this.keyCode = keyCode;
this.keyChar = keyChar;
}
}
private class DrawingPanelKeyListener implements KeyListener {
int repeatCount = 0;
public DrawingPanelKeyListener() {
panel.addKeyListener(this);
panel.requestFocus();
}
public void keyPressed(KeyEvent event) {
// System.out.println("key pressed");
repeatCount++;
if ((repeatCount == 1) || (getKeysSize() < 2))
insertKeyData(event.getKeyChar(),event.getKeyCode());
}
public void keyTyped(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
repeatCount = 0;
}
}
}
Your drawMissile method which is called inside moveMissile method everytime it is called it is re-initializing the missileDeltaY and missileDeltaX variables.
missileDeltaY = - gunPositionY * MISSILE_SPEED;
missileDeltaX = gunPositionX * MISSILE_SPEED;
So no matter what calculations take place in the rest body of the moveMissile method, those variables will take their default values hence no change of the trajectory when hitting the boundaries.
I have made some changes in your methods to help you figure out the solution, it looks like it is working if you shoot the missile to hit the left boundary
shootMissile
public static void shootMissile(Graphics g){
if(missileActive == false){
missileActive = true;
missilePositionX = shooterPositionX;
missilePositionY = SHOOTER_POSITION_Y-SHOOTER_SIZE;
missileDeltaX = 0;
missileDeltaY = 0;
missilePositionX += missileDeltaX;
missilePositionY += missileDeltaY;
missileDeltaY = - gunPositionY * MISSILE_SPEED;
missileDeltaX = gunPositionX * MISSILE_SPEED;
}
}
moveMissile
public static void moveMissile(Graphics g){
if(missileActive){
drawMissile(g, BACKGROUND_COLOR);
missilePositionX += missileDeltaX;
missilePositionY += missileDeltaY;
drawMissile(g, MISSILE_COLOR);
if(missilePositionY <= 0){
missileDeltaY = Math.abs(missileDeltaY);
missilePositionY += missileDeltaY;
}
if(missilePositionX >= 300){
missilePositionX += -Math.abs(missileDeltaX);
}
if(missilePositionX <=0){
missileDeltaX = Math.abs(missileDeltaX);
missilePositionX += missileDeltaX;
}
if(missilePositionY >= 300){
drawMissile(g, BACKGROUND_COLOR);
missileActive = false;
}
if( detectHitTarget((int)missilePositionX, (int)missilePositionY, (int)MISSILE_SIZE/2, targetPositionX, TARGET_POSITION_Y, TARGET_SIZE/2)){
hitCount ++;
drawMissile(g, BACKGROUND_COLOR);//erase the missile;
missileActive = false;
}
}
}
drawMissile
public static void drawMissile(Graphics g, Color c){
g.setColor(c);
g.fillOval((int)missilePositionX, (int)missilePositionY, MISSILE_SIZE, MISSILE_SIZE);
}

Java 2D Game adding bombs

I'm very new to game design (this is my first attempt) and this project will be used to create an android game.
I'm trying to make a simple game (as simple as possible).
What I need:
A background
a ship (that can move left an right at the bottom of the screen)
Enemies (Bombs dropping down from the sky)
projectiles (to shoot bombs with, shoot straight up)
Score (in the upper corner)
I have studied this tutorial:
http://www.kilobolt.com/game-development-tutorial.html
and changed code to get this:
http://i297.photobucket.com/albums/mm231/mabee84/Battleship.png
the black rectangles are projectiles.
Now I need to create the bombs but I can't figure out how to implement them.
they need to spawn at fixed y-value and a random x-value (within the screen)
Upon shooting on the bombs they should die but if bombs hit the ship game is over.
Please help i'm a bit stuck.
package kiloboltgame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.ArrayList;
public class StartingClass extends Applet implements Runnable, KeyListener {
private Ship ship;
public static Bomb b1, b2;
public static int score = 0;
private Font font = new Font(null, Font.BOLD, 30);
private Image image, Battleship, Background, Bomb;
private static Background bg1, bg2;
private URL base;
private Graphics second;
#Override
public void init() {
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("BattleShip");
try{
base = getDocumentBase();
}catch (Exception e){
//TODO: handle exception
}
//Image Setups
Battleship = getImage(base, "data/Battleship.png");
Background = getImage(base, "data/Background.png");
Bomb = getImage(base, "data/Bomb1.png");
}
#Override
public void start() {
bg1 = new Background(0, 0);
bg2 = new Background(800, 0);
ship = new Ship();
b1 = new Bomb(340, 100);
b2 = new Bomb(700, 100);
Thread thread = new Thread(this);
thread.start();
}
#Override
public void stop() {
// TODO Auto-generated method stub
}
#Override
public void destroy() {
// TODO Auto-generated method stub
}
#Override
public void run() {
while (true) {
ship.update();
ArrayList projectiles = ship.getProjectiles();
for(int i = 0; i < projectiles.size(); i++){
Projectile p = (Projectile) projectiles.get(i);
if(p.isVisible() == true){
p.update();
}else{
projectiles.remove(i);
}
}
b1.update();
b2.update();
bg1.update();
bg2.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void update(Graphics g) {
if(image == null){
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
#Override
public void paint(Graphics g) {
g.drawImage(Background, bg1.getBgX(), bg1.getBgY(), this);
ArrayList projectiles = ship.getProjectiles();
for(int i = 0; i < projectiles.size(); i++){
Projectile p = (Projectile) projectiles.get(i);
g.setColor(Color.BLACK);
g.fillRect(p.getX(), p.getY(), 5, 10);
}
g.drawImage(Battleship, ship.getCenterX() + 230, ship.getCenterY() -23, this);
g.drawImage(Bomb, b1.getCenterX() - 20, b1.getCenterY() - 20, this);
g.drawImage(Bomb, b2.getCenterX() - 20, b2.getCenterY() - 20, this);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawString(Integer.toString(score), 710, 30);
}
#Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
ship.moveLeft();
break;
case KeyEvent.VK_RIGHT:
ship.moveRight();
break;
case KeyEvent.VK_CONTROL:
ship.shoot();
score = score +100;
break;
}
}
#Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
ship.stop();
break;
case KeyEvent.VK_RIGHT:
ship.stop();
break;
}
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static Background getBg1() {
return bg1;
}
}
package kiloboltgame;
import java.util.ArrayList;
public class Ship {
//In Java, Class Variables should be private so that only its methods can change them.
private int centerX = 100;
private int centerY = 382;
private int speedX = 0;
private int speedY = 1;
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
public void update() {
// Moves Character or Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
} else if (speedX == 0) {
System.out.println("Do not scroll the background.");
} else {
if (centerX <= 440) {
centerX += speedX;
} else {
System.out.println("Scroll Background Here");
}
}
// Updates Y Position
if (centerY + speedY >= 382) {
centerY = 382;
}else{
centerY += speedY;
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= -230) {
centerX = -229;
}
}
public void moveRight() {
speedX = 6;
}
public void moveLeft() {
speedX = -6;
}
public void shoot(){
Projectile p = new Projectile(centerX + 285, centerY -10);
projectiles.add(p);
}
public ArrayList getProjectiles(){
return projectiles;
}
public void stop() {
speedX = 0;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public int getSpeedX() {
return speedX;
}
public int getSpeedY() {
return speedY;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
}
package kiloboltgame;
public class Background {
private int bgX, bgY, speedX;
public Background(int x, int y){
bgX = x;
bgY = y;
speedX = 0;
}
public void update() {
bgX += speedX;
if (bgX <= -800){
bgX += 1600;
}
}
public int getBgX() {
return bgX;
}
public int getBgY() {
return bgY;
}
public int getSpeedX() {
return speedX;
}
public void setBgX(int bgX) {
this.bgX = bgX;
}
public void setBgY(int bgY) {
this.bgY = bgY;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
}
public class Projectile {
private int x, y, speedY;
private boolean visible;
public Projectile(int startX, int startY) {
x = startX;
y = startY;
speedY = -7;
visible = true;
}
public void update() {
y += speedY;
if(y > 480){
visible = false;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedY() {
return speedY;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
package kiloboltgame;
public class Enemy {
private int maxHealth, currentHealth, power, speedX, centerX, centerY;
private Background bg = StartingClass.getBg1();
//Behavioral Methods
public void update(){
centerX += speedX;
speedX = bg.getSpeedX();
}
public void die(){
}
public void attack(){
}
public int getMaxHealth() {
return maxHealth;
}
public int getCurrentHealth() {
return currentHealth;
}
public int getPower() {
return power;
}
public int getSpeedX() {
return speedX;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public Background getBg() {
return bg;
}
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
public void setCurrentHealth(int currentHealth) {
this.currentHealth = currentHealth;
}
public void setPower(int power) {
this.power = power;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setBg(Background bg) {
this.bg = bg;
}
}
package kiloboltgame;
public class Bomb extends Enemy {
public Bomb(int centerX, int centerY) {
setCenterX(centerX);
setCenterY(centerY);
}
}
This is all code that i have so far (I know the background is f*ed since the game this is based on is scrolling right and i haven't fixed it yet.
I recommend putting all object creation in a seperate part of the program. I'd make a BombFactory with a makeBomb mathod that returns a new Bomb instance. Inside the factory, figure out the x-coordinate, for instance using a randomiser. As parameters, you could specify a y-coordinate and possibly an upper and lower bound for the x. This way you can make new Bombs on the fly.
public class BombFactory {
private final Random rand;
public BombFactory() {
this.rand = new Random();
}
public Bomb makeBomb(int lowerboundX, int rangeX, int yPos) {
final int xPos = lowerboundX + rand.nextInt(rangeX);
return new Bomb(xPos, yPos);
}
}
As for the behaviour, I'd look into inheritance and interfaces some more. I see a lot of methods occurring more than once. You generally want to avoid that kind of duplication. You can start by taking all the methods having something to do with coords or movement and putting them in an abstract base class.
You can make a method inside Enemy that checks for a collision and responds to that in different ways, depending on how the subclass overrides it. In case of a Bomb, it would probably always kill itself and whatever it came in contact with.

Typing game: choosing two words

I'm creating a game like zType
and I have a problem in validating the input. As you can see in the picture below it select two words. How can I fix this problem, please help.
This is My Game class
import java.awt.Canvas;
import java.awt.event.KeyListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable, KeyListener
{
public static final int WIDTH = 640;
public static final int HEIGHT = 680;
public final String TITLE = "Game";
private boolean running = false;
private Thread thread;
private BufferedImage spriteSheet = null;
private BufferedImage background = null;
public int level = 1;
public int fps = 0;
public int score = 0;
private int enemy_count = 15;
private int enemy_killed = 0;
private int enemy_notkilled = 0;
private boolean target = false;
public String t = "";
private Controller c;
private Textures tex;
private LinkedList<Enemy> e = new LinkedList<Enemy>();
private Enemy enemy;
public void init()
{
requestFocus();
try
{
spriteSheet = ImageIO.read(getClass().getResource("/sprite_sheet.png"));
background = ImageIO.read(getClass().getResource("/background.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
tex = new Textures(this);
c = new Controller(tex, this);
addKeyListener(this);
c.createEnemy(enemy_count);
e = c.getEnemy();
}
private synchronized void start()
{
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop()
{
if(!running)
return;
running = false;
try
{
thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.exit(1);
}
public void run()
{
init();
long lastTime = System.nanoTime();
final double amountOfTicks= 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1)
{
update();
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
fps = frames;
frames = 0;
}
}
stop();
}
private void update()
{
c.update();
if(enemy_killed + enemy_notkilled >= enemy_count)
{
e.clear();
t = "";
level++;
enemy_killed = 0;
enemy_notkilled = 0;
c.createEnemy(enemy_count);
}
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(background, 0, 0, null);
c.render(g);
g.dispose();
bs.show();
}
public void keyPressed(KeyEvent ek)
{
int key = ek.getKeyCode();
char character = Character.toLowerCase(ek.getKeyChar());
boolean result = isValid(key);
if(result && !target)
{
for(int i = 0; i < e.size(); i++)
if(e.get(i).getFirstLetter() == character && e.get(i).getOnScreen())
{
enemy = e.get(i);
t = enemy.getText();
if(enemy.getCurrentIndex() == 0)
enemy.addCurrentIndex();
target = true;
System.out.println("---"+enemy.text);
break;
}
}
else if(result && t.charAt(enemy.getCurrentIndex()) == character)
enemy.addCurrentIndex();
}
public void keyReleased(KeyEvent ek){ }
public void keyTyped(KeyEvent ek){ }
public static void main(String[] args)
{
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public boolean isValid(int key)
{
return key >= 65 && key <= 90 ? true : false;
}
public BufferedImage getSpriteSheet()
{
return spriteSheet;
}
public int getEnemy_count()
{
return enemy_count;
}
public void setEnemy_count(int enemy_count)
{
this.enemy_count = enemy_count;
}
public int getEnemy_killed()
{
return enemy_killed;
}
public void setEnemy_killed(int enemy_killed)
{
this.enemy_killed = enemy_killed;
}
public int getEnemy_notkilled()
{
return enemy_notkilled;
}
public void setEnemy_notkilled(int enemy_notkilled)
{
this.enemy_notkilled = enemy_notkilled;
}
public void addScore(int k)
{
score += k;
}
public void falseTarget()
{
target = false;
}
public String getT()
{
return t;
}
public void setT(String k)
{
t = k;
}
}
This is my Enemy Class
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
public class Enemy
{
private double x ,y;
public String text;
private char firstLetter;
private AttributedString as;
private Controller c;
private Textures tex;
private Game game;
private int currentIndex = 0;
private int textLength = 0;
private double speed = 0.0;
private int stringWidth = 0;
private boolean onScreen = false;
public Enemy(double x, double y, double speed, Textures tex, Controller c, Game game, String text, int stringWidth)
{
this.x = x;
this.y = y;
this.tex = tex;
this.text = text;
this.game = game;
this.c = c;
this.speed = speed;
firstLetter = this.text.charAt(0);
textLength = this.text.length();
this.stringWidth = stringWidth;
}
public void update()
{
y += speed;
if(y >= 0)
onScreen = true;
if(currentIndex >= textLength)
{
game.addScore(5);
game.setT("");
c.removeEnemy(this);
game.setEnemy_killed(game.getEnemy_killed() + 1);
game.falseTarget();
}
if(y >= Game.HEIGHT - 50)
{
//game.decreaseHealth();
game.setT("");
c.removeEnemy(this);
game.setEnemy_notkilled(game.getEnemy_notkilled() + 1);
game.falseTarget();
game.t ="";
}
}
public void render(Graphics g)
{
g.drawImage(tex.enemy, (int)x, (int)y, null);
as = new AttributedString(text);
if(currentIndex >= 1)
as.addAttribute(TextAttribute.FOREGROUND, Color.WHITE, 0, currentIndex);
as.addAttribute(TextAttribute.FONT, new Font("Consolas", Font.BOLD, 12), 0, text.length());
g.drawString(as.getIterator(), (int)x + getAdd(stringWidth), (int)y + 13);
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public String getText()
{
return text;
}
public char getFirstLetter()
{
return firstLetter;
}
public void addCurrentIndex()
{
currentIndex++;
}
public int getCurrentIndex()
{
return currentIndex;
}
public int getTextLength()
{
return textLength;
}
public int getAdd(int width)
{
return (96 / 2) - (width / 2);
}
public boolean getOnScreen()
{
return onScreen;
}
}
The problem is that you're calling falseTarget() whenever an enemy moves out of the screen. In your example, you assigned target = true when you pressed the 'o' (for the enemy named "owe"), but then a different enemy (maybe "jet", maybe an enemy that is no longer visible) finished "dropping" out of the screen (y >= Game.HEIGHT - 50), so you called falseTarget() - and thus, when you pressed 'i' you started a new enemy ("ice").
What you want to do is call falseTarget() in this case only if the enemy that dropped out of the screen is the one that's currently being targeted:
if(y >= Game.HEIGHT - 50)
{
//game.decreaseHealth();
//game.setT(""); // <-- Commented this line out
c.removeEnemy(this);
game.setEnemy_notkilled(game.getEnemy_notkilled() + 1);
if(game.getT().equals(text)) // <-- Added this if before calling falseTarget()
{ // and clearing the game's current enemy text
game.falseTarget();
game.t ="";
}
}

why wont my program paintComponent() if I set an ImageIcon and Image?

I've tried to find similar questions, but I think I have a unique situation here.
I am programming a game in java, my main class creates a frame which adds a component class which is an extension of JPanel. Basically, I draw these ovals and they move around and do different things, and I want to implement a method inside of one my classes which will use an image instead of an oval. However, anytime I try to create and image from a file, the program will not run the overridden "paintComponent(Graphics g)" method.
My main:
package mikeengine;
import javax.swing.JFrame;
public class MikeEngine {
static final int fx = 1300;
static final int fy = 800;
static final int px = 1292;
static final int py = 767;
static interactivePanel levelPanel;
public static void pause() {
try {
Thread.sleep(10); // wait 10ms
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("MEngine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(fx, fy);
frame.setResizable(false);
frame.setVisible(true);
levelPanel = new interactivePanel();
addship();
frame.getContentPane().add(levelPanel);
levelPanel.requestFocusInWindow();
while (true) {
pause();
levelPanel.move();
levelPanel.repaint();
}
}
static void addship() {
PlayerShip ship = new PlayerShip(100, 25, 25, px, 0, py, 0);
ship.setGraphic("C:/Users/asdf/Documents/NetBeansProjects/mikeEngine/src/mikeengine/res/right-arrow.jpg");
levelPanel.addObject(ship);
for (int i = 0; i < 5; i++) {
PlayerShip ship2 = new PlayerShip((int) (Math.random() * 1000) + 100, (int) (Math.random() * 1000) + 100, 25, px, px / 2, py, 0);
ship2.setMovement((int) (Math.random() * 10) / 5, (int) (Math.random() * 10) / 2);
levelPanel.addObject(ship2);
}
}
}
now, if I comment out the line:
ship.setGraphic("C:/Users/asdf/Documents/NetBeansProjects/mikeEngine/src/mikeengine/res/right-arrow.jpg");
everything renders perfectly.
I don't even paint the image that is going to be created. The image variable is just part of a class and it is currently just being created and doing nothing, so I don't understand how it affects the paintcomponentMethod(), if the paintComponent() isn't even told to try to paint the image ever.
The "PlayerShip" class extends the abstract "OnScreenObject" class (you really shouldn't need to see the code for PlayerShip, it only overrides one irrelevant method).
OnScreenObject looks like so:
package mikeengine;
import java.awt.Color;
import java.awt.Image;
import javax.swing.ImageIcon;
public abstract class OnScreenObject {
private ImageIcon graphic;
Image g;
protected int xmin;
protected int ymin;
protected int xsize;
protected int ysize;
protected int rise;
protected int run;
protected int containerYMax;
protected int containerYMin;
protected int containerXMax;
protected int containerXMin;
protected boolean visible;
protected boolean allowedOffscreen;
protected boolean isSelected;
protected Color color;
OnScreenObject(int x, int y, int sizeX, int sizeY, int cxMax, int cxMin, int cyMax, int cyMin) {
xmin = x;
ymin = y;
xsize = sizeX;
ysize = sizeY;
containerXMax = cxMax;
containerXMin = cxMin;
containerYMax = cyMax;
containerYMin = cyMin;
//
rise = 0;
run = 0;
visible = true;
allowedOffscreen = false;
isSelected = false;
}
public int getXMin() {
return xmin;
}
public int getYMin() {
return ymin;
}
public int getXMax() {
return xmin + xsize;
}
public int getYMax() {
return ymin + ysize;
}
public int getXSize() {
return xsize;
}
public int getYSize() {
return ysize;
}
public Color getColor() {
return color;
}
public boolean getAllowedOffscreen() {
return allowedOffscreen;
}
public boolean getVisible() {
return visible;
}
public int getRun() {
return run;
}
public ImageIcon getGraphic() {
return graphic;
}
public boolean isWithin(int x, int y) {
return x >= xmin && x <= getXMax() && y >= ymin && y <= getYMax();
}
public void setXMin(int x) {
xmin = x;
}
public void setYMin(int y) {
ymin = y;
}
public void setXSize(int x) {
xsize = x;
}
public void setYSize(int y) {
ysize = y;
}
public void setGraphic(String setto) {
try{
graphic = new ImageIcon("setto");
g=graphic.getImage();
System.out.println("tried");
} catch(Exception e){
System.out.println("caught:" + e);
}
}
public void setAllowedOffscreen(boolean allowed) {
allowedOffscreen = allowed;
}
public void setMovement(int riseM, int runM) {
rise = riseM * -1;//rise means to go up, and negative will move it up
run = runM;
}
public void nudge(boolean horizontal, int amount) {
if (horizontal) {
run += amount;
} else {
rise += amount;
}
}
public void setVisible(boolean vis) {
visible = vis;
}
public void setColor(Color c) {
color = c;
}
public boolean checkCollide(OnScreenObject other) {
if (other.getYMax() < getYMin()) { //if other object is above this
return false;
}
if (other.getYMin() > getYMax()) {//if other object is below this
return false;
}
if (other.getXMax() < getXMin()) {//if other is to the left
return false;
}
if (other.getXMin() > getXMax()) {//if other is to the right
return false;
}
return true;
}
public void move() {
if (!allowedOffscreen) {
checkEdge();
}
xmin += run;
ymin += rise;
}
protected abstract void checkEdge();
}
The interactivePanel class is the one that extends JPanel:
package mikeengine;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class interactivePanel extends JPanel {
ArrayList<OnScreenObject> objects;
IClick myClick;
Ipress myType;
PlayerShip currentShip;
interactivePanel() {
objects = new ArrayList<>();
myClick = new IClick();
myType = new Ipress();
this.addMouseListener(myClick);
this.addKeyListener(myType);
}
#Override
public void paintComponent(Graphics g) {
System.out.println("painting");
paintBackground(g);
paintObjects(g);
checkClick();
checkPress();
}
public void move() {
System.out.println("here2");
checkDeadShip();//also sets current ship
moveObjects();//also removes invisible
checkCollisions();
} // end method move
private void checkClick() {
if (!myClick.getClicked()) {
return;
}
for (int i = 0; i < objects.size(); i++) {
OnScreenObject current = objects.get(i);
if (current.isWithin(myClick.getX(), myClick.getY())) {
System.out.println("CLICKED");
}
}
}
private void paintBackground(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
}
private void paintObjects(Graphics g) {
for (int i = 0; i < objects.size(); i++) {
OnScreenObject current = objects.get(i);
g.setColor(current.getColor());
g.fillOval(current.getXMin(), current.getYMin(), current.getXSize(), current.getYSize());
}
}
public void addObject(OnScreenObject toAdd) {
objects.add(toAdd);
}
private void checkPress() {
if (myType.getDown()) {
objects.get(0).nudge(false, 3);
}
if (myType.getUp()) {
objects.get(0).nudge(false, -3);
}
if (myType.getLeft()) {
objects.get(0).nudge(true, -3);
}
if (myType.getRight()) {
objects.get(0).nudge(true, 3);
}
if (myType.getSpace()) {
fire();
}
}
private void fire() {
OnScreenObject shotBullet = new Bullet(currentShip.getXMax(), ((currentShip.getYMax() - currentShip.getYMin()) / 2) + currentShip.getYMin(), 5, getWidth(), 0, getHeight(), 0);
int shipBonus = 0;
if (currentShip.getRun() > 0) {
shipBonus = currentShip.getRun();
}
shotBullet.setMovement(0, shipBonus + 5);
addObject(shotBullet);
}
private void checkCollisions() {
for (int i = 0; i < objects.size() - 1; i++) {
for (int j = 0; j < objects.size(); j++) {
if (j != i) {
OnScreenObject current = objects.get(i);
OnScreenObject next = objects.get(j);
if (current.checkCollide(next)) {
objects.remove(i);
if (i < j) {
objects.remove(j - 1);
} else {
objects.remove(j);
}
}
}
}
}
}
private void checkDeadShip() {
if (objects.size() > 0) {
try {
currentShip = (PlayerShip) objects.get(0);
} catch (Exception e) {
System.out.println("GAME OVER!");
}
}
}
private void moveObjects() {
for (int i = 0; i < objects.size(); i++) {
OnScreenObject current = objects.get(i);
current.move();
if (!current.getVisible()) {
objects.remove(i);
}
}
}
}
if I do not have that line commented out, the
System.out.println("painting"); inside of my public void paintComponent(Graphics g) is never run, which is why I assume that paintComponent isn't running.
(I cant post images since I don't have 10 rep, but its just a JFrame with the beige empty panel look when it is not commented out).
EDIT:
In your setGraphic() method, replace the line
graphic = new ImageIcon("setto");
with this:
graphic = new ImageIcon(this.getClass()
.getResource(setto));

trouble running smooth animation in thread only when using key listener

first time using a forum for coding help so sorry if i post this all wrong. i have more than a few classes i don't think screenManger or core holds the problem but i included them just incase. i got most of this code working through a set of tutorials. but a certain point started trying to do more on my own.
i want to play the animation only when i'm moving my sprite.
in my KeyTest class i am using threads to run the animation it used to work (poorly) but now not at all pluss it really gunks up my computer. i think it's because of the thread. im new to threads so i'm not to sure if i should even be using one in this situation or if its dangerous for my computer.
the animation worked smoothly when i had the sprite bouce around the screen forever. the animation loop played with out stopping.
i think the main problem is between the animationThread, Sprite, and keyTest classes, but itcould be more indepth.
if someone could point me in the right direction for making the animation run smoothly when i push down a key and stop runing when i let off it would be greatly apriciated.
i already looked at this Java a moving animation (sprite) obviously we were doing the same tutorial. but i feel my problem is slightly different.
p.s. sorry for the typos.
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
public class KeyTest extends Core implements KeyListener {
public static void main(String[] args){
new KeyTest().run();
}
Sprite player1;
Image hobo;
Image background;
animation hoboRun;
animationThread t1;
//init also calls init form superclass
public void init(){
super.init();
loadImages();
Window w = s.getFullScreenWindow();
w.setFocusTraversalKeysEnabled(false);
w.addKeyListener(this);
}
//load method will go here.
//load all pics need for animation and sprite
public void loadImages(){
background = new ImageIcon("\\\\STUART-PC\\Users\\Stuart\\workspace\\Gaming\\yellow square.jpg").getImage();
Image face1 = new ImageIcon("\\\\STUART-PC\\Users\\Stuart\\workspace\\Gaming\\circle.png").getImage();
Image face2 = new ImageIcon("\\\\STUART-PC\\Users\\Stuart\\workspace\\Gaming\\one eye.png").getImage();
hoboRun = new animation();
hoboRun.addScene(face1, 250);
hoboRun.addScene(face2, 250);
player1 = new Sprite(hoboRun);
t1 = new animationThread();
t1.setAnimation(player1);
}
//key pressed
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stop();
}
if(keyCode == KeyEvent.VK_RIGHT){
player1.setVelocityX(0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_LEFT){
player1.setVelocityX(-0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_DOWN){
player1.setVelocityY(0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_UP){
player1.setVelocityY(-0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);;
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}else{
e.consume();
}
}
//keyReleased
#SuppressWarnings("static-access")
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_LEFT){
player1.setVelocityX(0);
try{
this.t1.setRunning(false);
}catch(Exception ex){}
}
if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN){
player1.setVelocityY(0);
try{
this.t1.setRunning(false);
}catch(Exception ex){}
}else{
e.consume();
}
}
//last method from interface
public void keyTyped(KeyEvent e){
e.consume();
}
//draw
public void draw(Graphics2D g){
Window w = s.getFullScreenWindow();
g.setColor(w.getBackground());
g.fillRect(0, 0, s.getWidth(), s.getHieght());
g.setColor(w.getForeground());
g.drawImage(player1.getImage(), Math.round(player1.getX()), Math.round(player1.getY()), null);
}
public void update(long timePassed){
player1.update(timePassed);
}
}
abstract class Core {
private static DisplayMode modes[] = {
new DisplayMode(1600, 900, 64, 0),
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(800, 480, 32, 0),
new DisplayMode(800, 480, 24, 0),
new DisplayMode(800, 480, 16, 0),};
private boolean running;
protected ScreenManager s;
//stop method
public void stop() {
running = false;
}
public void run() {
try {
init();
gameLoop();
} finally {
s.restoreScreen();
}
}
//set to full screen
//set current background here
public void init() {
s = new ScreenManager();
DisplayMode dm = s.findFirstCompatibleMode(modes);
s.setFullScreen(dm);
Window w = s.getFullScreenWindow();
w.setFont(new Font("Arial", Font.PLAIN, 20));
w.setBackground(Color.GREEN);
w.setForeground(Color.WHITE);
running = true;
}
//main gameLoop
public void gameLoop() {
long startTime = System.currentTimeMillis();
long cumTime = startTime;
while (running) {
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
Graphics2D g = s.getGraphics();
draw(g);
g.dispose();
s.update();
try {
Thread.sleep(20);
} catch (Exception ex) {
}
}
}
//update animation
public void update(long timePassed) {
}
//draws to screen
abstract void draw(Graphics2D g);
}
public class animationThread implements Runnable{
String name;
volatile boolean playing;
Sprite a;
//constructor takes input from keyboard
public animationThread(){
}
//The run method for animation
public void run() {
long startTime = System.currentTimeMillis();
long cumTime = startTime;
while(getRunning()){
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
a.startAnimation(timePassed);
}
}
public String getName(){
return name;
}
public void setAnimation(Sprite a){
this.a=a;
}
public void setName(String name){
this.name=name;
}
public synchronized void setRunning(boolean running){
this.playing = running;
}
public synchronized boolean getRunning(){
return playing;
}
}
class animation {
private ArrayList scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
//constructor
public animation() {
scenes = new ArrayList();
totalTime = 0;
start();
}
//add scene to ArrayLisy and set time for each scene
public synchronized void addScene(Image i, long t) {
totalTime += t;
scenes.add(new OneScene(i, totalTime));
}
public synchronized void start() {
movieTime = 0;
sceneIndex = 0;
}
//change scenes
public synchronized void update(long timePassed) {
if (scenes.size() > 1) {
movieTime += timePassed;
if (movieTime >= totalTime) {
movieTime = 0;
sceneIndex = 0;
}
while (movieTime > getScene(sceneIndex).endTime) {
sceneIndex++;
}
}
}
//get animations current scene(aka image)
public synchronized Image getImage() {
if (scenes.size() == 0) {
return null;
} else {
return getScene(sceneIndex).pic;
}
}
//get scene
private OneScene getScene(int x) {
return (OneScene) scenes.get(x);
}
//Private Inner CLASS//////////////
private class OneScene {
Image pic;
long endTime;
public OneScene(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
}
class Sprite {
private animation a;
private float x;
private float y;
private float vx;
private float vy;
//Constructor
public Sprite(animation a) {
this.a = a;
}
//change position
public void update(long timePassed) {
x += vx * timePassed;
y += vy * timePassed;
}
public void startAnimation(long timePassed) {
a.update(timePassed);
}
//get x position
public float getX() {
return x;
}
//get y position
public float getY() {
return y;
}
//set x
public void setX(float x) {
this.x = x;
}
//set y
public void setY(float y) {
this.y = y;
}
//get sprite width
public int getWidth() {
return a.getImage().getWidth(null);
}
//get sprite height
public int getHeight() {
return a.getImage().getHeight(null);
}
//get horizontal velocity
public float getVelocityX() {
return vx;
}
//get vertical velocity
public float getVelocityY() {
return vx;
}
//set horizontal velocity
public void setVelocityX(float vx) {
this.vx = vx;
}
//set vertical velocity
public void setVelocityY(float vy) {
this.vy = vy;
}
//get sprite / image
public Image getImage() {
return a.getImage();
}
}
class ScreenManager {
private GraphicsDevice vc;
public ScreenManager() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
//get all compatible DM
public DisplayMode[] getCompatibleDisplayModes() {
return vc.getDisplayModes();
}
//compares DM passed into vc DM and see if they match
public DisplayMode findFirstCompatibleMode(DisplayMode modes[]) {
DisplayMode goodModes[] = vc.getDisplayModes();
for (int x = 0; x < modes.length; x++) {
for (int y = 0; y < goodModes.length; y++) {
if (displayModesMatch(modes[x], goodModes[y])) {
return modes[x];
}
}
}
return null;
}
//get current DM
public DisplayMode getCurrentDisplayMode() {
return vc.getDisplayMode();
}
//checks if two modes match each other
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
if (m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
return false;
}
if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
return false;
}
if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
return false;
}
return true;
}
//make frame full screen
public void setFullScreen(DisplayMode dm) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
if (dm != null && vc.isDisplayChangeSupported()) {
try {
vc.setDisplayMode(dm);
} catch (Exception ex) {
}
}
f.createBufferStrategy(2);
}
//sets graphics object = this return
public Graphics2D getGraphics() {
Window w = vc.getFullScreenWindow();
if (w != null) {
BufferStrategy s = w.getBufferStrategy();
return (Graphics2D) s.getDrawGraphics();
} else {
return null;
}
}
//updates display
public void update() {
Window w = vc.getFullScreenWindow();
if (w != null) {
BufferStrategy s = w.getBufferStrategy();
if (!s.contentsLost()) {
s.show();
}
}
}
//returns full screen window
public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}
//get width of window
public int getWidth() {
Window w = vc.getFullScreenWindow();
if (w != null) {
return w.getWidth();
} else {
return 0;
}
}
//get height of window
public int getHieght() {
Window w = vc.getFullScreenWindow();
if (w != null) {
return w.getHeight();
} else {
return 0;
}
}
//get out of full screen
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
//create image compatible with monitor
public BufferedImage createCopatibleImage(int w, int h, int t) {
Window win = vc.getFullScreenWindow();
if (win != null) {
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, t);
}
return null;
}
}
In your animationThread class you are accessing the field "playing". This should be done using synchronized blocks. As one option put the "synchronized" keyword before "playing". As one option put the "volatile" keyword before "playing".
Without "synchronized" one thread may not see a change of "playing" done in another thread. So the threads may run forever. (short answer only, because I am on one of those fancy mobile devices now)
EDIT:
With the "volatile" keyword the "playing" field, the field is accessed each time its value is read or written. Without it, a Thread may use a copy of it and doesn't see when the original field has changed.
You can make blocks "synchronized":
boolean setRunning(boolean running) {
synchronized (this) {
this.playing = running;
}
}
You can make methods "synchronized":
synchronized boolean setRunning(boolean running) {
this.playing = running;
}
"synchronized" ensures, that no two threads execute this code at the same time and it makes sure that a thread sees an "up to date" object. For instance if one thread changes the "playing" field and when another thread enters a synchronized block, then that other thread will see the changed value of the field.
i just figured out something new that may be important to note: when holding down the the right key to move the sprite and update the animation the code is being re executed i found out by doing this
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stop();
}
if(keyCode == KeyEvent.VK_RIGHT){
player1.setVelocityX(0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
player1.setVelocityX(0);
}
at first the spite doesn't move because the velocity is set to zero at the end but if i hold the butten long enough the sprite jumps across the screen.
this has to be the problem but idk how to fix it
hey thanks for all the help i just solved it though.
public void keyPressed(KeyEvent e){
if(godDamn){
godDamn=false;
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stop();
}
if(keyCode == KeyEvent.VK_RIGHT){
player1.setVelocityX(0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_LEFT){
player1.setVelocityX(-0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_DOWN){
player1.setVelocityY(0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}
if(keyCode == KeyEvent.VK_UP){
player1.setVelocityY(-0.3f);
try{
t1.setRunning(true);
Thread th1 = new Thread(t1);;
th1.start();
}catch(Exception ex){System.out.println("noooo");}
}else{
e.consume();
}
}
}
//keyReleased
#SuppressWarnings("static-access")
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_LEFT){
player1.setVelocityX(0);
try{
this.t1.setRunning(false);
}catch(Exception ex){}
godDamn=true;
}
if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN){
player1.setVelocityY(0);
try{
this.t1.setRunning(false);
}catch(Exception ex){}
}else{
e.consume();
}
}
the new boolean stops the multiple key events from executing. i have to fine tune it but other wise i'm good.

Categories