سؤال

There are lots of questions like this one, but I checked them all out and none of them solved the problem I have, so please don't close this as a duplicate.

I am making a game with one big circle in the middle, surrounded by six other circles which are gradually increasing in size. I want to end the game if one of the six circles collides with the central circle. Can anyone provide a suitable solution?

Here is my code:

package virus;


import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class VirusGamePanel extends JPanel implements MouseListener{
    private static final long serialVersionUID = 1L;//serialVersionUID field
    Random colour = new Random();//the outside ovals will always be a random colour
    private int sizeX1 = 0;//the x size of the outside ovals 
    private int sizeX2 = 0;
    private int sizeX3 = 0;
    private int sizeX4 = 0;
    private int sizeX5 = 0;
    private int sizeX6 = 0;

    private int sizeY1 = 0;//the y size of the outside ovals
    private int sizeY2 = 0;
    private int sizeY3 = 0;
    private int sizeY4 = 0;
    private int sizeY5 = 0;
    private int sizeY6 = 0;
    int score = 0;

    static String scorestring = "Score: ";
    Color rand = new Color(colour.nextInt(255), colour.nextInt(255), colour.nextInt(255)); //generate the random colour

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.magenta);
        g.drawString(scorestring+score,275,250);
        g.setColor(Color.orange);
        g.drawOval(200, 150, 200, 200);
        g.setColor(rand);
        g.fillOval(300 - sizeX1 / 2, 50 - sizeY1 / 2, sizeX1, sizeY1);//these six ovals are supposed to increase in size
        g.fillOval(130 - sizeX2 / 2,100 - sizeY2 / 2, sizeX2, sizeY2);
        g.fillOval(480 - sizeX3 / 2,100 - sizeY3 / 2, sizeX3, sizeY3);
        g.fillOval(130 - sizeX4 / 2,400 - sizeY4 / 2, sizeX4, sizeY4);
        g.fillOval(480 - sizeX5 / 2,400 - sizeY5 / 2, sizeX5, sizeY5);
        g.fillOval(305 - sizeX6 / 2,450 - sizeY6 / 2, sizeX6, sizeY6);


        try
        {
            Thread.sleep(100);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        inc();
    }


    private void inc()//increase the size of the ovals
    {

            sizeX1++;
            sizeY1++;
            sizeX2++;
            sizeY2++;
            sizeX3++;
            sizeY3++;
            sizeX4++;
            sizeY4++;
            sizeX5++;
            sizeY5++;
            sizeX6++;
            sizeY6++;
            repaint();

    }


    public static void main(String[] args) {}
هل كانت مفيدة؟

المحلول

Calculating whether circles overlap is not that hard. 2 circles will have overlap as soon as the sum of the radius of both circles becomes equal to or greater then the distance between their center points.

Putting those into Google gives the required formulas:

Next to that, some remarks about your code

  • Override the paintComponent method instead of the paint method
  • Do not call Thread.sleep on the Event Dispatch Thread as this will block the UI. My guess is that with the current code, you will never see a repaint. Consult the Concurrency in Swing tutorial for more information. The solution for what you want to do is using a Swing Timer
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top