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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:01:49+00:00 2026-06-17T23:01:49+00:00

I’m using a scroll pane, with a JPanel inside that draws a grid of

  • 0

I’m using a scroll pane, with a JPanel inside that draws a grid of squares that are objects from a [][] array.

IF the array is [83][81] of size 18^2 rectangles it looks like: https://i.stack.imgur.com/MEBrt.png
(Notice the white border at the edge of the grid)

However, same rectangles at [84][82]: https://i.stack.imgur.com/36WGm.png (The last rows get sliced).

Now, I’ve:

  • Increased the preferred and Max sizes of both the scroll pane and the Jpanel to well above what is needed.
  • Checked that the JPanel is the viewport of the scrollable object.

And it hasn’t changed anything.

My Jpanel Class is:

package BlastRadius;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

/**
 * Canvas Class Stuart Bradley 25-1-2013 Contains the paint component
 */
public class BlastRadiusCanvas extends JPanel {

    GridOfNodes grid = new GridOfNodes();

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh = g2d.getRenderingHints();
        rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(rh);

        //Draw space
        int pixelsAcross = 32;
        int pixelsDown = 0;
        int size = 18;
        for (int i = 0; i < grid.getRows(); i++) {
            for (int j = 0; j < grid.getColumns(); j++) {
                g2d.setColor(grid.getNodeGrid()[i][j].getColour());
                g2d.fillRect(pixelsAcross, pixelsDown, size, size);
                g2d.setColor(new Color(0, 0, 0));
                g2d.drawRect(pixelsAcross, pixelsDown, size, size);
                //Better Ovals maybe needed, try Ellipise2D class

                if (grid.getNodeGrid()[i][j].getHasOval() == true) {
                    g2d.setColor(new Color(255, 255, 255));
                    g2d.fillOval((pixelsAcross + (size / 2) - 1), (pixelsDown + (size / 2) - 1), size / 4, size / 4);
                }
                pixelsAcross += 18;
            }
            pixelsDown += 18;
            pixelsAcross = 32;
            //Draws gene string for first object in each row
            g2d.setColor(new Color(0, 0, 0));
            g2d.drawString(grid.getNodeGrid()[i][0].getGeneString(), 5, pixelsDown - 5);
        }
    }
}

Scroll pane and related, can post the entire GUI class if needed:

jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane1.setPreferredSize(new java.awt.Dimension(10000, 10000));
jScrollPane1.setViewportView(drawingJPanel);

//Grouping 

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

While I’ve done GUI’s before, both via RAD environments and by hand, scrollable interfaces are somewhat new to me!

  • 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-17T23:01:51+00:00Added an answer on June 17, 2026 at 11:01 pm

    The BlastRadiusCanvas needs to be providing information back to the scroll pane about how big it wants to be.

    enter image description here

    Based on my interpretation of what it is your trying to do, try adding the following to your BlastRadiusCanvas

    @Override
    public Dimension getPreferredSize() {
        return new Dimension((grid.getColumns() * 18) + 64, (grid.getRows() * 18) + 1);
    }
    

    Now really, you shouldn’t rely on “magic” numbers, it would be better to use something like…

    protected static final int GRID_SIZE = 18;
    protected static final int H_GAP = 32;
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension((grid.getColumns() * GRID_SIZE) + (H_GAP * 2), (grid.getRows() * GRID_SIZE) + 1);
    }
    

    This would make your paintComponent method look more like…

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints rh = g2d.getRenderingHints();
        rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(rh);
    
        //Draw space
        int pixelsAcross = H_GAP;
        int pixelsDown = 0;
        int size = GRID_SIZE;
        for (int i = 0; i < grid.getRows(); i++) {
            for (int j = 0; j < grid.getColumns(); j++) {
                g2d.setColor(grid.getNodeGrid()[i][j].getColour());
                g2d.fillRect(pixelsAcross, pixelsDown, size, size);
                g2d.setColor(new Color(0, 0, 0));
                g2d.drawRect(pixelsAcross, pixelsDown, size, size);
                //Better Ovals maybe needed, try Ellipise2D class
    
                if (grid.getNodeGrid()[i][j].getHasOval() == true) {
                    g2d.setColor(new Color(255, 255, 255));
                    g2d.fillOval((pixelsAcross + (size / 2) - 1), (pixelsDown + (size / 2) - 1), size / 4, size / 4);
                }
                pixelsAcross += size;
            }
            pixelsDown += size;
            pixelsAcross = H_GAP;
            //Draws gene string for first object in each row
            g2d.setColor(new Color(0, 0, 0));
            g2d.drawString(grid.getNodeGrid()[i][0].getGeneString(), 5, pixelsDown - 5);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am using jsonparser to parse data and images obtained from json response. When
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.