My basket will not move. Using the console I've checked whether the keyPressed methods are being called; this is the case. Also, adding the basket.setMoveL method to the constructor does not cause the basket to move.
This is the code in the DrawingPanel class:
package view;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
import model.ControlElement;
import model.DrawingObject;
public class DrawingPanel extends JPanel {
private ArrayList<DrawingObject> gameElements;
private ControlElement basket;
private ControlElement launcher;
private Timer t;
public DrawingPanel() {
super();
gameElements = new ArrayList<DrawingObject>();
t = new Timer(20, (e) -> repaint() );
t.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (basket==null) { // if no basket is made yet, create it
newBasket();
}
if(basket!=null) {
basket.setMoveL(true);
}
if (launcher==null) { // if no basket is made yet, create it
newLauncher();
}
for(int i = 0; i < gameElements.size(); i++) {
gameElements.get(i).paintComponent(g);
}
}
public void newBasket() {
basket = new ControlElement("basket.png", this, getWidth() / 2, getHeight() - 25);
gameElements.add(basket);
}
public void newLauncher() {
launcher = new ControlElement("launcher.png", this, 0, getHeight() - 53);
gameElements.add(launcher);
}
//Left and Right methods
public void left() {
basket.setMoveL(true);
}
public void right() {
basket.setMoveL(false);
}
public void stop() {
t.stop();
}
}
And this the GameUI class:
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
public class GameUI extends JFrame implements KeyListener {
private JPanel contentPane;
private DrawingPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameUI frame = new GameUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GameUI() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
panel = new DrawingPanel();
panel.setBackground(Color.BLACK);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
);
contentPane.setLayout(gl_contentPane);
//Activating the key listener
addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
//Move left if left arrow is pressed
if(e.getKeyCode() == 37) {
panel.left();
}
//Move right if right arrow is pressed
if(e.getKeyCode() == 39) {
panel.right();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Do you see any mistakes in the code?