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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:15:50+00:00 2026-05-26T19:15:50+00:00

I am having a problem with my java code. I’m trying to make it

  • 0

I am having a problem with my java code. I’m trying to make it so the top left quadrant produces a set number of lines input by a user through JOption Pane which are in random colors and in random positions. The programs builds successfully but it does not produce the number of lines the user input, nor does it set a random color (This it at the very bottom of my code). Can someone please explain how to fix this problem? Thanks very much.
Edit: fixed the curve braces but still will not work.
Edit: Everything works now except the random colors

import javax.swing.*;   //for JFrame
import java.awt.*;      //for Graphics and Container
import java.util.Random;
import javax.swing.JOptionPane;

// other import statements here


public class RandomGraphics
{
    // constants are used to draw the grid, and for you to put shapes in the grid
    public static final int MIDX = 400;
    public static final int MIDY = 300;
    public static final int MAXX = 799;
    public static final int MAXY = 599;
    public static final int COLOR = (int) (Math.random() * 256);

    // make another constant for the color value that will
    // be used to generate a random color

    public static void main( String[] args )throws InterruptedException
    {
        //*** This next section sets up the graphics window.
        //*** You are not required to understand it
        Container contentPane;
        Graphics g;
        JFrame win = new JFrame("Random Graphics");
        win.setSize(825,650);
        win.setLocation(0,0);
        win.setVisible(true);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = win.getContentPane();
        contentPane.setBackground(Color.white);
        g = contentPane.getGraphics();
        Thread.sleep(50);
        //*** done setting up graphics window


        // Draws Grid - DO NOT CHANGE
        // After you use JOptionPane to get the number of lines, you can move this
        // section of code to just after that, so the lines will not disappear
        g.drawRect(0,0,MAXX+1,MAXY+1);
        g.drawLine(0,MIDY,MAXX,MIDY); // horizontal line
        g.drawLine(MIDX,0,MIDX,MAXY); // vertical line


        // Create Random object
        Random r = new Random();



        // Top left quadrant:


        // Use a JOptionPane to ask the user to enter the number of lines 1 to 100.
        int count = 0;
        do
        {

            String morelines = JOptionPane.showInputDialog("Enter a number of lines between 1 to 100");
            count = Integer.parseInt(morelines);
        }
        while(count > 100 || count < 1);


        for(int i = 1; i >= count; i++)
{
                g.setColor(new Color (r.nextInt(COLOR), r.nextInt(COLOR), r.nextInt(COLOR)));
                g.drawLine(r.nextInt(MIDX), r.nextInt(MIDY), r.nextInt(MIDX), r.nextInt(MIDY));
}
  • 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-26T19:15:51+00:00Added an answer on May 26, 2026 at 7:15 pm
    g = contentPane.getGraphics();
    

    Graphics objects are not persistent, the programmer needs to draw the GUI to them when asked to do so. For tips, see the Performing Custom Painting Lesson of the tutorial.


    Beside the ‘always include curly braces around loops advice’, note..

    for(int i = 1; i >= count; i++)
    

    Should be..

    for(int i = 1; i <= count; i++)
    

    But don’t ignore the advice about custom painting. The app. will not render reliably until that is fixed.

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

Sidebar

Related Questions

I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm having an inexplicable problem with the Java Calendar class when I'm trying to
I am having a problem in this code. what i am trying to do
I am having a problem with the following bit of code. I am trying
I'm having a strange problem when debugging some java code with eclipse (Version: Helios
I´m having a stupid problem with java.util.Date . I have this line of code,
am having problem in Saving some data in my Java code. I have like
Hi I'm having problem with initialization in java, following code give me compile error
I'm having a problem with java sessions. I'm developing a simple web app, where
I'm having a small problem in Java. I have an interface called Modifiable. Objects

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.