0

I double checked everything and it seems okay yet the basket is not moving. (while checking whether I converted local to field there wasn't any switch. The only line I had as the class variable was this line " private final DrawingPanel panel= new DrawingPanel(); " so I deleted this and add the example codes that you can see above. GameUI:

package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import model.DrawingObject;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class GameUI extends JFrame implements KeyListener {
    private DrawingPanel panel;
    private JPanel contentPane;

    /**
     * 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() {
        panel = new DrawingPanel();
        panel.setBounds(new Rectangle(0, 0, 600, 400));

        panel.setBackground(Color.BLACK);
        setBounds(new Rectangle(0, 0, 600, 400));
        setBackground(Color.BLACK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 659, 456);
        contentPane = new JPanel();
        contentPane.setBounds(new Rectangle(0, 0, 600, 400));
        contentPane.setBackground(Color.LIGHT_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(panel, GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(panel, GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
        );
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 631, Short.MAX_VALUE)
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGap(0, 399, Short.MAX_VALUE)
        );
        panel.setLayout(gl_panel);
        contentPane.setLayout(gl_contentPane);

        addKeyListener(this);

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        // show keycode of pressed key:
        System.out.println("keyPressed " + e.getKeyCode());
        if (e.getKeyCode()==39);{
            panel.right();
        }
        if (e.getKeyCode()==37); {
            panel.left();
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

Drawing Panel:

package view;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

import model.Ball;
import model.ControlElement;
import model.DrawingObject;

    public class DrawingPanel extends JPanel {
    private ArrayList<DrawingObject> gameElements;
    private ControlElement basket;
    private ControlElement launcher;
    private Timer t;
    private Random r;
    private Ball ball;
    private int score = 0;

    public void newBasket() { //new method and the constructor for the basket
        basket = new ControlElement("basket.png", this, getWidth() / 2, getHeight() - 25);
        gameElements.add(basket);
    }

    public void newLauncher() { //new method and the constructor for launcher
        launcher = new ControlElement("launcher.png", this, 0, getHeight() - 52);
        gameElements.add(launcher);
    }

    public void left() {
        basket.setMoveL(true); //when setMoveL is true the basket will move to the left
        basket.move(); //calling the move method
    }

    public void right() {
        basket.setMoveL(false); //when setMoveL is false the basket will move to the left
        basket.move(); //calling the move method
    }

    public void newBall() {
        ball = new Ball(getWidth(), getHeight());
        gameElements.add(ball);
    }

    public boolean captured(int x, int y) {
        for (DrawingObject o : gameElements) {
            if (o instanceof Ball ) { // only Balls can be captured
                if (x >= o.getX() - o.getWidth() && x <= o.getX() + o.getWidth() &&
                        y >= o.getY() - o.getHeight() && y <= o.getY() + o.getHeight()) {
                    gameElements.remove(o);
                    score++;
                    return true;
                }
            }
        }
        return false;
    }

    public DrawingPanel() {
        super();
        // TODO Auto-generated constructor stub
        // create a list of game elements (which are DrawingObjects):
                gameElements = new ArrayList<DrawingObject>();
        //initializing the timer:
                t = new Timer(20, (e) -> repaint() ); // timer calls repaint method after each clock tick
                t.start(); //calling start method
        // initialize the Random generator:
                r = new Random();

    }
    @Override
    protected void paintComponent(Graphics arg0) {
        // TODO Auto-generated method stub
        super.paintComponent(arg0);


        if (basket==null) { // if no basket is made yet, create it
            newBasket();
        }
        if (launcher==null) { //if no launcher is made yet, create it
            newLauncher();  
        }
        for (DrawingObject e: gameElements) {
            e.paintComponent(arg0);
        }
        // there is a chance of 1 in 20 on a new ball:
        if (r.nextInt(20) == 1) {
            newBall();
        }
        //see if the basket captured anything
        //if anything captured show the notification in the console


    }



}

Can you help me with this? Thank you...

Share a link to this question (includes your user id)
| edit | | close | delete |
0

You have two semicolons ';' were they should not be:

if (e.getKeyCode()==39);{
    panel.right();
}
if (e.getKeyCode()==37); {
    panel.left();
}

It should be:

if (e.getKeyCode()==39) {
    panel.right();
}
if (e.getKeyCode()==37) {
    panel.left();
}

If you put a ';' right after an if-statement, it basically means an empty statement will be executed when the if-condition is true. The { ... } code after it will be seen as just a block of code with no relation to the if-statement.

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.