0

This is my code for now. If you run it, it shows three squares in the right down corner, and if you click shoot button, a ball will be thrown in a parabola movement. I want to make the program in a way that with collision of the ball and any of the squares that square becomes invisible. Something same as angry birds type games.How should I do it?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;

public class ProjectileShooterTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600, 600);
        final ProjectileShooter projectileShooter = new ProjectileShooter();
        ProjectileShooterPanel projectileShooterPanel = new ProjectileShooterPanel(
                projectileShooter);
        projectileShooter.setPaintingComponent(projectileShooterPanel);
        JPanel controlPanel = new JPanel(new GridLayout(1, 0));
        controlPanel.add(new JLabel("Angle"));
        final JSlider angleSlider = new JSlider(0, 90, 45);
        controlPanel.add(angleSlider);
        controlPanel.add(new JLabel("Power"));
        final JSlider powerSlider = new JSlider(0, 100, 50);
        controlPanel.add(powerSlider);
        JButton shootButton = new JButton("Shoot");
        shootButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int angleDeg = angleSlider.getValue();
                int power = powerSlider.getValue();
                projectileShooter.setAngle(Math.toRadians(angleDeg));
                projectileShooter.setPower(power);
                projectileShooter.shoot();
            }
        });
        controlPanel.add(shootButton);
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(controlPanel, BorderLayout.NORTH);
        f.getContentPane().add(projectileShooterPanel, BorderLayout.CENTER);
        f.setVisible(true);
    }
}

class ProjectileShooter {
    private double angleRad = Math.toRadians(45);
    private double power = 50;
    private Projectile projectile;
    private JComponent paintingComponent;

    void setPaintingComponent(JComponent paintingComponent) {
        this.paintingComponent = paintingComponent;
    }

    void setAngle(double angleRad) {
        this.angleRad = angleRad;
    }

    void setPower(double power) {
        this.power = power;
    }

    void shoot() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                executeShot();
            }
        });
        t.setDaemon(true);
        t.start();
    }

    private void executeShot() {
        if (projectile != null) {
            return;
        }
        projectile = new Projectile();
        Point2D velocity = AffineTransform.getRotateInstance(angleRad).transform(
                new Point2D.Double(1, 0), null);
        velocity.setLocation(velocity.getX() * power * 0.5, velocity.getY() * power * 0.5);
        projectile.setVelocity(velocity);
        long prevTime = System.nanoTime();
        while (projectile.getPosition().getY() >= 0) {
            long currentTime = System.nanoTime();
            double dt = 3 * (currentTime - prevTime) / 1e8;
            projectile.performTimeStep(dt);
            prevTime = currentTime;
            paintingComponent.repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
        projectile = null;
        paintingComponent.repaint();
    }

    Projectile getProjectile() {
        return projectile;
    }
}

class Projectile {
    private final Point2D ACCELERATION = new Point2D.Double(0, -9.81 * 0.1);
    private final Point2D position = new Point2D.Double();
    private final Point2D velocity = new Point2D.Double();

    public Point2D getPosition() {
        return new Point2D.Double(position.getX(), position.getY());
    }

    public void setPosition(Point2D point) {
        position.setLocation(point);
    }

    public void setVelocity(Point2D point) {
        velocity.setLocation(point);
    }

    void performTimeStep(double dt) {
        scaleAddAssign(velocity, dt, ACCELERATION);
        scaleAddAssign(position, dt, velocity);
    }

    private static void scaleAddAssign(Point2D result, double factor, Point2D addend) {
        double x = result.getX() + factor * addend.getX();
        double y = result.getY() + factor * addend.getY();
        result.setLocation(x, y);
    }
}

class ProjectileShooterPanel extends JPanel {
    private final ProjectileShooter projectileShooter;

    public ProjectileShooterPanel(ProjectileShooter projectileShooter) {
        this.projectileShooter = projectileShooter;
    }

    @Override
    protected void paintComponent(Graphics gr) {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D) gr;
        Projectile projectile = projectileShooter.getProjectile();
        if (projectile != null) {
            g.setColor(Color.RED);
            Point2D position = projectile.getPosition();
            int x = (int) position.getX();
            int y = getHeight() - (int) position.getY();
            Ellipse2D.Double gerd = new Ellipse2D.Double(x - 01, y - 10, 20, 20);
            g.draw(gerd);
            // g.fillOval(x-01, y-10, 20, 20);
        }
        Rectangle hadaf1 = new Rectangle(450, 450, 50, 50);
        Rectangle hadaf2 = new Rectangle(500, 450, 50, 50);
        Rectangle hadaf3 = new Rectangle(475, 400, 50, 50);
        g.draw(hadaf1);
        g.draw(hadaf2);
        g.draw(hadaf3);
    }
}

I will be so thankful if you help me.

  • 1
    I didn't go through all your code (you've posted a lot, perhaps too much, and not well formatted -- with too many multi-lined blank lines), but this line, `Projectile projectile = projectileShooter.getProjectile();` should definitely **not** be within the paintComponent method. It should go elsewhere, where I can't say just yet, but not within paintComponent since that method should be for painting and painting only. Please format your code including getting rid of multi-lined blank lines. They only make it harder to read your code. – Hovercraft Full Of Eels Jan 24 '16 at 04:27
  • 1
    Also, you've posted a "want" and not a question. What specifically are you asking? What have you tried that's not working? – Hovercraft Full Of Eels Jan 24 '16 at 04:29
  • OK, I edited your code, but in the future, please do this for us. – Hovercraft Full Of Eels Jan 24 '16 at 04:30
  • I want you to guide me how should I do it? – user2266688 Jan 24 '16 at 04:34
  • 1
    Start by placing the `Rectangle`s in `List`, this way, you only need to update the `List` and call `repaint` to allow the view to updated. Also beware, you're risking a possible race condition between your `executeShot` thread the EDT – MadProgrammer Jan 24 '16 at 04:50
  • I am new in java, could you show me how? – user2266688 Jan 24 '16 at 04:55
  • 1
    [Collections Trail](http://docs.oracle.com/javase/tutorial/collections/) would be a good start – MadProgrammer Jan 24 '16 at 04:57
  • 2
    So, I've stuffed around with your code, which basically resulted in rewriting 99% of it. Suffice to say, you have some work to do. Start by focusing on separating the logic of the game from the view. The view should be dumb and simply render the current state of the model. You might want to read up on [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) – MadProgrammer Jan 24 '16 at 05:19

0 Answers0