2

I don't know how to check if I click on a circle in JPanel. And if I'm at it, i have a problem with the "deleting"(redraw in white) part in the thread because it let's behind some stuff. Basically I want to make a game where if you click start circles will appear randomly on the screen at a given rate and they start to decrease as soon as they appear.If you click on them(the selection part) they dissapear and it gives you 1 score. If the circle dissapears you get -1 score. And I made the stop part too in a way. Here is the code.

My JFrame

public class JFrame extends javax.swing.JFrame {

    Minge m;
    GenerareMinge mg;

    /**
     * Creates new form JFrame
     */
    public JFrame() {
        initComponents();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 500, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 500, Short.MAX_VALUE));

        jToggleButton1.setText("START");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Score: 0");

        jButton1.setText("STOP");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(0, 29, Short.MAX_VALUE))).addContainerGap()));
        layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 11, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addGap(28, 28, 28).addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(36, 36, 36).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(38, 38, 38).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    } // </editor-fold>                        
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        Graphics g = jPanel1.getGraphics();

        Random rand = new Random();

        Color randomColor = new Color(1, 1, 1);

        int latime = jPanel1.getWidth(),
        inaltime = jPanel1.getHeight();

        mg = new GenerareMinge(inaltime, latime, true, g, randomColor);
        mg.start();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        mg.finish = false;
    }
}

My Object

public class Cerc {

    public int score = 0;
    int x,
    y,
    r;
    Graphics g;
    Color c;

    int ok = 2;

    public Cerc(int x, int y, int r, Graphics g, Color c) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.g = g;
        this.c = c;
    }

    public Cerc(Graphics g) {

    }

    void draw() {
        g.setColor(c);
        g.drawOval(x - r, y - r, 2 * r, 2 * r);
        g.fillOval(x - r, y - r, 2 * r, 2 * r);
    }

    void show() {
        g.setColor(c);
        draw();
    }

    void delete() {
        g.setColor(Color.white);
        g.drawOval(x - r, y - r, 2 * r, 2 * r);
        g.fillOval(x - r, y - r, 2 * r, 2 * r);
    }

    void minimize(int dx) {

        delete();

        Random rand = new Random();
        float valRand = rand.nextFloat() * 2;
        r -= valRand;
        show();
    }

}

My thread for circle generation when pressing on jButton

public class GenerareMinge extends Thread {

    boolean finish;
    int inaltime;
    int latime;
    Graphics g;
    Color color;

    public GenerareMinge(int inaltime, int latime, boolean finish, Graphics g, Color color) {
        this.finish = finish;
        this.latime = latime;
        this.inaltime = inaltime;
        this.g = g;
        this.color = color;
    }

    @Override
    public void run() {

        while (finish) {

            try {
                Random rand = new Random();
                float R = rand.nextFloat();
                float G = rand.nextFloat();
                float B = rand.nextFloat();

                color = new Color(R, G, B);

                Cerc c = new Cerc(rand.nextInt(inaltime), rand.nextInt(latime), rand.nextInt(100), g, color);
                c.draw();

                Minge m = new Minge(c, true);
                m.start();

                sleep(200);
            } catch(InterruptedException ex) {
                Logger.getLogger(Minge.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
}

My thread for minimizing

public class Minge extends Thread {

    Cerc c;
    boolean finish;
    public Minge(Cerc c, boolean finish) {
        this.c = c;
        this.finish = finish;
    }

    @Override
    public void run() {

        while (finish) {
            c.minimize(1);

            try {
                sleep((int)(50 * Math.random()));
            } catch(InterruptedException ex) {
                Logger.getLogger(Minge.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
}
MelvinWM
  • 749
  • 4
  • 13
Rex Brute
  • 21
  • 1
  • Does this answer your question? [Point within circle](https://stackoverflow.com/q/7398901/5221149) – Andreas Jun 06 '20 at 06:21
  • I would recommend that you reformat your code in your editor/IDE of choice, there should be automatic tools for doing so. – MelvinWM Jun 06 '20 at 07:12
  • I am a bit worried about your use of threads, they are a somewhat 'advanced' topic. But as you write, you need some way to make the circles fade away or similar. I would recommend that you use something different that will help you avoid having to use threads, maybe a single instance of https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html that fades all your circles. In a typical game engine, there would be a "game loop", executed a number of times per second (related to "frames per second" or FPS), and then you could just fade the circles in that loop. – MelvinWM Jun 06 '20 at 07:22
  • You've clicked inside a circle if the distance from the center of the circle to the clicked point is less than or equal to the radius. The distance formula is Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); – Gilbert Le Blanc Jun 06 '20 at 07:27

0 Answers0