Sign in with
Sign up | Sign in
Your question

I need help with java programming

Last response: in Apps General Discussion
Share
May 13, 2012 2:27:00 PM

hi guys i need little help with my "breakout" program


i need a code to move the paddle "easy code" with arrow keys




this is my code!!




Quote:

public class Brick {
int xPos,yPos;

}





Quote:
import java.awt.*;
import java.awt.event.*;

public class test extends Canvas {




Graphics buffer;
Image bufferI;

Brick[] brickor = new Brick[12];


public test () {
setPreferredSize(new Dimension(480,480));

for (int n=0; n<2; n++) {
for (int m = 0; m<6; m++) {
brickor[n*6+m] = new Brick();
brickor[n*6+m].xPos = m*80;
brickor[n*6+m].yPos = n*12;
}
}

}

public void update(Graphics g) {
paint(g);

}

public void paint(Graphics yta) {
if (buffer == null) {
bufferI = createImage(480,480);
buffer = bufferI.getGraphics();
}
buffer.setColor(Color.BLACK);
buffer.fillRect(0,0,480,480);

// Ritar ut alla block

for (int n = 0; n<12; n++) {
buffer.setColor(Color.RED);
buffer.fillRect(brickor[n].xPos,brickor[n].yPos,78,10);
}

// Hنr skriv koden fِr att rita ut "pinnen".


buffer.setColor(Color.BLUE);
buffer.fillRect(x,y,80,10);

yta.drawImage(bufferI,0,0,null);
}

}



Quote:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class breakout extends JFrame implements KeyListener
{

int velX = 0, velY = 0;

int x = 200;
int y = 350;


public breakout()
{
setTitle("BreakOut");
test kludd = new test();
add(kludd);
pack();
setVisible(true);

kludd.addKeyListener(this);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);

}

public void keyPressed(KeyEvent e) {
// System.out.println(e);

System.out.println(e.getKeyCode());
if (e.getKeyCode() == 37){



System.out.println("Du tryckte pه hِgerpil!!!");

}


}

public void keyReleased(KeyEvent e) {

System.out.println("Key Released!!!");


}





public void keyTyped(KeyEvent e) {

}




public static void main(String[] arg)
{
new breakout();

}
}




More about : java programming

a b L Programming
May 15, 2012 12:47:02 PM

I don't think you are going to get an answer out of anyone unless you ask a specific question.

It looks like you've got the basics of the key listener in there. In the key pressed method you are looking for specific arrow key codes so all that's left to do is redraw the paddle.
m
0
l
May 15, 2012 2:38:17 PM

yeah, but don't know how to redraw the paddle., that's why iam posted this .
someone told me that I must have "Timer" or something like that to keep moving the paddle
and i don't know what a timer is!!

can you help me little more

thank you man!
m
0
l
Related resources
a b L Programming
May 15, 2012 3:21:33 PM

The Timer class is just something that can trigger a piece of code to run after a certain period of time. I think you'll need it for moving the ball in your game but not the paddle.

So in the test class (Class names should start with an uppercase FYI), you are drawing the blue paddle within the paint method. You've set the position of the paddle to be x and y. Notice how you are drawing one Image on the canvas but have 3 components - background, blocks and paddle. You might want to change this as you are going to have to update things and it will be expensive to redraw everything rather than just what you need.

I can't remember the exact API calls to do what you need to do but fundamentally on an arrow key press:
- Figure out if the paddle actually needs to move i.e. if it's already as far left as it can move you don't need to redraw anything
- set variables x and y on the breakout object (i'm assuming that's where the values are coming from for your paddle - your code is not clear)
- Ask the canvas to update
m
0
l
a b L Programming
May 15, 2012 3:32:09 PM

I think that you are going to need a timer to control paddle movement also. Otherwise the speed of movement is going to depend upon variable factors such as processor speed or key repeat rate. You need to control this timing by only checking the keypress and moving the paddle at well-defined intervals. This has the additional advantage that you can then have variable speeds under program control.
m
0
l
May 15, 2012 8:22:31 PM

i don't know guys but its still not working? :S





Quote:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class breakout extends JFrame implements KeyListener,ActionListener
{

int Xnew = 0;
int Ynew = 0;

int x = 200, y = 350;

Timer time = new Timer(5,this);


public breakout()
{

time.start();
setTitle("BreakOut");
test kludd = new test();
add(kludd);
pack();
setVisible(true);
setFocusable(true);
setFocusTraversalKeysEnabled(false);


kludd.addKeyListener(this);
addKeyListener(this);



}


public void actionPerformed(ActionEvent e){

if(x<0)
{
Xnew=0;
x=0;
}

if(x>480)
{
Xnew=0;
x=400;

}

if(y<0)
{
Ynew=0;
y=0;
}
if(y>480)
{
Ynew=0;
y=470;
}

x = x + Xnew ;
y = y + Ynew;

repaint();
}

public void keyPressed(KeyEvent e) {
// System.out.println(e);

System.out.println(e.getKeyCode());
if (e.getKeyCode() == 37){

Xnew = 1;
Ynew = 0;




System.out.println("Du tryckte på högerpil!!!");

}


}

public void keyReleased(KeyEvent e) {

System.out.println("Key Released!!!");

Xnew = 0;
Ynew = 0;


}





public void keyTyped(KeyEvent e) {

}




public static void main(String[] arg)
{
new breakout();

}
}
m
0
l
a b L Programming
May 16, 2012 2:59:30 PM

At a glance and without too much thought, I'm not sure keyReleased should be resetting the xnew and ynew variables as there is potential for a key to be pressed and released before actionPerformed is finished?
m
0
l
May 29, 2012 8:10:27 PM

i fixed :D 

kludd.x = kludd.x-10;
kludd.x = kludd.x+10;
m
0
l
May 30, 2012 3:24:35 PM

yeah but it took two weeks until I solved it XD
m
0
l
!