stefan axiaq

Greenhorn
+ Follow
since Jan 18, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by stefan axiaq

Many thanks mate...that line ok code tooke me 2 days nahh...THanks mate and sorry for the bulky code I left there lol...



Michael Dunn wrote:default value for JPanel is "not focusable", so add the indicated line

next, uncomment repaint, at the end of the switch.

should work now, but you would be far better off (in the long run) to use keybindings instead of keylisteners

13 years ago
Hi,
Basically this is a part of my assignment. A space ship simulator. All was working then I had to implement arrow keys listeners. The problem is that with the debug the code is not being executed and helt at the addKeyListener(new KeyAdapter(). Here Below please fine part of my code that help you understand such problem.

Begining with the MAIN METHOD:



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

public class Simulator
{
public static void main(String[] args)
{
JFrame frmSimulator = new JFrame("Simulator Version stefan");
frmSimulator.setSize(1030, 850);
Container content = frmSimulator.getContentPane();
content.setLayout(new GridLayout(3,0)); // 3 rows and no coloumns

PnlOutsideView pnlOutsideView = new PnlOutsideView();//2
//PnlControls pnlControls = new PnlControls(pnlOutsideView); //3 passing links of outside panel
//PnlControlsUp pnlControlsUp = new PnlControlsUp(pnlOutsideView);//1

//content.add(pnlControlsUp);
content.add(pnlOutsideView);
//content.add(pnlControls);

//MovingThread thread = new MovingThread(pnlControls); //creation of thread
//thread.start();


frmSimulator.setVisible(true);
}
}

//SO FAR SO GOOD HERE.

THE PROBLEM BEGINS BELOW
:

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

public class PnlOutsideView
extends JPanel
{
// Constants --------------------------------------------------------------------

public static final String PLANET_1 = "Planet_overview.png";
//overview

// Members ----------------------------------------------------------------------

Image bgImage; // Image
int x, y,w, h ; // Position of image

// Constructors -----------------------------------------------------------------

public PnlOutsideView()
{
setBackground(Color.white);
loadImage();


//loadImage();
//KeyboardPanel();
}

// Methods ----------------------------------------------------------------------

/**
* Loads the image and saves it in the member bgImage
*/
private void loadImage()
{
bgImage = Toolkit.getDefaultToolkit().getImage(PLANET_1);
}

public int getImageX()
{
return x;
}

public int getImageY()
{
return y;
}

public void setImageX(int x)
{
this.x = x;
}

public void setImageY(int y)
{
this.y = y;
}


//////////////////////KEYLISTENERS BELOW
public void KeyboardPanel(){ //was without keylistene

addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
switch (ke.getKeyCode()){
case KeyEvent.VK_DOWN:
{
setImageY(getImageY()-40);
System.out.println("Value of X " + getImageX());
System.out.println("Value of Y " + getImageY());
}; break;

case KeyEvent.VK_UP:
{
setImageY(getImageY()+40);
System.out.println("Value of X " + getImageX());
System.out.println("Value of Y " + getImageY());
}; break;

case KeyEvent.VK_LEFT:
{
setImageX(getImageX()-40);
System.out.println("Value of X " + getImageX());
System.out.println("Value of Y " + getImageY());
}; break;

case KeyEvent.VK_RIGHT:
{
setImageX(getImageX()+40);
System.out.println("Value of X " + getImageX());
System.out.println("Value of Y " + getImageY());
}; break;

}//switch
//repaint();
}
});//keylistener
} //public keyb

//////////////////////KEYLISTENERS ABOVE

public void paintComponent(Graphics g)
{
super.paintComponent(g);
//(bgImage, getImageX(), getImageY(), null);
KeyboardPanel();
System.out.println("Value of X " + getImageX());
System.out.println("Value of Y " + getImageY());

g.drawImage(bgImage, getImageX(), getImageY(), h+100, w+100, null);
//the NULL can do another class and attach to image
//eg when changing size. image observor.
}
}

So basically it is not giving me the chance to input keys. BDW the System.out..... are used for testing purposes only.

If any one can help I will appreciate alot as time is passing and I need to hand it in.

Thanks
13 years ago