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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:59:51+00:00 2026-06-01T20:59:51+00:00

I have been having a some issues with Swing lately. I’m trying to make

  • 0

I have been having a some issues with Swing lately. I’m trying to make something like in the image below on the fly, to illustrate the data structures for algorithms.

notebook image
(source: ius.edu)

All that I was trying to do in the following class is draw out some rectangles with numbers on them. and translate them, however the last rectangle draws at 0,0. I’m stumped.

If you add the JPanel (commented out) after the loop then it draws as expected.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawingRect {

    public static void main(String[] args) {
        DrawingRect d = new DrawingRect();
    }

    public DrawingRect() {

        JFrame frame = new JFrame("Drawing a rect");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel swingPanel = new JPanel();
        swingPanel.setPreferredSize(new Dimension(500, 500));

        swingPanel.setVisible(true);

        swingPanel.setLayout(new BorderLayout(0, 0));

        int base = 15;
        for (int i = 1; i <= 25; i++) {
            Graphic re = new Graphic(i);

            //translating the graphic
            re.setBounds(base + 30 * i, base + 20 * i, 110, 110);


            swingPanel.add(re);
        }


        // if say I add a JPanel in here as the last element
        // then the boxes will draw correctly.
        //swingPanel.add(new JPanel());

        swingPanel.setPreferredSize(new Dimension(800, 600));

        frame.getContentPane().add(swingPanel);

        frame.pack();
        frame.setVisible(true);
    }

    public class Graphic extends JComponent {

        private static final long serialVersionUID = 1L;
        private static final int PREF_W = 100;
        private static final int PREF_H = 100;
        int id;

        public Graphic(int id) {
            this.id = id;
        }

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

            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.black);

            g2.setColor(Color.black);
            g2.drawRoundRect(0, 0, 30, 30, 20, 20);

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

            Font font = new Font("", Font.PLAIN, 13);
            g2.setFont(font);

            g2.drawString("" + id, 15, 20);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_H);
        }
    }
}   
  • 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-01T20:59:52+00:00Added an answer on June 1, 2026 at 8:59 pm

    The default layout of JPanel is FlowLayout, but you’re positioning components as if the layout were null. Moreover, you’re drawing content that a nested component could do automatically.

    Instead, add several JPanels containing JLabel‘s to a containing panel having GridLayout. Use empty panels as required. Override paintComponent() in the outer panel to draw connecting lines.

    Addendum: Anytime one is tempted to use Absolute Positioning, JInternalFrame may be an alternative. Related examples may be found here and here.

    Addendum: If the project grows beyond the prototype stage, also consider a Custom Layout Manager.

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

Sidebar

Related Questions

I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
I have been having some problems trying to get my PHP running. When I
I've been having some strange errors lately. I have a working install of Git,
I have been having some issues retrieving JSON data from a WCF service application
I have been having intermittent issues on some servers running Archlinux / php-fpm 5.3.9
I have been having some issues with creating a connection to amazon's MWS service.
I have been having some major issues with my Visual Studio 2008 Pro install.
I have been having some issues with django-haystack and need some help. I run
Ok, I have been searching thick and thin, and I am having some issues
I have some utility methods that use Microsoft.Web.Administration.ServerManager that I've been having some issues

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.