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 648749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:52:01+00:00 2026-05-13T21:52:01+00:00

This question is related to my previous question How to generate Cartesian Coordinate (x,y)

  • 0

This question is related to my previous question How to generate Cartesian Coordinate (x,y) from GridBaglayout?

I have successfully get the coordinate of each pictures, however when I checked the coordinate through (System.out.println) and the placement of the images on the screen, it seems to be wrong. e.g. if on the screen it was obvious that the x point of the first picture is on cell 2 which is on coordinate of 20, but the program shows x=1.

Here is part of the code:

public Grid (){

  setPreferredSize(new Dimension(600,600)); 
  ....
  setLayout(new GridBagLayout());
  GridBagConstraints gc = new GridBagConstraints();
  gc.weightx = 1d; 
  gc.weighty = 1d;
  gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right 
  gc.fill = GridBagConstraints.BOTH; 

  JLabel[][] label = new JLabel[ROWS][COLS];         
  Random rand = new Random();

  // fill the panel with labels
  for (int i=0;i<IMAGES;i++){
    ImageIcon icon = createImageIcon("myPics.jpg");
    int r, c;
    do{   
  //pick random cell which is empty
     r = (int)Math.floor(Math.random() * ROWS);
        c = (int)Math.floor(Math.random() * COLS); 
    } while (label[r][c]!=null);

    //randomly scale the images                
    int x = rand.nextInt(50)+30;
    int y = rand.nextInt(50)+30;                  
    Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH);
    icon.setImage(image);

    JLabel lbl = new JLabel(icon); // Instantiate GUI components 
    gc.gridx = r;
    gc.gridy = c;         
    add(lbl, gc); //add(component, constraintObj);         
    label[r][c] = lbl; 
}

I checked the coordinate through this code:

Component[] components = getComponents();     
for (Component component : components) {
   System.out.println(component.getBounds());
}
  • 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-13T21:52:02+00:00Added an answer on May 13, 2026 at 9:52 pm

    You can use SwingUtilities convertPointToScreen() and convertPointFromScreen() to convert between screen and component coordinates.

    Addendum: Here’s a simple example I used when trying to understand how components move and resize under the influence of a layout manager.

    public class MyPanel extends JPanel {
    
        public MyPanel() {
            super(new GridLayout(4, 4));
            for (int i = 0; i < 16; i++) {
                JPanel panel = new JPanel(new GridLayout());
                panel.add(new CenterLabel());
                this.add(panel);
            }
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MyPanel());
            f.pack();
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    
        private static class CenterLabel extends JLabel {
    
            public CenterLabel() {
                this.setHorizontalAlignment(JLabel.CENTER);
                this.setVerticalAlignment(JLabel.CENTER);
                this.setOpaque(true);
                this.setBackground(Color.lightGray);
                this.setBorder(BorderFactory.createLineBorder(Color.blue));
                this.setPreferredSize(new Dimension(160, 100));
                this.addComponentListener(new ComponentAdapter() {
    
                    @Override
                    public void componentResized(ComponentEvent e) {
                        int w = e.getComponent().getWidth();
                        int h = e.getComponent().getHeight();
                        CenterLabel.this.setText("[" + w/2 + "\u253C" + h/2 + "]");
                    }
                });
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to my previous question . The storyline: I have an
I have a question related to this previous question of mine . In an
Ok guys, this question is related to my previous one. If I have set
(This question is loosely related to my previous question ) Hello there. I have
This question is related to a previous post of mine Here . Basically, I
This question is related to a previous question of mine That's my current code
This question is related to the previous post. How to save file and read
This question is related to a previous post . Is there something comparable to
This question is related to my previous question - MySQL query to show records
This question is directly related to my previous question ASP.NET AJAX Is it possible

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.