Nullpointer error from my classes - java

I was given this code by my professor and it should run as is.
I compile it and get the following error.
java.lang.NullPointerException
at WholePanel.<init>(WholePanel.java:59)
at Assignment7.init(Assignment7.java:19)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:744)
I have no idea why this is happening. Here are my classes.
I compiled and ran the code, got the error, tried commenting some things out and still nothing. I have programmed some previous applets and they run fine.
Assignment7
import javax.swing.*;
public class Assignment7 extends JApplet
{
public void init()
{
// create a WholePanel object and add it to the applet
WholePanel wholePanel = new WholePanel();
getContentPane().add(wholePanel);
//set applet size to 400 X 400
setSize (400, 400);
}
}
Whole Panel
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class WholePanel extends JPanel
{
private Color currentColor;
private CanvasPanel canvas;
private JPanel primary, buttonPanel, leftPanel;
private JButton erase, undo;
private ArrayList rectList, tempList;
private JRadioButton[] colorRButtons;
private Color[] colors;
private int x1, y1, x2, y2, x3, y3;
private boolean mouseDragged = false;
//Constructor to instantiate components
public WholePanel()
{
//default color to draw rectangles is black
currentColor = Color.black;
rectList = new ArrayList();
//create buttons
//create radio buttons for 5 colors
//black will be chosen by default
colorRButtons = new JRadioButton[5];
colorRButtons[0] = new JRadioButton("black", true);
//store 5 colors in an array
//group radio buttons so that when one is selected,
//others will be unselected.
ButtonGroup group = new ButtonGroup();
for (int i=0; i<colorRButtons.length; i++)
group.add(colorRButtons[i]);
//add ColorListener to radio buttons
ColorListener listener = new ColorListener();
for (int i=0; i<colorRButtons.length; i++)
colorRButtons[i].addActionListener(listener);
//primary panel contains all radiobuttons
primary = new JPanel(new GridLayout(5,1));
for (int i=0; i<colorRButtons.length; i++)
primary.add(colorRButtons[i]);
//canvas panel is where rectangles will be drawn, thus
//it will be listening to a mouse.
canvas = new CanvasPanel();
canvas.setBackground(Color.white);
canvas.addMouseListener(new PointListener());
canvas.addMouseMotionListener(new PointListener());
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvas);
setLayout(new BorderLayout());
add(sp);
}
//ButtonListener defined actions to take in case "Create",
//"Undo", or "Erase" is chosed.
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
} // end of ButtonListener
// listener class to set the color chosen by a user using
// the radio buttons.
private class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == colorRButtons[0])
currentColor = colors[0];
else if (event.getSource() == colorRButtons[1])
currentColor = colors[1];
else if (event.getSource() == colorRButtons[2])
currentColor = colors[2];
else if (event.getSource() == colorRButtons[3])
currentColor = colors[3];
else if (event.getSource() == colorRButtons[4])
currentColor = colors[4];
}
}
//CanvasPanel is the panel where rectangles will be drawn
private class CanvasPanel extends JPanel
{
//this method draws all rectangles specified by a user
public void paintComponent(Graphics page)
{
super.paintComponent(page);
//draw all rectangles
for (int i=0; i < rectList.size(); i++)
{
// ((Rect) rectList.get(i)).draw(page);
}
//draw an outline of the rectangle that is currently being drawn.
if (mouseDragged == true)
{
page.setColor(currentColor);
//Assume that a user will move a mouse only to left and down from
//the first point that was pushed.
page.drawRect(x1, y1, x3-x1, y3-y1);
}
}
} //end of CanvasPanel class
// listener class that listens to the mouse
public class PointListener implements MouseListener, MouseMotionListener
{
//in case that a user presses using a mouse,
//record the point where it was pressed.
public void mousePressed (MouseEvent event)
{
//after "create" button is pushed.
}
//mouseReleased method takes the point where a mouse is released,
//using the point and the pressed point to create a rectangle,
//add it to the ArrayList "rectList", and call paintComponent method.
public void mouseReleased (MouseEvent event)
{
}
//mouseDragged method takes the point where a mouse is dragged
//and call paintComponent nethod
public void mouseDragged(MouseEvent event)
{
canvas.repaint();
}
public void mouseClicked (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}
} // end of PointListener
} // end of Whole Panel Class

It seems you create only one colorRButtons, but trying to use all five.
So in colorRButtons[0] is not null, but all others are null and using of addActionListener for them is impossible.

