0

So I am trying to make a copy of a game (http://www.aimbooster.com/) for a final project in a class, but I cannot think for the life of me how I make the targets. I have tried a paint component but there is no way to click a paint component. Does anyone have some ideas? Here is my code

package Final_Project;

import javax.swing.JFrame;

public class AimFrame
{

    public static void main(String[] args)
    {
        {
            JFrame frame = new JFrame("Aim Booster");
            frame.setDefaultCloseOperation(3);
            frame.getContentPane().add (new AimPanel(600,600));
            frame.pack();
            frame.setVisible(true);
        }
    }
}

And the main code is

package Final_Project;

import java.awt.*;

import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

public class AimPanel extends JPanel
{
    private int width, height;
    
    private int px, py, pw, ph;
    private Timer circletimer;
    private Timer targettimer;
    
    private ArrayList<Point> circles;
    private ArrayList<Point> targets;

    public AimPanel(int width, int height)
    {
        this.width = width;
        this.height = height;
        this.setPreferredSize(new Dimension(width, height));
        this.setBackground(Color.gray);
        
        px = 0;
        py = 0;
        pw = ph = 50;
        
        this.addMouseListener(new ClickListener());
        
        circles = new ArrayList<Point>();
        circletimer = new Timer (3000,new circlelistener());
        targettimer = new Timer (5000, new circlelistener());
        
        targets = new ArrayList<Point>();
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.black);
        
        for(int i = 0; i < circles.size(); i++)
        {
            g.fillOval(circles.get(i).x, circles.get(i).y - 10, 10, 10);
        }

        super.paintComponent(g);
        g.setColor(Color.orange);
        
        for(int i = 0; i < targets.size(); i++)
        {
            g.fillRect(targets.get(i).x, targets.get(i).y -50, 50, 50);
        }
    }
    
    private class ClickListener implements MouseListener
    {

        @Override
        public void mouseClicked(MouseEvent e)
        {
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
    System.out.println("Clicked");
            
            //add points to circle list
            circles.add(new Point(e.getX(), e.getY()));
            circletimer.start();
            
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }
    }
    
    private class circlelistener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (circles.size()>0)
                circles.remove(0);
            repaint();
            
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Razers
  • 1
  • 3
    _there is no way to click a paint component_ As you have done, in the code in your question, i.e. add a mouse listener. You need to save the coordinates and dimensions of your targets and check whether the mouse was clicked inside one of them. – Abra Jan 12 '21 at 14:13
  • Hi Razers, thanks for explaining your question clearly and including your (nice clear) code. A tiny point to bear in mind when including code is to try to make sure the `code block` markers surround all the code else it is a bit more difficult to read – Chris Jan 12 '21 at 14:25
  • @Abra im not sure if u looked at the game I creating but one of the goals of the game is to click the targets as fast as possible but the targets randomize so I don't know if there is a good way to save the coordiantes of my targets – Razers Jan 12 '21 at 14:28
  • @Abra my teacher recommended I try a boolean but I am not sure how that would work – Razers Jan 12 '21 at 14:28
  • One thing that will reduce bloat is to use a MouseAdapter instead of a MouseListener (Extend this - it implements the methods of MouseListener with a dummy implementation and you override the ones you want to use) – William Jarvis Jan 12 '21 at 14:34
  • @Abra just type into a browser aimbooster.com, specifically in google – Razers Jan 12 '21 at 14:35
  • @WilliamDutton I would give it a shot but this is for a final project in a class and my teacher never taught us mouse adapter, so it wouldn't necessarily look good if that's what I did – Razers Jan 12 '21 at 14:36
  • Create an application model that holds a java.util.List of Circle instances. You would define the Circle class to hold a center java.awt.Point and an int radius that varies. The view would draw the List of Circle instances. Your controller class (Mouse Listener) would determine if the mouse Point is inside a Circle instance and "pop" the Circle instance if it is. See this [Stack Overflow answer](https://stackoverflow.com/questions/63793252/java-swing-making-a-growing-circle-by-mouse-click-on-jpanel/63796411#63796411) for a Java application that creates growing circles where you click. – Gilbert Le Blanc Jan 12 '21 at 16:55
  • See [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson Jan 12 '21 at 21:16

0 Answers0