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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:36:40+00:00 2026-06-05T19:36:40+00:00

Hi im making a Gui programme using a null layout and a setBounds() method

  • 0

Hi im making a Gui programme using a null layout and a setBounds() method for lay out . In the programme I want 25 strings printed out on the screen in random locations . I know that i could do this with a for loop however i have been trying this to no avail.

public void paint(Graphics g){
    super.paint(g);
    for(i=0;i<25;i++){
        g.drawString("string name",Math.random()*250,Math.random()*250);
        g.setColor(Color.RED);
    }
}

I have been trying this and it has not been working so my question is is there some better way to do this or am i making some sort of obvious mistake.

  • 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-06-05T19:36:42+00:00Added an answer on June 5, 2026 at 7:36 pm

    You are not using the Math.random() part correctly try this instead:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author David
     */
    public class JavaApplication142 extends JFrame {
    
        private int width = 300, height = 300;
    
        public JavaApplication142() {
            createAndShowUI();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JavaApplication142 jApp = new JavaApplication142();
                }
            });
        }
    
        private void createAndShowUI() {
            setTitle("Painting");
            setSize(width, height);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            addComponentsToContentPane(getContentPane());
            setVisible(true);
        }
    
        private void addComponentsToContentPane(Container contentPane) {
            Panel panel1 = new Panel();
            contentPane.add(panel1);
        }
    
        class Panel extends JPanel {
    
            private Random rand;
    
            public Panel() {
                rand = new Random();
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                for (int i = 0; i < 25; i++) {
                    g.drawString("string name", (int) rand.nextInt(width), (int) rand.nextInt(height));
                    g.setColor(Color.RED);
                }
            }
        }
    }
    

    This will help to get all the strings drawn within the region of the panel, although strings with varying length might go offscreen, just add some extra code to check the length of the string and set its co-ords appropriately

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Java Netbeans GUI Builder for making GUI. I want to give
I am making my first ever C# GUI. It is using a BackgroundWorker to
I am making a GUI using SpringLayout using the following code: private void createAndShowGUI()
Making sense out of an .MSI verbose trace. I created the .MSI using VisualStudio
I am making a GUI program using gtkmm. I would like to draw some
I'm making Python software using wx GUI library but was wondering how to run
I'm making a simple cross platform chat program. I'm using wXWidgets for the GUI
I`m making a GUI for my java program using Swing Components and Netbeans IDE.
I'm making a GUI using Java (who isn't?). I know the Swing Worker class
I'm making a GUI and I'm finding myself to be using a lot of

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.