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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:54:37+00:00 2026-05-21T09:54:37+00:00

This turned out to be more difficult than i thought it would. I’m just

  • 0

This turned out to be more difficult than i thought it would. I’m just doing some learning and poking around with the draw function. What I want is for a program to launch a window that contains, say, a rectangle. The size of that rectangle will be scaled on the size of the window (i.e. if the window is 1000px and the rectangle is set to scale at 90%, the rectangle will be 900px). I have the math figured out on how to center it and determine it’s size, but through the use of stubs, i have found that using the object.getWidth() and height, etc, is returning 0 every time, completely b0rking the math for sizing.

After thinking about it for a while, i’m assuming that it is because it is trying to obtain the width and height from an object that is still being constructed. To my own logic, as long as you do the math after the build parts of the constructor, it should be fine, but alas — i appear to be wrong.

I tried creating separate classes that creates a frame and one that makes the drawing. The drawing object accepts a JFrame as part of the constructor and attempts to draw on it. I instantiate each one separately, frame first, then drawing object (sending the frame object to it), but that doesnt seem to work either.

Any suggestions?

EDIT:

Per Andrew’s suggestion. Here is my panel object to be added to the window

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

public class MyDrawing extends JPanel
{

    public MyDrawing() throws HeadlessException
    {
        this.setVisible(true);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        //set scaling
        int usrScale = 90;
        int scaleRef = (100 - usrScale) / 2;
        int xStart = this.getWidth() * (scaleRef / 100);
        int yStart = this.getHeight() * (scaleRef / 100);
        int width = (usrScale / 100) * this.getWidth();
        int height = (usrScale / 100) * this.getHeight(); 

        //draw square outline
        g.setColor(Color.green);
        g.fillRect(xStart, yStart, width, height);
    }
}

And this is the window adding that jpanel object:

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

public class DrawThis extends JFrame
{
    MyDrawing drawing;

    public DrawThis() throws HeadlessException
    {
        drawing = new MyDrawing();
        this.add(drawing);
    }
}

And the Launcher

package scaling_test;

public class ScaleTest
{
    public static void main(String[] args)
    {
        DrawThis program = new DrawThis();
        program.setBounds(250, 250, 800, 600);
        program.setVisible(true);
    }
}

Updated panel (still doesnt work):

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

public class MyDrawing extends JPanel
{

    public MyDrawing() throws HeadlessException
    {
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);

        //set scaling
        int usrScale = 90;
        int scaleRef = (100 - usrScale) / 2;
        int xStart = this.getWidth() * scaleRef / 100;
        int yStart = this.getHeight() * scaleRef / 100;

        int width = usrScale * this.getWidth() / 100;
        int height = usrScale * this.getHeight() / 100;

        //draw square outline
        g.setColor(Color.green);
        g.fillRect(xStart, yStart, width, height);
    }
}
  • 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-21T09:54:38+00:00Added an answer on May 21, 2026 at 9:54 am

    Why would you use the getGraphics(…) method when the paintComponent(…) method is passed the Graphics object? Also, you should be invoking super.paintComponent(g) as the first statement in your method. I don’t know why you commented it out.

    Read the section from the Swing tutorial on Custom Painting for working examples.

    If you need more help then post your SSCCE so we don’t keep guessing what your actual code looks like.

    Edit:

    Did you add any display statements to see if the values used in the drawRect(…) method make any sense? Don’t assume your math forumulas are correct. For one thing you need to understand how integer calculations work:

    //        int width = (usrScale / 100) * this.getWidth();
    //        int height = (usrScale / 100) * this.getHeight();
            int width = usrScale * this.getWidth() / 100;
            int height = usrScale * this.getHeight() / 100;
    

    In the commented out code (usrScale / 100) = 0 since that value is converted to an integer before the multiplication is done.

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

Sidebar

Related Questions

No related questions found

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.