In this loop
for (int i=0; i<colorRButtons.length; i++)
colorRButtons[i].addActionListener(listener);
you are accessing an array of colorRButtons, but only the first one
colorRButtons[0] = new JRadioButton("black", true);
has been created.

Related

JPanel not displaying after button pressed in another JPanel

Hi I'm building a game which includes 3 JPanels on a JFrame, a Startscreen, a DrawingPanel and a GameOver screen. If I just create the DrawingPanel and tell the GameController class to begin updating it works fine, but if I create a StartScreen with a button to start the game, then when I press the start game button the game window does not display, although the game code runs.
EDIT:
I have created a new program which mimics the creation of the JPanels, but excludes all of the game code to make it a bit simpler to follow. Below I have included all the relevant classes:
This class creates a JFrame and two JPanels. It also runs the code that updates the game state and tells the DrawingPanel to repaint.
public class TestController{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private final int FRAME_WIDTH = (int)screenSize.getWidth();
private final int FRAME_HEIGHT = (int)screenSize.getHeight();
public boolean gameStarted = false;
private JFrame gameWindow;
private TestDrawingPanel myDrawingPanel;
private TestStartGame startGame;
int counter;
Set<Rectangle> rects;
//creates a JFrame and all the JPanels
public TestController(String title)
{
gameWindow = new JFrame(title);
gameWindow.setSize(FRAME_WIDTH, FRAME_HEIGHT);
gameWindow.setVisible(true);
gameWindow.setResizable(false);
gameWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
startGame = new TestStartGame(getAvailableWidth(), getAvailableHeight());
startGame.addAL(new StartButton());
myDrawingPanel = new TestDrawingPanel(getAvailableWidth(), getAvailableHeight());
gameWindow.add(startGame);
gameWindow.add(myDrawingPanel);
myDrawingPanel.setVisible(false);
myDrawingPanel.setEnabled(false);
rects = new HashSet();
}
private int getAvailableWidth()
{
return gameWindow.getWidth() - gameWindow.getInsets().left - gameWindow.getInsets().right;
}
private int getAvailableHeight()
{
return gameWindow.getHeight() - gameWindow.getInsets().top - gameWindow.getInsets().bottom;
}
//starts the game running
public void startTheGame()
{
myDrawingPanel.setEnabled(true);
startGame.setVisible(false);
startGame.setEnabled(false);
myDrawingPanel.setVisible(true);
gameStarted = true;
update();
}
public boolean getGameStarted()
{
return gameStarted;
}
//loop that runs the game code
public void update()
{
counter = 0;
while(gameStarted)
{
updatePictureState();
myDrawingPanel.draw(rects);
myDrawingPanel.repaint();
}
}
//updates the game state
public void updatePictureState()
{
rects.clear();
for (int i = counter + 10; i < counter + 100; i = i + 10)
{
rects.add(new Rectangle(i,i,10,10));
}
}
//an action listener to be added to the start screen
private class StartButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object buttonPressed = e.getSource();
if(buttonPressed.equals(TestStartGame.start))
{
startTheGame();
}
}
}
}
This class is an extended JPanel with a single button to start the game:
public class TestStartGame extends JPanel{
private final JPanel buttons;
public static JButton start;
//creates a JPanel with a single button to start the game
public TestStartGame(int width, int height)
{
setSize(width, height);
setLayout(new GridLayout(2,1));
setBackground(Color.GREEN);
buttons = new JPanel();
buttons.setSize(width, height / 2);
buttons.setBackground(Color.red);
start = new JButton("Start");
buttons.add(start);
add(buttons, BorderLayout.SOUTH);
}
//adds an action listener to the button
public void addAL(ActionListener al)
{
start.addActionListener(al);
}
}
This class is an extended JPanel and acts as the main screen for the game, being updated to with each cycle of the game to display it's current state:
public class TestDrawingPanel extends JPanel{
Set<Rectangle> drawSet;
//creates the drawing panel and sets the size and background
public TestDrawingPanel(int width, int height)
{
setSize(width, height);
this.setBackground(Color.CYAN);
drawSet = new HashSet();
}
public void draw(Set<Rectangle> platforms)
{
drawSet.clear();
drawSet = platforms;
}
//draws the game window
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println("works here");
g.setColor(Color.red);
for (Rectangle r : drawSet)
{
g.fillRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());
}
}
}
If I just add the TestDrawingPanel, it displays fine, but if I start with a TestStartScreen then when I click the start game button the TestStartScreen does not disappear and the TestDrawingPanel never displays. Interestingly, if I have both screens but do not call the update method is TestController then the start game button works correctly and the TestDrawingPanel displays, although obviously nothing happens as the update method is where the game state is changed.
I have discovered the problem is that if the TestDrawingPanel is not the only JPanel created then the call to repaint it fails.
Here:
Thread.sleep(20);
You are most likely sleeping on the Event Dispatcher Thread. That will freeze your whole application. You have to step back and look into invokeLater to ensure "correct" threading within your UI.
Problem solved courtesy of #Andrew-Thompson:
"while(gameStarted) .. No, no a thousand times no. An infinite loop will likely freeze the GUI. Use a Swing based Timer to call those code statements in the loop, in the actionPerformed method of an ActionListener"

