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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:31:24+00:00 2026-06-16T03:31:24+00:00

can someone take a look at my code below and tell me why, when

  • 0

can someone take a look at my code below and tell me why, when I change the following two statements, I do not see a change on the rectangle that is painted. So if I change:

g.setColor(Color.black); 
g.fillRect(l, w, 100, 100);

The program still prints a black rectangle with the same dimensions and in the same position that I first started with even though I change color to yellow or try to change the dimensions or location. I am BlueJ. The following is my full code:

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

public class SwingPaintDemo2 extends JComponent {

public static boolean isWall = true;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI(); 
        }
    });
}


private static void createAndShowGUI() {
    //System.out.println("Created GUI on EDT? "+
    //SwingUtilities.isEventDispatchThread());
    JFrame f = new JFrame("Swing Paint Demo");
    JPanel MyPanel = new JPanel();
     MyPanel.setBorder(BorderFactory.createEmptyBorder(1000, 1000, 1000, 1000));
     MyPanel.setPreferredSize(new Dimension(250, 200));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel());
    f.pack();
    f.setVisible(true);

}


public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    int l = 10;
    int w = 10;

  g.setColor(Color.black); 
  g.fillRect(l, w, 100, 100);

        }

}

Any advice would be appreciated.

  • 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-16T03:31:27+00:00Added an answer on June 16, 2026 at 3:31 am

    Your SSCCE doesnt compile where is MyPanel class or did you mean new SwingPaintDemo2()?

    On the assumption you meant new SwingPaintDemo2():

    The code does work just fine but the JFrame is sized very small:

    enter image description here

    because you dont give it any size and none of its components have a size as they do not have any components added to them, thus we must make the JComponent return a correct size so when we call pack() our JFrame is sized correctly

    Solution

    override getPreferredSize() of JComponent to return a width and height which fits all drawings.

    Some suggestions though:

    • Dont extend JComponent rather extend JPanel

    Here is an example (your code with above fixes implemented):

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class SwingPaintDemo2 extends JPanel {
    
        public static boolean isWall = true;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            //System.out.println("Created GUI on EDT? "+
            //SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Swing Paint Demo");
            JPanel MyPanel = new JPanel();
            MyPanel.setBorder(BorderFactory.createEmptyBorder(1000, 1000, 1000, 1000));
            MyPanel.setPreferredSize(new Dimension(250, 200));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new SwingPaintDemo2());
            f.pack();
            f.setVisible(true);
    
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int l = 10;
            int w = 10;
    
            g.setColor(Color.black);
            g.fillRect(l, w, 100, 100);
    
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(150, 150);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you take a look at the code below you can see that I
Can someone take a look at this part of my code and tell me
Can someone please take a look at these, it just is not happing for
can someone take a look at this please http://jsfiddle.net/bloodygeese/ecscY/56/ it looks like I have
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Could someone take a look at this code and find out what's wrong with
Can someone take a look at this menu http://www.abmoldremediationnj.com/3 and let me know how
Can someone smarter than me take a look at this. I'm trying to implement
Please take a look at the code snippet below. I want the 2nd MongoDB

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.