1. Senior Member
Join Date
Mar 2013
Location
Greece
Posts
183
Rep Power
9

## Do a lot task in the same time..

hello all.. i am working on mini game project.. i watch a lot of tutorials on the web and so far i have this game codes :

Java Code:
```import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Ball {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private MyGame game;

public Ball(MyGame game) {
this.game= game;
}

void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > game.getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (collision1()){
ya = -1;
y = 330 - 30;
}
if (collision2()){
ya = +1;
y = 5+15 ;
}
x = x + xa;
y = y + ya;
}

private boolean collision1() {
return game.racquet.getBounds2D().intersects(getBounds());
}

private boolean collision2() {
return game.racquet2.getBounds2D().intersects(getBounds());
}

public Rectangle getBounds() {
return new Rectangle(x, y, 30, 30);
}

public void paint(Graphics2D g) {
g.setColor(Color.BLUE);
g.fillOval(x, y, 30, 30);

}

}```
Java Code:
```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 MyGame extends JPanel {

Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);
Racquet2 racquet2 = new Racquet2(this);

public MyGame() {
@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
racquet2.keyReleased(e);
racquet.keyReleased(e);
}

@Override
public void keyPressed(KeyEvent e) {
racquet2.keyPressed(e);
racquet.keyPressed(e);
}
});
setFocusable(true);
}

private void move() {
ball.move();
racquet.move();
racquet2.move();
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
racquet.paint(g2d);
racquet2.paint(g2d);
}

public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
MyGame game = new MyGame();
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

while (true) {
game.move();
game.repaint();
}
}
}```
Java Code:
```import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Racquet {
int x ;
int xa = 0;
int y ;
int rectWidth = 60;
int rectHeight = 10;
private MyGame game;

public Racquet(MyGame game) {
this.game= game;
y = 330;
x = 0;
}

public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-60)
x = x + xa;
}

public void paint(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, rectWidth, rectHeight);
}

public void keyReleased(KeyEvent e) {
xa = 0;
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -2;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 2;
}

public Rectangle getBounds2D() {
return new Rectangle(x, y, rectWidth, rectHeight);
}
}```
Java Code:
```import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Racquet2 {
int x ;
int xa = 0;
int y ;
int rectWidth = 60;
int rectHeight = 10;
private MyGame game;

public Racquet2(MyGame game) {
this.game= game;
y = 10;
x = 0;
}

public void move() {
if (x + xa > 0 && x + xa < game.getWidth()-60)
x = x + xa;
}

public void paint(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, rectWidth, rectHeight);
}

public void keyReleased(KeyEvent e) {
xa = 0;
}

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_A)
xa = -2;
if (e.getKeyCode() == KeyEvent.VK_D)
xa = 2;
}

public Rectangle getBounds2D() {
return new Rectangle(x, y, rectWidth, rectHeight);
}
}```
now.. if you try to play the game you will see that two raquets objects has connection with keylistener..
but only one can move at a time.. for example when player one Press right key to move right the second player can't press the A key to move left.. because my java program do only one thing at a time.. i have a book in java from my university.. i check it all but i find out only one thing about
Threads and the Runnable implementation.. but i don't really understand what's going on.. can someone help me to solve this problem? thx in advance ;)
Last edited by ShadowWalker; 07-12-2013 at 01:42 PM.

2. ## Re: Do a lot task in the same time..

I tested the code, and it kinda works for me. The only problem is that both racquets stop moving as soon as you release any key... With that bit of info, can you figure out what the problem is?

3. Senior Member
Join Date
Mar 2013
Location
Greece
Posts
183
Rep Power
9

## Re: Do a lot task in the same time..

yeah .. the racquets move right when you check one racquet at a time.. but when you press right key and then you press A and then D but you have pressed the right key then the program stop the down racquet movement.. i don't know how to explaine it.. to find the mistake leave onother person to play the second racquet and you will see the problem..
it's prety hard to find out the problem by your own.. but when you call a friend to join the game with you will see the bugs.. ;)
Last edited by ShadowWalker; 07-12-2013 at 03:39 PM.

4. ## Re: Do a lot task in the same time..

I'd consider myself pretty good at bug-hunting and testing code... and I downloaded and compiled the exact code above. The result was that I could move both racquets at the same time; the only problem was that when I released any key, both would stop. I even know where the bug in the code is regarding that (and fixed said bug), but I figured you'd learn more if you were guided to it and you found it yourself.

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Do a lot task in the same time..

Its funny how life often shows a certain symmetry. On that note, I would expect keyPressed and keyReleased to also "be more the same".

6. Senior Member
Join Date
Mar 2013
Location
Greece
Posts
183
Rep Power
9

## Re: Do a lot task in the same time..

hmm interesting.. give me some time to think of it more ;) !!!

7. Senior Member
Join Date
Mar 2013
Location
Greece
Posts
183
Rep Power
9

## Re: Do a lot task in the same time..

Thx A lot.. i belive that the bug fixed..

Java Code:
```public void keyReleased(KeyEvent e) {
if(e.getKeyChar() == 'A' || e.getKeyChar() == 'D'){
racquet2.keyReleased(e);
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_LEFT){
racquet.keyReleased(e);
}
}```
in main class MyGame instead of previous keyReleased method.. thx a lot guys!! and i have to do a lot of improvements..

8. ## Re: Do a lot task in the same time..

Hmm. While that code will work, I think it'd be better, structurally, to put that code in the individual Racquet classes. Other than that though, great work finding the problem!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•