Applet not running java

I'm wondering why my applet isn't running. I'm using netbeans, and the program works fine with no errors, but I can't seem to get the applet screen to appear to test all the functions of my program.I was wondering if someone can point out what I am doing wrong in starting my applet up. I haven't done to many things with applets, so I'm quite curious why it isn't upping and running for me. I really didn't want to post the full code of my program, but I've no idea if there's an error in my code, or I'm just not doing the right thing to actually start the applet or something.
I do not get any errors while running my program, that applet simply does not appear and run in itself. Otherwise I get no errors from neatbeans while running my program.
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
public class Program7 extends JApplet implements ActionListener, ItemListener
{
//static varaibles
private static int GuiHeight = 90;
private static int Square = 50;
//gui varaibles
private JRadioButton drawSquare;
private JRadioButton mess;
private JTextField messageFeild;
private JCheckBox color;
private JButton button;
private JComboBox location;
//modable global varaibles
private int width, height, drawX, drawY;
private Color drawColor;
private String draw;
#Override
public void init()
{
//creating the panels
JPanel drawChoice;
JPanel drawSet;
//setting group onject to link certain buttons together
ButtonGroup ObjGroup;
//setting layout to make things look nice
setLayout(new FlowLayout());
//ibtializing other varaibles
width = 0;
height = 0;
drawX = 0;
drawY= 0;
drawColor = Color.BLACK;
draw = "";
//intialzing the gui interface
drawSquare = new JRadioButton("Draw a Square!");
mess = new JRadioButton("Write a Message!");
location = new JComboBox(new String[]{"", "Random Pick", "Top Left"});
color = new JCheckBox("Draw in Color!");
button = new JButton("Draw it!");
//linking the buttons together
ObjGroup = new ButtonGroup();
ObjGroup.add(drawSquare);
ObjGroup.add(mess);
//adding event handlers to the buttons
location.addItem(this);
button.addActionListener(this);
color.addItemListener(this);
drawSquare.addActionListener(this);
mess.addActionListener(this);
messageFeild = new JTextField(20);
//adding to the first paels
drawChoice = new JPanel();
drawChoice.add(drawSquare);
drawChoice.add(mess);
drawChoice.add(messageFeild);
//adding to the applet
add(drawChoice);
//adding to the second panel
drawSet = new JPanel();
drawSet.add(new JLabel("Select where to draw!"));
drawSet.add(location);
drawSet.add(color);
drawSet.add(button);
//adding to the applet
add(drawSet);
}
#Override
public void paint(Graphics g)
{
//calling super constructor
super.paint(g);
//getting width
width = getWidth();
height = getHeight();
//setting graphics color
g.setColor(drawColor);
if (location.getSelectedItem() != "")
{
//ifstatement checking for inputs
if(draw == "Square"||draw == "square"||draw == "SQUARE")
{
//creating rectange
g.fillRect(drawX, drawY, width, height);
}
if(draw == "Message"|| draw == "MESSAGE"||draw == "message")
{
//writing message
g.drawString(messageFeild.getText(), drawX, drawY);
}
}
}
#Override
public void actionPerformed(ActionEvent e)
{
//repainting
repaint();
}
#Override
public void itemStateChanged(ItemEvent e)
{
//if statement checking for the various changes being made by the user in the pograms interface
//if statement check checking to see if there's a square or to write a message
if (e.getSource() == drawSquare && e.getStateChange() == ItemEvent.SELECTED)
{
draw = "Square";
messageFeild.setEnabled(false);
}
if (e.getSource() == mess && e.getStateChange() == ItemEvent.SELECTED)
{
draw = "Message";
messageFeild.setEnabled(true);
}
//if statement set checking for locational input
if (e.getSource() == location)
{
if(location.getSelectedItem() == "Random Pick")
{
drawX=(int)(Math.random()*(width+1));
drawY=(int)(GuiHeight+ Math.random()*(height-GuiHeight+1));
}
if(location.getSelectedItem() == "Top Left")
{
drawX=0;
drawY=GuiHeight;
}
}
//if statement to check for color change
if(e.getSource()==color && e.getStateChange()==ItemEvent.SELECTED)
{
drawColor = Color.GREEN;
}
if(e.getSource()==color && e.getStateChange()==ItemEvent.DESELECTED)
{
drawColor = Color.BLACK;
}
}
public static void main(String[] args) {
}
}

