Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:30:05+00:00 2026-05-27T06:30:05+00:00

What is the best approach to define appropriate coordinates for second shape, taking into

  • 0

What is the best approach to define appropriate coordinates for second shape, taking into account that none should overlap each other? Here is the code for action performed when timer fires:

@Override
    public void actionPerformed(ActionEvent e) {

        if(allowedToDrawRing || allowedToDrawSquare){

        xRing = (int) ((getWidth() - ringSize) * (Math.random()));
        yRing = (int) ((getHeight() - ringSize) * (Math.random()));

        xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
        ySquare = (int) ((getHeight() - squareSize) * (Math.random()));

        isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing)  && ySquare>=yRing && ySquare<=(yRing+ringSize);

        while (isOverlaped) {
            xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
            ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
            isOverlaped = xSquare>=xRing && xSquare<=(ringSize+xRing)  && ySquare>=yRing && ySquare<=(yRing+ringSize);
        }
        setParamsRing(xRing, yRing, ringSize);
        setParamsSquare(xSquare,ySquare, squareSize);
        repaint();
       }
    }

Source code:

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
import javax.swing.Timer;

public class mull extends JPanel implements ActionListener {

    private static JPanel p;
    Timer timer;
    ActionListener updateProBar;
    private double esize;
    private double maxSize = 0;
    private boolean initialize = true;
    private static int squareX = 50;
    private static int squareY = 50;
    private static int squareSize = 200;
    private static int ringX = 50;
    private static int ringY = 50;
    private static int ringSize = 100;
    private static int frameWidth = 500;
    private static int frameHeight = 500;
    private int jPanelHeight;
    private int jPanelWidth;
    private int i = 0;

    public mull() {
        timer = new Timer(500, this);
        timer.setInitialDelay(500);
        timer.start();
    }
    int xRing;
    int yRing;
    int xSquare;
    int ySquare;
    public static boolean allowedToDrawRing = true;
    public static boolean allowedToDrawSquare = true;
    public static boolean isOverlapedOnX = true;
    public static boolean isOverlapedOnY = true;
    public static boolean isOverlaped = true;
    @Override
    public void actionPerformed(ActionEvent e) {

        if (allowedToDrawRing || allowedToDrawSquare) {

            xRing = (int) ((getWidth() - ringSize) * (Math.random()));
            yRing = (int) ((getHeight() - ringSize) * (Math.random()));

            xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
            ySquare = (int) ((getHeight() - squareSize) * (Math.random()));

//          isOverlaped = (xSquare + squareSize * 2) < (xRing)
//                  || (xSquare) > (xRing + ringSize * 2)
//                  || (ySquare + squareSize * 2) < (yRing)
//                  || (ySquare) > (yRing + ringSize * 2);
//

            while (xSquare >= xRing && xSquare <= (2.0 * ringSize + xRing)
                    && ySquare >= yRing && ySquare <= (yRing + 2.0 * ringSize)) {
                xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
            }
            setParamsRing(xRing, yRing, ringSize);
            setParamsSquare(xSquare, ySquare, squareSize);

        }
        repaint();
    }

    public void setParamsRing(int xpos, int ypos, int size) {
        mull.ringX = xpos;
        mull.ringY = ypos;
        mull.ringSize = size;
    }
    public void setParamsSquare(int xpos, int ypos, int size) {
        mull.squareX = xpos;
        mull.squareY = ypos;
        mull.squareSize = size;
    }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (allowedToDrawRing) {
            g.setColor(Color.BLACK);
            g.drawOval(ringX, ringY, ringSize, ringSize);
            g.setColor(Color.BLUE);
            g.fillOval(ringX, ringY, ringSize, ringSize);

        }

        if (allowedToDrawSquare) {
            g.setColor(Color.BLACK);
            g.drawRect(squareX, squareY, ringSize, ringSize);
            g.setColor(Color.RED);
            g.fillRect(squareX, squareY, ringSize, ringSize);
        }

    }

    public static void main(String[] args) throws InterruptedException {

        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new JPanel();

        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {

                if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
                        && (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {

                    allowedToDrawRing = false;
                }
                if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
                        && (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {

                    allowedToDrawSquare = false;
                }

            }
        });

        f.add(p);
        f.add(new mull());
        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

OK, There is the solution:

@Override
    public void actionPerformed(ActionEvent e) {

        if (allowedToDrawRing || allowedToDrawSquare) {

            xRing = (int) ((getWidth() - ringSize) * (Math.random()));
            yRing = (int) ((getHeight() - ringSize) * (Math.random()));

            xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
            ySquare = (int) ((getHeight() - squareSize) * (Math.random()));


            while( !(
                    (xSquare + squareSize) < (xRing)
                    || (xSquare) > (xRing + ringSize )
                    || (ySquare + squareSize) < (yRing)
                    || (ySquare) > (yRing + ringSize)
                    )
                    ){
                xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                ySquare = (int) ((getHeight() - squareSize) * (Math.random()));

            }
            setParamsRing(xRing, yRing, ringSize);
            setParamsSquare(xSquare, ySquare, squareSize);

        }
        repaint();
    }

Last question: why my application becomes frozen after some iterations of timer if timer was set to low values ??? (in this example 50):

public mull() {
        timer = new Timer(50, this);
        timer.setInitialDelay(500);
        timer.start();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:30:06+00:00Added an answer on May 27, 2026 at 6:30 am

    You’ll need to change

    while ((xSquare + squareSize) >= xRing && xSquare <= (ringSize + xRing)  && (ySquare + squareSize) >= yRing && ySquare <= (ringSize + yRing)) {
       xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
       ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best approach to define additional data for typedef enums in C?
What is the best approach to build a small (but scalable) application that works
Any guidance on how best to define CSS styles in a web page that's
What is the best approach to synchronizing a DataSet with data in a database?
What would be the best approach to install a keyboard hook on Linux (X-windows)
What is the best approach to calculating the largest prime factor of a number?
What is the best approach in stripping leading and trailing spaces in C?
I wonder what the best approach is for a situation where I want to
What is the best approach to releasing a new version of a hosted web
What is the best approach to implementing authorisation/authentication for a Windows Forms app talking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.