I need help with java programming

Status
Not open for further replies.

semthan

Distinguished
Jul 30, 2010
10
0
18,510
hi guys i need little help with my "breakout" program


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

oge9s6.png



this is my code!!




public class Brick {
int xPos,yPos;

}




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);
}

}


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();

}
}



 

Rusting In Peace

Distinguished
Jul 2, 2009
1,048
0
19,460
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.
 

semthan

Distinguished
Jul 30, 2010
10
0
18,510
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!
 

Rusting In Peace

Distinguished
Jul 2, 2009
1,048
0
19,460
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
 
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.
 

semthan

Distinguished
Jul 30, 2010
10
0
18,510
i don't know guys but its still not working? :S





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();

}
}
 
Status
Not open for further replies.