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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:03:17+00:00 2026-06-14T08:03:17+00:00

I have the following simple code in a JFrame constructor super(name); setBounds(0,0,1100,750); setLayout(null); setVisible(true);

  • 0

I have the following simple code in a JFrame constructor

    super(name);
    setBounds(0,0,1100,750);
    setLayout(null);


    setVisible(true);

    g = this.getGraphics();
    int[] x =new int[]{65,  122,  77,  20, };
    int[] y =new int[]{226,  258, 341,  310};
    g.setColor(Color.RED);  
    g.drawPolygon (x, y, x.length);
    System.out.println(g);

I get the output on console as:

sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0]]

But no red polygon drawn on JFrame but just the blank JFrame.

Why ??

  • 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-14T08:03:18+00:00Added an answer on June 14, 2026 at 8:03 am
    • Dont overridepaint(..) in JFrame

    • Rather add custom JPanel with overridden paintComponent(Graphics g) to JFrame

    • Dont use Null/AbsoluteLayout use an appropriate LayoutManager

    • Dont call setBounds(..) on JFrame instance (not that its not allowed but cant see it being relevant in this application)

    • Dont forget to use EDT for creating and changing GUI components:

      javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  @Override
                 public void run() {
                      Test test = new Test();
                 }
      });
      

    you would then do something like this:

    public class Test {
    
        /**
         * Default constructor for Test.class
         */
        public Test() {
            initComponents();
        }
    
        public static void main(String[] args) {
    
            /**
             * Create GUI and components on Event-Dispatch-Thread
             */
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Test test = new Test();
                }
            });
        }
    
        /**
         * Initialize GUI and components (including ActionListeners etc)
         */
        private void initComponents() {
            JFrame jFrame = new JFrame();
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            jFrame.add(new MyPanel());
    
            //pack frame (size JFrame to match preferred sizes of added components and set visible
            jFrame.pack();
            jFrame.setVisible(true);
        }
    }
    
    class MyPanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int[] x = new int[]{65, 122, 77, 20};
            int[] y = new int[]{226, 258, 341, 310};
            g.setColor(Color.RED);
            g.drawPolygon(x, y, x.length);
        }
    
        //so our panel is the corerct size when pack() is called on Jframe
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
    

    which produces:

    enter image description here

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

Sidebar

Related Questions

I have following simple code: <%@ Page Language=C# AutoEventWireup=true CodeBehind=testForm.aspx.cs Inherits=Orbs.testForm %> <html> <body>
I have the following simple code: <Window x:Class=WpfApplication3.MainWindow x:Name=WindowInst …> <local:UserControl1/> </Window> <UserControl x:Class=WpfApplication3.UserControl1
I have the following simple code: var tabs = null; function addTab(id, title, data)
I have the following simple code that draws rectangle <Canvas Name=MainImageLayer > <Image >
I have a following simple code: def get(): return [lambda: i for i in
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple code abstract class A { public abstract void Test(Int32
I have the following very simple code snippet which is loaded from a separate
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have got the following very simple code: function init() { var articleTabs =

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.