transfer drawn content from one jcomponent to onther

I have a Java program with two textareas and a button. I want to allow the user to write one one textarea using a touch pen or mouse and when he/she clicks the button, the drawn content should be send to textarea no 2.
So the textarea where the user is writing on, I gave a mousemotion listener with paintComponent method in it.
When I run the application, I have realized that texarea method getText() and setText() can't set or get the drawn content.
Is there way I can achieve the above task? I have also tried JPanel but it doesn't have the setContent() method.
I appreciate any help.
This is my first textarea where user is writing on with touchpen.
public class Area1 extends JTextArea {
int pointcount = 0;
Point[] points = new Point[10000];
public Area1() {
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent event) {
if (pointcount < points.length) {
points[pointcount] = event.getPoint();
++pointcount;
repaint();
}
}
});
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < pointcount; i++) {
g.fillOval(points[i].x, points[i].y, 4, 4);
}
}
}
this is my overall application with textarea2 and button
public class Handwriting extends JFrame {
private JTextArea area2;
private JButton b1;
Area1 area1;
public Handwriting() {
Box box = Box.createHorizontalBox();
area1 = new Area1();
area1.setRows(30);
area1.setColumns(30);
area2 = new JTextArea(30, 30);
b1 = new JButton("Copy");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == b1) {
area2.setText(area1.getText());
}
}
});
box.add(area1);
box.add(b1);
box.add(area2);
add(box);
}
public static void main(String[] args) {
Handwriting hand = new Handwriting();
hand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hand.setSize(500, 500);
hand.setVisible(true);
}
}
One possible solution would be to make one class as a Inner Class of the other , that way even private instance fields can be accessed

How do I click a JLabel using MouseListener?

