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

  • SEARCH
  • Home
  • 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 6886209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:43:51+00:00 2026-05-27T05:43:51+00:00

I have code supposes to draw ring in random position within JPanel but for

  • 0

I have code supposes to draw ring in random position within JPanel but for some reason my ring sometimes gets out of this bounds, for instance, it appears in bottom half-cut.
As far as I understand, I have JPanel lying within JFrame, in order to be drawn within this boundaries, my ring should be drawn with this expression:

xRing = (int)((frameWidth -ringSize)* (Math.random()));
yRing = (int)((frameHeight-ringSize)*(Math.random()));
setParamsRing(xRing, yRing, ringSize); 

What is the problem?
Here is all code:

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 int squareX = 50;
        private int squareY = 50;
        private int squareSize = 200;  
        private int ringX = 50;
        private int ringY = 50;
        private int ringSize = 100;
        private static int frameWidth = 300;
        private static int frameHeight = 300;
        private int jPanelHeight;
        private int jPanelWidth;
        private int i = 0;

      public mull() {
            timer = new Timer(200, this);
            timer.setInitialDelay(200);
            timer.start();
          }
      int xRing;
      int yRing;
          @Override
          public void actionPerformed(ActionEvent e) {
               xRing = (int)((frameWidth -ringSize)* (Math.random()));
               yRing = (int)((frameHeight-ringSize)*(Math.random()));
              setParamsRing(xRing, yRing, ringSize);
              repaint();

          }
//  public void getJPanelSize(){
//      Rectangle r = p.getBounds();
//        jPanelHeight = r.height;
//        jPanelWidth = r.width;
//  }
    public void setParamsRing(int xpos, int ypos, int size){
        this.ringX = xpos;
        this.ringY = ypos;
        this.ringSize = size;
    }
    public void setParamsSquare(int xpos, int ypos, int size){
        this.squareX = xpos;
        this.squareY = ypos;
        this.squareSize = size; 
    }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);       
      g.setColor(Color.BLACK);
      g.drawOval(ringX, ringY, ringSize, ringSize);
      g.setColor(Color.BLUE );
      g.fillOval(ringX, ringY, ringSize, ringSize);

//        g.setColor(Color.BLACK);
//    g.drawRect(ringX, ringY, ringSize, ringSize);
//    g.setColor(Color.BLUE );
//    g.fillRect(ringX, ringY, 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();

        p.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
//              moveSquare(e.getX(),e.getY());
          }
      });

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

}


Thanks this approach worked!

I need to draw both ring and square but not to overlap each other.
What is the issue? My code seems not to work (although it seems to be correct):

@Override
public void actionPerformed(ActionEvent e) {
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>=xRing && xSquare<=ringSize) && (ySquare>=yRing && ySquare<=ringSize)  ){
xSquare = (int)((getWidth() -squareSize)* (Math.random()));
ySquare = (int)((getHeight()-squareSize)*(Math.random()));
               } 
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
repaint();

 }
  • 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-27T05:43:51+00:00Added an answer on May 27, 2026 at 5:43 am

    The problem in your code is, that the panel you are painting on is not of dimensions frameWidthxframeHeight but smaller. The frame’s bounds are set to these values but you have to consider the title bar and border which makes your actual panel a bit smaller than that.

    You can instead use the panel`s width and height:

    xRing = (int) ((getWidth() - ringSize) * Math.random());
    yRing = (int) ((getHeight() - ringSize) * Math.random());
    setParamsRing(xRing, yRing, ringSize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code that would, in theory, compile against any version of
Suppose I have some code: let listB = [ 1; 2; 3 ] Using
I have inherited some code from a guy whose favorite past time was to
I have a code which suppose to read an integer from a file. But
I have the code below that is supposed to draw lines from the top
I have use the source code in nashruddin.com to draw a rectangle on the
I have quite a big code here... But fortunately it is not needed to
Suppose I have code like this: template<class T, T initial_t> class Bar { //
Suppose I have code that maintains a parent/children structure. In such a structure I
Suppose to have a code like this: <div class=notSelected> <label>Name <input type=text name=name id=name

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.