0

I am making bouncing balls, there are icons on the labels in the codes. When I multiply the variables 'hizx','hizx2','hizx3' by -1, they only hop on the x-axis. If I write the variables 'hizy', 'hizy2', 'hizy3' in the comment line as I did, the icons get weird when the balls cross each other.Could you help?

How can I make icons bounce after touching each other?

Pictures I used

enter image description here

enter image description here

enter image description here

form starter

package odev5_m_metin;

import javax.swing.*;

public class giris {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                odev_intersect o1 = new odev_intersect();
                o1.setVisible(true);
            }
        });
    }
}

Main code:

package odev5_m_metin;

import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Rectangle;


public class odev_intersect extends JFrame {

    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;

    static int x,y,x2,y2,x3,y3,hizx = 5, hizy = 4,hizx2 = 5, hizy2 = 7,hizx3=5,hizy3=5;
    static Timer time1;
    static TimerTask g1,g2;

    static Rectangle r1;
    static Rectangle r2;
    static Rectangle r3;

    public odev_intersect() {
        add(panel1);
        setSize(500,500);
        setTitle("Intersect Ödev");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label1.setBounds(150,150,50,50);
        label2.setBounds(150,300,50,50);
        label3.setBounds(450,450,50,50);

        x=100;
        y=200;
        x2=200;
        y2=200;
        x3=300;
        x3=300;

        time1 = new Timer();

        g1 = new TimerTask() {
            @Override
            public void run() {
                r1 = new Rectangle(label1.getX(),label1.getY(),label1.getWidth(),label1.getHeight());
                r2 = new Rectangle(label2.getX(),label2.getY(),label2.getWidth(),label2.getHeight());
                r3 = new Rectangle(label3.getX(),label3.getY(),label3.getWidth(),label3.getHeight());

                x+=hizx;
                y+=hizy;
                x2+=hizx2;
                y2+=hizy2;
                x3+=hizx3;
                y3+=hizy3;

                if (x>=440 || x<=0) hizx*=-1;
                if (y>=410 || y<=0) hizy*=-1;
                if (x2>=440 || x2<=0) hizx2*=-1;
                if (y2>=410 || y2<=0) hizy2*=-1;
                if (x3>=440 || x3<=0) hizx3*=-1;
                if (y3>=410 || y3<=0) hizy3*=-1;

                if (r1.intersects(r2) || r1.intersects(r3)) hizx*=-1; //hizy*=-1;
                if (r2.intersects(r1) || r2.intersects(r3)) hizx2*=-1; //hizy2*=-1;
                if (r3.intersects(r2) || r3.intersects(r1)) hizx3*=-1; //hizy3*=-1;

                label1.setBounds(x,y,50,50);
                label2.setBounds(x2,y2,50,50);
                label3.setBounds(x3,y3,50,50);
            }
        };

        time1.schedule(g1,0,10);
    }
}
  • Does this answer your question? [Java - Determining if two Ellipses intersect](https://stackoverflow.com/questions/71430237/java-determining-if-two-ellipses-intersect) – Yanick Rochon Oct 27 '22 at 16:56

1 Answers1

0

The code in your question is not a minimal, reproducible example since it throws NullPointerException because variables, like panel1, are not assigned values.

Therefore the below code simply demonstrates how to "bounce balls" and is not a "fixed" version of the code in your question. Hopefully it will help you to write your intended application.

In order to perform animation in Swing you need to use class javax.swing.Timer (and not class java.util.Timer).

If you want to place the JLabels anywhere inside the JFrame then you should set the layout manager to null. In the below code, I null the layout manager of the content pane.

The below code displays only two balls (and not three as in the code in your question) merely in order to show you how to check whether the two balls are touching. Because of this, the below code is not an accurate simulation as it does not obey the laws of physics.

The below code uses method references.

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class Bouncing {
    private int  factorBasketball = 1;
    private int  factorSoccerballX = 1;
    private int  factorSoccerballY = -1;
    private JFrame  frame;
    private JLabel  basketball;
    private JLabel  soccerball;

    private void buildAndDisplayGui() {
        frame = new JFrame("Balls");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(null);
        basketball = new JLabel(new ImageIcon("DA0zx.png"));
        basketball.setBounds(150, 150, 50, 50);
        contentPane.add(basketball);
        soccerball = new JLabel(new ImageIcon("WgQXM.png"));
        soccerball.setBounds(150, 300, 50, 50);
        contentPane.add(soccerball);
        frame.setSize(500,500);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        Timer timer = new Timer(50, this::timerTask);
        timer.start();
    }

    private void timerTask(ActionEvent event) {
        Rectangle boundsBasketball = basketball.getBounds();
        Rectangle boundsSoccerball = soccerball.getBounds();
        if (boundsBasketball.intersects(boundsSoccerball)) {
            factorBasketball *= -1;
        }
        boundsBasketball.x += 10 * factorBasketball;
        boundsBasketball.y += 10 * factorBasketball;
        if (boundsBasketball.x >= 440  ||
            boundsBasketball.x <= 0    ||
            boundsBasketball.y >= 410  ||
            boundsBasketball.y <= 0) {
            factorBasketball *= -1;
        }
        basketball.setBounds(boundsBasketball);
        boundsSoccerball.x += 10 * factorSoccerballX;
        boundsSoccerball.y += 10 * factorSoccerballY;
        if (boundsSoccerball.x >= 440  ||
            boundsSoccerball.x <= 0    ||
            boundsSoccerball.y >= 410  ||
            boundsSoccerball.y <= 0) {
            factorSoccerballX *= -1;
            factorSoccerballY *= -1;
        }
        soccerball.setBounds(boundsSoccerball);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Bouncing().buildAndDisplayGui());
    }
}

screen capture of running Swing app

Abra
  • 19,142
  • 7
  • 29
  • 41