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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:28:26+00:00 2026-06-12T12:28:26+00:00

so i have a program where i display an image using paintComponent (arcs and

  • 0

so i have a program where i display an image using paintComponent (arcs and ovals, to be precise) beside a set of JButtons and a JTextArea, and i want the arcs/ovals to change size when/if the user changes the size of the frame. i have implemented the getWidth, getHeight stuff, but i cant seem to get it to work.

this is my code. if i dont setPreferredSize, then it doesnt work; the arcs/ovals get squished between the edge of the frame and the buttons. if i try to use getWidth() and getHeight() instead of 200 for size, it doesnt work either; the arcs/ovals dont show up at all. not sure what to do.

also, any tips on making my code less convoluted (eg. if only need one class) would be appreciated.

public class GUIDesign
{
public static void main(String[] args)
{
    GUIFrame frame = new GUIFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}
class GUIFrame extends JFrame
{
PaintPanel2 canvas = new PaintPanel2();
public GUIFrame()
{
    ...

    add(mainHolder, BorderLayout.CENTER);   //has the JButtons, JTextArea.
    add(canvas, BorderLayout.WEST);
    this.setTitle("this");
    this.pack();
    this.setLocationRelativeTo(null);
}
}
class PaintPanel2 extends JPanel
{
private static int SIZE = 200;

public PaintPanel2()
{
    setPreferredSize(new Dimension(SIZE, SIZE));
}
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

    int xCenter = getWidth()/2;
    int yCenter = getHeight()/2;

    int startOvalX = (int) (xCenter/5);
    int startOvalY = (int) (yCenter/5);
    int endOvalX = (int) (xCenter * 1.5);
    int endOvalY = (int) (yCenter * 1.5);
    g.setColor(Color.green);
    g.fillArc(startOvalX, startOvalY, endOvalX, endOvalY, 0, 180);
    g.setColor(Color.black);
    g.drawArc(startOvalX, startOvalY, endOvalX, endOvalY, 0, 180);
    g.setColor(Color.black);
    g.fillOval((int)(startOvalX/1.5) - 1, (int) (startOvalY * 2.5),(int) (endOvalX * 1.1) + 2,(int)(endOvalY/1.5));
    g.setColor(Color.green);
    g.fillOval((int)(startOvalX/1.5), (int) (startOvalY * 2.5) -1,(int) (endOvalX * 1.1),(int)(endOvalY/1.5));
}
}
  • 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-12T12:28:27+00:00Added an answer on June 12, 2026 at 12:28 pm

    When you are using a BorderLayout as the layout manager, the component that is added in the CENTER will be the one to resize when the main container resizes. So, if you change the code to have the canvas in the center, you will get the expected results:

    add(mainHolder, BorderLayout.WEST);   //has the JButtons, JTextArea.
    add(canvas, BorderLayout.CENTER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have c++ program which display images, I want to move from one image
I have a splash image that I want to display when the app is
In my program I am trying to display an image from a file using
I have a program which displays an image to the windows form, and places
We have installed RHQ monitoring system http://rhq-project.org/display/RHQ/Home . From that time our program started
I have a table in my database called Programs. I want to display a
I have done a program for a pop-up using Ajax ModalPopupExtender where I will
I have a scenario where I need to display charts(generated using jfreecharts) converted it
In my application I want to display some pictures (I need to have them
I just want to create a program which crops an image and sends the

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.