I am trying to make this program for minecraft, and now im just getting started. I want that if you click a label, it will check what label is it and will do something.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me.getX()+", "+me.getY()+".");
Object source = me.getSource();
int intx = me.getX();
int inty = me.getY();
if(me.getX()>=1 && me.getY()>=1 && me.getX()<=70 && me.getY()<=45){
permissionsframe.setLocation(810,250);
System.out.println(p1p.length);
permissionsframe.pack();
permissionsframe.setSize(200, 200);
permissionsframe.setVisible(true);
JLabel playerperms = new JLabel("Player "+p1s+" has "+p1p.length+" permissions.");
playerperms.setBounds(1, 1, 150, 150);
permissionsframe.add(playerperms);
System.out.println("You chose "+player1.getText()+".");
}
else{
System.out.println("You did not click any label.");
}
}
});
This selection area is adapted to the name I have now - NonameSL. But if the name will be longer or shorter, the selection area will obviuosly be different...
Is there a way to get the excact label? I tried if(source.equals(player1))(Player 1 is the label) but I placed the label in 1, 1 and I have to click the excact point that I defined the label in, X=1, Y=1. How can I make a mouse listener listen to a label?
There is no need to check if the mouse coordinates are inside your JLabel.
You can bind a Listener to every JLabel an process the click/press event in your MyMouseListener.class
To do so:
You have to add the MouseListener to every JLabel:
MyMouseListener myMouseListener = new MyMouseListener();
label01.setName("name01");
label01.addMouseListener(myMouseListener);
label02.setName("name02");
label02.addMouseListener(myMouseListener);
To identify the JLabel you could do something like this:
class MyMouseListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
JLabel l = (JLabel) e.getSource();
if(l.getName().equals("name01"))
doSomething01();
else if(l.getName().equals("name02"))
doSomething02();
}
}
The correct way to do this is to use the .getComponent() method instead of the .getSource() since this is MouseEvent which is something different than ActionEvent.
Implement the Listener to your class:
public class Main implements MouseListener {
public static void main(String[] args) {
//setup JFrame and some label and then
label.addMouseListener(this);
}
#Override
public void mousePressed(MouseEvent e) {
if (e.getComponent().equals(label)) {
System.out.println("clicked");
}
}
//the other orderride methods..

Java last added button allows agent.move, rest dont

I'm new to java, and have been working on creating a code to get a small picture of a car to move using the keys. My problem is when I add more than 1 buttons to the panel. The function of the button in the code I post is nothing, just prints "button [i] clicked" message. The purpose is for them to read a file and update the locations of the car from the data in the file. This is supposed to be a piece on my Reinforcement Learning project. I thought it would be a good opportunity to learn java since this "graphics package" is not necessary for the project, just a "nice" addition. The code is here:
package graphics;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Agent agent;
private String button = "button.png";
private Image image;
protected JButton b1;
protected JButton b2;
protected JButton b3;
public Board() {
//Keylistener added for the agent to respond to arrow keys
addKeyListener(new TAdapter());
agent = new Agent();
timer = new Timer(10, this); //10ms timer calls action performed
timer.start();
//This part for the button.
ImageIcon i = new ImageIcon(this.getClass().getResource(button));
image = i.getImage();
b1 = new JButton("1", i);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setActionCommand("Active1");
b2 = new JButton("2", i);
b2.setVerticalTextPosition(AbstractButton.CENTER);
b2.setHorizontalTextPosition(AbstractButton.LEADING);
b2.setActionCommand("Active2");
b3 = new JButton("3", i);
b3.setVerticalTextPosition(AbstractButton.CENTER);
b3.setHorizontalTextPosition(AbstractButton.LEADING);
b3.setActionCommand("Active3");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1); add(b2); add(b3);
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
//Transformations for the agent to be painted based upon its position and orientation
AffineTransform trans = new AffineTransform();
trans.rotate(Math.toRadians(agent.getTh()), agent.getX()+64, agent.getY()+64);
trans.translate(agent.getX(), agent.getY());
g2d.drawImage(agent.getImage(), trans, this); // Draws agent with said transformations
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent ae) {
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(true);
if (ae.getActionCommand()=="Active1") {
b1.setEnabled(false);
b2.setEnabled(true);
b3.setEnabled(true);
System.out.println("Clicked 1");
agent.reset();
}
if(ae.getActionCommand()=="Active2") {
b1.setEnabled(true);
b2.setEnabled(false);
b3.setEnabled(true);
System.out.println("Clicked 2");
agent.reset();
}
if (ae.getActionCommand()=="Active3") {
b1.setEnabled(true);
b2.setEnabled(true);
b3.setEnabled(false);
System.out.println("Clicked 3");
agent.reset();
}
agent.move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
agent.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
agent.keyPressed(e);
}
}
}
Now, the problem is this. If I click button 1, or button 2, the other buttons are disabled and it says "Clicked 1 (or 2)" which is fine. But the agent.move() and repaint() aren't invoked. The car doesn't move when I press the keys. If I then click button 3, the other two buttons are disabled and the car moves with the keys.
If I add buttons in a different order add(b3); add(b2); add(b1); then the same happens but this time its button 1 that works fine.
Problems:
Your main problem is one of focus -- When a JButton gets focus and the JPanel loses focus, the JPanel's KeyListener won't work, since the KeyListener requires that the listened to component have the focus, no exception.
A bad solution is to force the JPanel to retain the focus at all times. This will be bad if your window has JButtons and will be disaster if you need to display JTextFields, JTextAreas, or other text components.
A better solution is not to use a KeyListener as it has lots of problems with Swing applications and in particular it has focus issues as noted above. Use Key Bindings instead. Google the tutorial for the gory details on this.
Don't use == to compare Strings, use equals(...) or equalsIgnoreCase(...) instead. The problem is that == checks for object equality, is String A the same object as String B, and you don't care about this. You want to know if the two Strings hold the same chars in the same order which is where the two methods come in.
Don't dispose() of a Graphics object given you by the JVM as this may mess up the painting of a component's borders, children, and have even other side effects.
don't draw in the JPanel's paint(...) method but rather in its paintComponent(...) method just as the tutorials tell you to do. Drawing in paint(...) can have side effects on a component's borders and children if you're not careful, and also does not have the benefit of default double buffering which is important for smooth animation. paintComponent(...) fixes all these problems.
Speaking of which, you should Google and read the Swing graphics tutorials. You can't make stuff up and hope that it will work, and graphics programming will require a whole different approach from what you're used to.
Ignore Andromeda's threading suggestion. While he means well, I suggest that you don't do painting in a background thread. Just move the car with your Swing Timer as you're doing. Background threading has its uses, but not here, as a Timer will work just fine. You will need to use a background thread when you have a long-running process that blocks the calling thread, something that you don't have in your current code, and so a thread "fix" is not needed and in fact is a potential source of problems if you don't take extreme care to make your Swing calls on the Swing event thread. The unknown for us though is what your "agent" is doing. If it is calling long-running code or code with Thread.sleep(...) or wait()/notify() in it, then yes, you will need to use background threading.
But again, we know that's not your primary problem, since your problems began only after adding focus grabbers -- the JButtons. Again this is strong indication that your primary problem is not threading but is use of KeyListeners and their focus requirement.
For example:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.*;
import java.util.EnumMap;
import javax.swing.*;
#SuppressWarnings("serial")
public class KeyBindingPanel extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = PREF_W;
private static final Stroke THICK_STROKE = new BasicStroke(5f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static final int OVAL_WIDTH = 30;
private static final int OVAL_HEIGHT = 30;
private static final Color OVAL_COLOR = Color.red;
private static final Color BKGRD_COLOR = Color.black;
private static final int TIMER_DELAY = 20;
public static final int STEP = 2;
private int myX = 0;
private int myY = 0;
private JButton[] buttons = new JButton[3];
private int condition = WHEN_IN_FOCUSED_WINDOW;
private InputMap inputMap = getInputMap(condition);
private ActionMap actionMap = getActionMap();
private EnumMap<Direction, Boolean> directionMap = new EnumMap<Direction, Boolean>(
Direction.class);
public KeyBindingPanel() {
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(new ButtonAction());
add(buttons[i]);
}
setBackground(BKGRD_COLOR);
for (final Direction direction : Direction.values()) {
directionMap.put(direction, Boolean.FALSE);
Boolean[] onKeyReleases = { Boolean.TRUE, Boolean.FALSE };
for (Boolean onKeyRelease : onKeyReleases) {
KeyStroke keyStroke = KeyStroke.getKeyStroke(
direction.getKeyCode(), 0, onKeyRelease);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new DirAction(direction,
onKeyRelease));
}
}
new Timer(TIMER_DELAY, new GameTimerListener()).start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2b = (Graphics2D) g.create();
g2b.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2b.setStroke(THICK_STROKE);
g2b.setColor(OVAL_COLOR);
g2b.drawOval(myX, myY, OVAL_WIDTH, OVAL_HEIGHT);
g2b.dispose(); // since I created this guy
}
private class GameTimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
for (Direction direction : Direction.values()) {
if (directionMap.get(direction)) {
myX += STEP * direction.getRight();
myY += STEP * direction.getDown();
}
}
repaint();
}
}
private class DirAction extends AbstractAction {
private Direction direction;
private boolean onRelease;
public DirAction(Direction direction, boolean onRelease) {
this.direction = direction;
this.onRelease = onRelease;
}
#Override
public void actionPerformed(ActionEvent evt) {
directionMap.put(direction, !onRelease); // it's the opposite!
}
}
private class ButtonAction extends AbstractAction {
public ButtonAction() {
super("Press Me!");
}
#Override
public void actionPerformed(ActionEvent e) {
JButton thisBtn = (JButton) e.getSource();
for (JButton btn : buttons) {
if (btn == thisBtn) {
btn.setEnabled(false);
} else {
btn.setEnabled(true);
}
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("KeyBindingPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new KeyBindingPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum Direction {
UP(KeyEvent.VK_UP, -1, 0), DOWN(KeyEvent.VK_DOWN, 1, 0), LEFT(
KeyEvent.VK_LEFT, 0, -1), RIGHT(KeyEvent.VK_RIGHT, 0, 1);
private int keyCode;
private int down;
private int right;
private Direction(int keyCode, int down, int right) {
this.keyCode = keyCode;
this.down = down;
this.right = right;
}
public int getKeyCode() {
return keyCode;
}
public int getDown() {
return down;
}
public int getRight() {
return right;
}
}
It seems you need to use Threading. You can make your class to implements Runnable and then override the public void run(){} method and do the painting there.

Categories

Resources