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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:17:41+00:00 2026-05-29T18:17:41+00:00

First off, please accept my apologies if this question is basic, I mainly have

  • 0

First off, please accept my apologies if this question is basic, I mainly have knowledge of C# but am forced to use Java for this particular project!

I’m trying to implement a GUI to display an occupancy grid based on robot sensor data. The occupancy grid will be quite large, perhaps up to 1500×1500 grid squares representing real-world area of around 10cm2 per grid cell.

Each grid square will simply store an Enumerable status, for example:

  • Unknown
  • Unoccupied
  • Occupied
  • Robot

I would simply like to find the best way to render this as a grid, using different colour squares to depict different grid cell status’.

I have implemented a naive, basic algorithm to draw squares and grid lines, however it performs VERY badly on larger occupancy grids. Other code in the class redraws the window every 0.5s as new sensor data is collected, I suspect the reason for the very poor performance is the fact that i am rendering EVERY cell EVERY time. Is there an easy way i can selectively render these cells, should I wrap each cell in an observable class?

My current implementation:

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    int width = getSize().width;
    int height = getSize().height;

    int rowHeight = height / (rows);
    int colWidth = width / (columns);

    //Draw Squares
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < columns; col++) {
            switch (this.grid[row][col]) {
                case Unexplored:
                    g.setColor(Color.LIGHT_GRAY);
                    break;
                case Empty:
                    g.setColor(Color.WHITE);
                    break;
                case Occupied:
                    g.setColor(Color.BLACK);
                    break;
                case Robot:
                    g.setColor(Color.RED);
                    break;
            }

            g.drawRect(col * colWidth, height - ((row + 1) * rowHeight), colWidth,     rowHeight);
            g.fillRect(col * colWidth, height - ((row + 1) * rowHeight), colWidth,     rowHeight);
        }
    }

    int k;
    if (outline) {
        g.setColor(Color.black);
        for (k = 0; k < rows; k++) {
            g.drawLine(0, k * rowHeight, width, k * rowHeight);
        }

        for (k = 0; k < columns; k++) {
            g.drawLine(k * colWidth, 0, k * colWidth, height);
        }
    }

}


 private void setRefresh() {
    Action updateUI = new AbstractAction() {
        boolean shouldDraw = false;

        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    };

    new Timer(updateRate, updateUI).start();
}

Please help! Thanks in advance.

  • 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-05-29T18:17:53+00:00Added an answer on May 29, 2026 at 6:17 pm

    Creating rectangles is probably too slow. Instead, why don’t you create a bitmap image, each pixel being a cell of the grid, you can then scale it to whatever size you want.

    The following class takes a matrix of integers, and saves it to a bitmap file.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class BMP {
        private final static int BMP_CODE = 19778;
    
        byte [] bytes;
    
        public void saveBMP(String filename, int [][] rgbValues){
            try {
                FileOutputStream fos = new FileOutputStream(new File(filename));
    
                bytes = new byte[54 + 3*rgbValues.length*rgbValues[0].length + getPadding(rgbValues[0].length)*rgbValues.length];
    
                saveFileHeader();
                saveInfoHeader(rgbValues.length, rgbValues[0].length);
                saveRgbQuad();
                saveBitmapData(rgbValues);
    
                fos.write(bytes);
    
                fos.close();
    
            } catch (FileNotFoundException e) {
    
            } catch (IOException e) {
    
            }
    
        }
    
        private void saveFileHeader() {
            byte[]a=intToByteCouple(BMP_CODE);
            bytes[0]=a[1];
            bytes[1]=a[0];
    
            a=intToFourBytes(bytes.length);
            bytes[5]=a[0];
            bytes[4]=a[1];
            bytes[3]=a[2];
            bytes[2]=a[3];
    
            //data offset
            bytes[10]=54;
        }
    
        private void saveInfoHeader(int height, int width) {
            bytes[14]=40;
    
            byte[]a=intToFourBytes(width);
            bytes[22]=a[3];
            bytes[23]=a[2];
            bytes[24]=a[1];
            bytes[25]=a[0];
    
            a=intToFourBytes(height);
            bytes[18]=a[3];
            bytes[19]=a[2];
            bytes[20]=a[1];
            bytes[21]=a[0];
    
            bytes[26]=1;
    
            bytes[28]=24;
        }
    
        private void saveRgbQuad() {
    
        }
    
        private void saveBitmapData(int[][]rgbValues) {
            int i;
    
            for(i=0;i<rgbValues.length;i++){
                writeLine(i, rgbValues);
            }
    
        }
    
        private void writeLine(int row, int [][] rgbValues) {
            final int offset=54;
            final int rowLength=rgbValues[row].length;
            final int padding = getPadding(rgbValues[0].length);
            int i;
    
            for(i=0;i<rowLength;i++){
                int rgb=rgbValues[row][i];
                int temp=offset + 3*(i+rowLength*row) + row*padding;
    
                bytes[temp]    = (byte) (rgb>>16);
                bytes[temp +1] = (byte) (rgb>>8);
                bytes[temp +2] = (byte) rgb;
            }
            i--;
            int temp=offset + 3*(i+rowLength*row) + row*padding+3;
    
            for(int j=0;j<padding;j++)
                bytes[temp +j]=0;
    
        }
    
        private byte[] intToByteCouple(int x){
            byte [] array = new byte[2];
    
            array[1]=(byte)  x;
            array[0]=(byte) (x>>8);
    
            return array;
        }
    
        private byte[] intToFourBytes(int x){
            byte [] array = new byte[4];
    
            array[3]=(byte)  x;
            array[2]=(byte) (x>>8);
            array[1]=(byte) (x>>16);
            array[0]=(byte) (x>>24);
    
            return array;
        }
    
        private int getPadding(int rowLength){
    
            int padding = (3*rowLength)%4;
            if(padding!=0)
                padding=4-padding;
    
    
            return padding;
        }
    
    }
    

    With that class, you can simply do:

    new BMP().saveBMP(fieName, myOccupancyMatrix);
    

    Generating the matrix of integers (myOccupancyMatrix) is easy. A simple trick to avoid the Switch statement is assigning the color values to your Occupancy enum:

    public enum Occupancy {
            Unexplored(0x333333), Empty(0xFFFFFF), Occupied(0x000000), Robot(0xFF0000);
    }
    

    Once you save it do disk, the BMP can be shown in an applet and scaled easily:

    public class Form1 extends JApplet {
        public void paint(Graphics g) {
            Image i = ImageIO.read(new URL(getCodeBase(), "fileName.bmp"));
            g.drawImage(i,0,0,WIDTH,HEIGHT,Color.White,null);
        }
    }
    

    Hope this helps!

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

Sidebar

Related Questions

First off, please forgive the stupidness of this question but Im not from a
Ok... First off, I know this isn't a new question. But, for some reason
First off, I'm doing this for myself so please don't suggest use GMP /
First off, I'm new to MVC, so please excuse the question if it's basic.
first off I am a rookie so please forgive my basic question. I am
First off, this question is ripped out from this question. I did it because
This starts off as an Android question but I think becomes a linux/cygwin question.
First off, please do not tl;dr this. It just a lot of quickly-scannable code.
So first off, this is a homework assignment, so please don't write any code
First off, please bear with the question as I am just starting out with

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.