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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:58:53+00:00 2026-05-23T12:58:53+00:00

For a homework assignment, I am trying to paint a box with paintComponent using

  • 0

For a homework assignment, I am trying to paint a box with paintComponent using user inputed variables. I have been able to build what I am need to do using fixed numbers. I have been working on this all day and have not been able to find a way to implement variables. Here is a stripped down version of what I am working on:

import javax.swing.*;
import java.awt.*;

public class Problem3 extends JFrame{

  public static void main(String[] args) {  
    int xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));

    JFrame gd = new JFrame();
    gd.setLocationRelativeTo(null);
    gd.setSize(300, 300);
    gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gd.setVisible(true);

    gd.add(new NewPanel());   
  }

  public int getX(){
    return xCoord;
  }
}

class NewPanel extends JPanel {

  int xCoord = getX();

  protected void paintComponent(Graphics g){
    super.paintComponent(g);

    g.drawRect(10,10,xCoord,50);
  }
}

EDIT

Since posting here I have tried using a setter method inside the NewPanel class

public void setX() {
    xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));
}

Running this lead to a strange error I have never seen before: I get a StackOverFlow error and then the dialog box pops up multiple/hundreds of times and won’t stop popping up.

EDIT 3 Using Hovercraft Full Of Eels solution, I got it to work! Thank you Full Of Eels for your time, help, and patience.

public class Problem3 extends JFrame{
    public static void main(String[] args) {  

        JFrame gd = new JFrame();
        gd.setLocationRelativeTo(null);
        gd.setSize(300, 300);
        gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gd.setVisible(true);

        NewPanel panel = new NewPanel();

        panel.setX(50); 
        gd.add(panel);
    } 
}
class NewPanel extends JPanel {

    int xCoord;

    public void setX(int x){
        xCoord = x;
    }

    public int getX(){
        return xCoord;
    }

    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawRect(10,10,xCoord,50);
    }

}
  • 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-23T12:58:54+00:00Added an answer on May 23, 2026 at 12:58 pm

    Just like more basic Java classes you often use mutator and accessor methods, also known as setters and getters, to change the state of an object. In your situation you need to give the NewPanel class a setter method, public void setXCoor(int x) {...} and perhaps a similar one for a yCoord if need be so that outside classes (the GUI that holds the NewPanel object) can change the NewPanel’s values. Once the xCoord has been changed, you’ll want to call repaint() on the same NewPanel object so that its paintComponent method can be called by the JVM displaying the effects of the changed xCoord value.

    Edit 1
    Also a question: why does your Problem3 class extend JFrame? That seems unnecessary here.

    Edit 2
    Also, I would not do this code in a main method:

    int xCoord = Integer.parseInt(
            JOptionPane.showInputDialog("Enter an X cord."));
    
    JFrame gd = new JFrame();
    gd.setLocationRelativeTo(null);
    gd.setSize(300, 300);
    gd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gd.setVisible(true);
    
    gd.add(new NewPanel()); 
    

    But rather in a constructor of a full-fledged class.

    Also, I’d give that class a NewPanel class field, and I’d put the object held by that field into the JFrame allowing me to have a reference to the NewPanel object so I can call it’s methods elsewhere in the class.

    Edit 3
    Regarding this code:

    public void setX() {
        xCoord = Integer.parseInt(
                JOptionPane.showInputDialog("Enter an X cord."));
    }
    

    I wouldn’t do user input in the drawing class. Instead I’d the setter method a true setter method, similar to ones I’m sure you’ve made many times before and in this form:

    public void setX(int xCoord) {
       // set your field in here like you always do
    }
    

    Then do your user interaction, be it a JOptionPane or whatever in the main GUI or elsewhere in your program. Once the user gives his input, then call the setter method above on your NewPanel variable passing in the user’s input (as an int of course).

    Edit 4

    Regarding this code:

    public static void main(String[] args) {
        // ....
        gd.add(new NewPanel());   
    
        NewPanel.setX(50); 
    }
    

    You’re calling the setX method on the NewPanel class, not on a NewPanel object, which is why the compiler is appropriately complaining. This is why I suggested in one of my edits above to create a NewPanel variable in your GUI class, to create your GUI in the GUI class’s constructor, not in the main method (which should mainly call the GUI class’s constructor), and to use the same NewPanel variable to place into the GUI’s JFrame and to call the setX(…) method on.

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

Sidebar

Related Questions

I have a homework assignment where I need to take input from a file
This is a homework assignment. I just need a nudge. I'm trying to create
I know this sounds like a homework assignment, but it isn't. Lately I've been
As part of a homework assignment, I have to program a simple chess game
I'm working on a homework assignment to modify code given by my professor using
I have three questions regarding a homework assignment for C++. The goal was to
as a part of a homework assignment, I'm trying to read a single char
I am working on a homework assignment and I'm trying to figure out a
I have a homework assignment. I'm not looking for anyone to do the work
Have a homework assignment in which I'm supposed to create a vector of pointers

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.