0

In assignment 5 I faced a problem in moving my basket. it only moves one to right and one to left and also it prints the picture of the baskets over and over without removing the old one. Also after compleating step 6 and running the code. How can I solve it? enter image description here

this is my DrawingPanel.java 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 {

// Adding ArrayList to store game elements

private ArrayList<DrawingObject> gameElements;
private ControlElement basket;
private ControlElement launcher;
private Ball ball;
private int score;
private PlayClip ding;
private PlayClip fail;


private Timer t;

private Random r;

public DrawingPanel() {
    super();
    // TODO Auto-generated constructor stub
    // create a list of game elements (which are DrawingObjects):
    gameElements = new ArrayList<DrawingObject>();

    // initializing the timer
    // repaint all elements every 20ms
    t = new Timer(30, (e) -> repaint());
    t.start();

    // initialize the Random generator:
    r = new Random();
    score = 0;

    // initialize sounds with the sound files:
    ding = new PlayClip("sound/Ding.wav");
}

@Override
protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    if (basket == null) {// if there is no basket, create it
        newBasket();
    }
    if (launcher == null) {// if there is no launcher, create it
        newLauncher();
    }
    if (ball == null) {// if there is no ball, create it
        ball();
    }

    for (DrawingObject e : gameElements) { // for each game element
        e.paintComponent(g); // draw the element
    }
    for (DrawingObject o : gameElements) { // for each game element
        o.paintComponent(g); // draw the element
    }
    for (int i = gameElements.size() - 1; i >= 1; i--) { // setting order of the objects drawn - last>first
        gameElements.get(i).paintComponent(g); // Draw element i
    }
    // there is a chance of 1 in 20 on a new ball:
    if (r.nextInt(20) == 1) {
        ball();
    }

    // creating a basket
    basket = new ControlElement("basket.png", this, getWidth() / 2, getHeight() - 25);
    gameElements.add(basket);

    // creating a loop to draw game elements

    launcher = new ControlElement("launcher.png", this, getWidth() / 50, getHeight() - 57);
    gameElements.add(launcher);

    ball = new Ball(getWidth(), getHeight());
    gameElements.add(ball);

    if (captured(basket.getX(), basket.getY()));
        System.out.println("Ball Captured!");
        ding.play();



for (DrawingObject e : gameElements) {
    if (e instanceof Ball && !e.visible()) { // only invisible Balls will be removed
        gameElements.remove(e);
        System.out.println("Ball missed");
        fail.play();
        break; // stop the loop
        }
    }
}

public void newLauncher() {
}

public void newBasket() {
}

public void ball() {
}

public void left() {

    basket.setMoveL(true);
    basket.move();
    System.out.println("left");

}

public void right() {

    basket.setMoveL(false);
    basket.move();
    System.out.println("right");

}

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

}

and GameUI.java

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;// declare class variable

/**
 * 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() {
    setSize(600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    panel = new DrawingPanel();// declaration of constructor
    panel.setBackground(Color.BLACK);
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(panel,
            Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE));
    gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(panel,
            Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE));
    contentPane.setLayout(gl_contentPane);
    // activating key listener
    addKeyListener(this);
}

public void keyTyped(KeyEvent e) {
}

public void keyPressed(KeyEvent e) {

    // show keycode of pressed key:
    System.out.println("keyPressed " + e.getKeyCode());

    if (e.getKeyCode() == 37) {// if left key is pressed
        panel.left();
    } else if (e.getKeyCode() == 39) {// if left key is pressed
        panel.right();
    } else if (e.getKeyCode() == 32) {// if space button is pressed
        panel.ball();
    }
}

public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

}

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

Try to get help during the chat this morning! If that does not help, let me know!

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

This question was answered on the chat, but just to summarize for others:

  • The newBasket, newLauncher and ball methods are empty, the lines that were supposed to be in there are placed in the paintComponent method instead.
  • This part makes no sense, balls will be created randomly instead
if (ball == null) {// if there is no ball, create it
        ball();
    }
  • These for loops are basically all doing the same thing, so all elements now get drawn three times. Two of them can be removed.
for (DrawingObject e : gameElements) { // for each game element
        e.paintComponent(g); // draw the element
    }
    for (DrawingObject o : gameElements) { // for each game element
        o.paintComponent(g); // draw the element
    }
    for (int i = gameElements.size() - 1; i >= 1; i--) { // setting order of the objects drawn - last>first
        gameElements.get(i).paintComponent(g); // Draw element i
    }
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.