Results 1 to 3 of 3
- 02-02-2013, 04:26 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
An annoying problem with graphics in java
Hi everyone!
I am new to java and programming in general,but so far(after 3 months of learning java) i started to work on a little shoot em up game,with spaceship and aliens.
and i hava a very annoying problem that i was not able to solve no matter how i tried.
I do all the painting with a Graphics class of java and i use a jFrame to put all the graphics component on it. my spaceship image is only moving right and left(using keylistener) and also have the ability to shoot. most of the time,the ship is moving just fine,but every once in a while, it sudddenly moving very slowly,and also the shoots images are moving very slowly on screen.
I tried everything,and i am very desperate and dissapointed of this annoying problem.
any ideas on what can it be??
i can post the code(or part of it) if you want,but i have the feeling that the experts here will probably guess what could be the problem.
by the way,it's not only on my laptop but also on my quad core pc unit so it is probably not a hardware related problem.
but what can it be than...?
- 02-02-2013, 05:22 PM #2
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Re: An annoying problem with graphics in java
Java Code:package battleShip; import javax.swing.JFrame; public class MainWindow { private JFrame frame; public MainWindow(){ frame = new JFrame(); frame.setSize(1100,700); frame.setLocation(150, 10); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Ship()); frame.setVisible(true); } } package battleShip; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.Timer; public class Ship extends JPanel implements ActionListener,KeyListener { private int xPosition; private ArrayList<Shot> shotsSupply = new ArrayList<Shot>(); private int shotsSupplyIndex; private boolean leftKeyPressed; private boolean rightKeyPressed; private boolean gotHit; private enum Movement {MOVE_RIGHT,MOVE_LEFT,STAY_PUT} private Movement currentMovement; Timer timer = new Timer(1,this); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image myShip = toolkit.getImage("c:\\Users\\assaf\\workspace\\My First Game\\images\\ship.png"); public Ship(){ this.xPosition = 500; this.shotsSupplyIndex = 0; fillShotsSupply(100); currentMovement = Movement.STAY_PUT; timer.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void fillShotsSupply(int shotsAmount){ for(int i = 0; i<shotsAmount; i++){ shotsSupply.add(new Shot()); } } public int getShipXposition(){ return this.xPosition; } public void move(Movement movement){ switch(movement){ case MOVE_RIGHT: this.xPosition++; break; case MOVE_LEFT: this.xPosition--; break; case STAY_PUT: this.xPosition = getShipXposition(); break; } repaint(); } public void paint(Graphics g){ super.paint(g); this.setBackground(Color.BLACK); //drawing the ship g.drawImage(myShip,this.xPosition,610,null); //drawing the shots for(Shot shot:shotsSupply){ if(shot.getShootable()){ shot.paint(g); } } //drawing the aliens for(Alien alien:AliensStorage.alienList1){ if(alien.getIsPainted()){ alien.paint(g); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(getShipXposition()<0){ this.xPosition = 0; } if(getShipXposition()>1050){ this.xPosition = 1050; } if(leftKeyPressed == false && rightKeyPressed == false){ currentMovement = Movement.STAY_PUT; } move(currentMovement); } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub int c = e.getKeyCode(); if(c==KeyEvent.VK_LEFT){ currentMovement = Movement.MOVE_LEFT; leftKeyPressed = true; } if(c==KeyEvent.VK_RIGHT){ currentMovement = Movement.MOVE_RIGHT; rightKeyPressed = true; } if(c==KeyEvent.VK_CONTROL){ if(shotsSupplyIndex<shotsSupply.size()){ shotsSupply.get(shotsSupplyIndex).setXposition(getShipXposition()); shotsSupply.get(shotsSupplyIndex).setShootable(true); shotsSupply.get(shotsSupplyIndex).timer.start(); shotsSupplyIndex++; } } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub int c = e.getKeyCode(); if(c==KeyEvent.VK_LEFT){ leftKeyPressed = false; } if(c==KeyEvent.VK_RIGHT){ rightKeyPressed = false; } } } package battleShip; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.Timer; public class Shot extends JPanel implements ActionListener{ private int yPosition; private int xPosition; private boolean shootable; private boolean scanning; private boolean hitAlien; public static ArrayList<Alien> currentAlienList; Timer timer = new Timer(1,this); public Shot(){ this.yPosition = 610; } public void setXposition(int position){ this.xPosition = position; } public int getShotXposition(){ return this.xPosition; } public boolean getShootable(){ return this.shootable; } public void setShootable(boolean shootable){ this.shootable = shootable; } public void move(){ this.yPosition--; } public int getShotYposition(){ return this.yPosition; } public void paint(Graphics g){ g.setColor(Color.ORANGE); g.drawLine(xPosition+23,yPosition,xPosition+23,yPosition-15); } public void checkIfHitAlien(ArrayList<Alien> list){ for(Alien alien:list){ if(getShotYposition()==alien.getYposition()+20&&getShotXposition()>=alien.getXposition()-20&&getShotXposition()<=alien.getXposition()+8){ alien.raiseHitsCounter(); setShootable(false); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub move(); checkIfHitAlien(currentAlienList); } } package battleShip; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.Timer; public class Alien extends JPanel implements ActionListener{ private int xPosition; private int yPosition; private short hitsCounter; private boolean isPainted; Toolkit toolkit = Toolkit.getDefaultToolkit(); Image myAlien = toolkit.getImage("c:\\Users\\assaf\\workspace\\My First Game\\images\\alien.png"); Image alienHit = toolkit.getImage("c:\\Users\\assaf\\workspace\\My First Game\\images\\alien2.gif"); Timer timer; long startTime; private int x; private int y; private boolean approval; private boolean readyForAction;//this variable will be used to check if aliens finished the starting scene and ready for free movement. AlienMovement a1 = new AlienMovement(); private enum State {MOVING_LEFT,MOVING_RIGHT,MOVING_DOWN,MOVING_DOWN_RIGHT,MOVING_DOWN_LEFT,STAY_IN_PLACE} private State currentState; public Alien(){ this.hitsCounter = 0; this.x = 0; this.y = 0; timer = new Timer(13,this); timer.setInitialDelay(4000); timer.start(); } public void setReadyForAction(boolean ready){ this.readyForAction=ready; } public boolean checkReadyForAction(){ return this.readyForAction; } public boolean getIsPainted(){ return this.isPainted; } public void setIsPainted(boolean painted){ this.isPainted=painted; } public void raiseHitsCounter(){ this.hitsCounter++; } public short getHitsCounter(){ return this.hitsCounter; } public int getXposition(){ return this.xPosition; } public void setXposition(int position){ this.xPosition = position; } public int getYposition(){ return this.yPosition; } public void setYposition(int position){ this.yPosition = position; } public void moveLeft(){ this.xPosition--; currentState = State.MOVING_LEFT; } public void moveRight(){ this.xPosition++; currentState = State.MOVING_RIGHT; } public void moveDown(){ this.yPosition++; currentState = State.MOVING_DOWN; } public void stayInPlace(){ this.xPosition = getXposition(); this.yPosition = getYposition(); currentState = State.STAY_IN_PLACE; } public void moveDownAndLeft(){ this.xPosition--; this.yPosition++; currentState = State.MOVING_DOWN_LEFT; } public void moveDownAndRight(){ this.xPosition++; this.yPosition++; currentState = State.MOVING_DOWN_RIGHT; } public void checkAlienState(){ //this method will run only single time when called (look in the paint method of this class) //and will change the startTime variable,so that the burning alien gif will be painted for 2 seconds only. if(this.x==this.y){ this.startTime = System.currentTimeMillis(); this.x++; this.approval = true; } } public void startingSceneAlienList1(){ for(Alien alien:AliensStorage.alienList1){ if(alien.getYposition()<100){ alien.moveDown(); } } if(AliensStorage.alienList1.get(0).getYposition()>=100&&AliensStorage.alienList1.get(0).getXposition()>=150){ AliensStorage.alienList1.get(0).moveLeft(); } else{ AliensStorage.alienList1.get(0).stayInPlace(); AliensStorage.alienList1.get(0).setReadyForAction(true); } if(AliensStorage.alienList1.get(1).getYposition()>=100&&AliensStorage.alienList1.get(1).getXposition()>=250){ AliensStorage.alienList1.get(1).moveLeft(); } else{ AliensStorage.alienList1.get(1).stayInPlace(); AliensStorage.alienList1.get(1).setReadyForAction(true); } if(AliensStorage.alienList1.get(2).getYposition()>=100&&AliensStorage.alienList1.get(2).getXposition()>=350){ AliensStorage.alienList1.get(2).moveLeft(); } else{ AliensStorage.alienList1.get(2).stayInPlace(); AliensStorage.alienList1.get(2).setReadyForAction(true); } if(AliensStorage.alienList1.get(3).getYposition()>=100&&AliensStorage.alienList1.get(3).getXposition()<=650){ AliensStorage.alienList1.get(3).moveRight(); } else{ AliensStorage.alienList1.get(3).stayInPlace(); AliensStorage.alienList1.get(3).setReadyForAction(true); } if(AliensStorage.alienList1.get(4).getYposition()>=100&&AliensStorage.alienList1.get(4).getXposition()<=750){ AliensStorage.alienList1.get(4).moveRight(); } else{ AliensStorage.alienList1.get(4).stayInPlace(); AliensStorage.alienList1.get(4).setReadyForAction(true); } if(AliensStorage.alienList1.get(5).getYposition()>=100&&AliensStorage.alienList1.get(5).getXposition()<=850){ AliensStorage.alienList1.get(5).moveRight(); } else{ AliensStorage.alienList1.get(5).stayInPlace(); AliensStorage.alienList1.get(5).setReadyForAction(true); } } public void paint(Graphics g){ if(getHitsCounter()<5){ g.drawImage(myAlien, getXposition(), getYposition(),null); } if(getHitsCounter()>=5){ g.drawImage(alienHit, getXposition(), getYposition(),null); checkAlienState(); } if(approval){ if(System.currentTimeMillis()>startTime+2000){ setIsPainted(false); } } } public void move(byte randomChoice){ switch(randomChoice){ case 0: moveLeft(); break; case 1: moveRight(); break; case 2: moveDown(); break; case 3: moveDownAndLeft(); break; case 4: moveDownAndRight(); break; } } @Override public void actionPerformed(ActionEvent e) { startingSceneAlienList1(); } class AlienMovement extends Thread{ @Override public void run() { } } }
- 02-02-2013, 05:55 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Annoying Generating Cookies files problem
By Burglar_Cat in forum Java ServletReplies: 3Last Post: 05-11-2010, 01:04 PM -
Annoying problem class/interface error
By Chick786 in forum New To JavaReplies: 2Last Post: 04-11-2010, 04:36 PM -
plz help me solving this small but annoying problem!! plz.. i need help urgently.
By Y. Progammer in forum New To JavaReplies: 13Last Post: 02-20-2010, 07:56 PM -
Insanely annoying java error...
By Nukleahboy in forum New To JavaReplies: 10Last Post: 08-07-2009, 09:22 AM -
Annoying database problem!
By DC1 in forum New To JavaReplies: 2Last Post: 05-29-2008, 09:43 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks