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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:50:19+00:00 2026-06-18T16:50:19+00:00

Recently I’ve been working on a program that paints an area with empty, colored

  • 0

Recently I’ve been working on a program that paints an area with empty, colored squares. Their locations on the screen are based off of the values 1 and 2 in a text file. 1s are supposed to make red boxes, and 2s are supposed to make green boxes. However, when I run the program, only red boxes are painted. I did some testing and found out that the repaint method is only being called twice(once sometimes for some reason), even though there are close to 300 values in the file, and repaint() should be called once for every value. Here is my code:

public class MAP extends JFrame {

    public static void main(String[] args) throws IOException {
        MAP map = new MAP();
    }

    Shape shape;
    int x = -32;
    int y = 0;
    ArrayList<Shape> shapes = new ArrayList<Shape>();
    Graphics2D g2;
    Color coulor = null;

    private class PaintSurface extends JComponent {

        public PaintSurface() {
        }

        public void paint(Graphics g) {
            g2 = (Graphics2D) g;
            g2.setColor(coulor);
            for (Shape s : shapes) {
                g2.draw(s);
            }

        }
    }

    public MAP() throws FileNotFoundException, IOException {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        frame.setTitle("Grid Maker");
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.add(new PaintSurface(), BorderLayout.CENTER);
        frame.setVisible(true);

        readNextLine();
    }

    private void readNextLine() throws IOException {
        File file = new File("map.txt");
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = in.readLine();

        while (line != null) {
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (c == '1') {
                    coulor = Color.RED;
                    x += 32;
                    int smallX = x / 32;
                    int smallY = y / 32;
                    shape = new Rectangle2D.Float(x, y, 32, 32);
                    shapes.add(shape);
                    repaint();
                } else if (c == '2') {
                    coulor = Color.GREEN;
                    x += 32;
                    int smallX = x / 32;
                    int smallY = y / 32;
                    shape = new Rectangle2D.Float(x, y, 32, 32);
                    shapes.add(shape);
                    repaint();

                }
            }

            line = in.readLine();
            x = -32;
            y += 32;
        }
    }
}

Why isn’t this code working properly?

  • 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-18T16:50:21+00:00Added an answer on June 18, 2026 at 4:50 pm

    Just to add to other answers, here is a piece of code (based on yours) which looks already a lot better (yet there are still some issues, but you are not there yet):

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.geom.Rectangle2D;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.StringReader;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class MAP extends JFrame {
    
        public static void main(String[] args) throws IOException {
            MAP map = new MAP();
        }
    
        public static class ColoredShape {
            private Shape shape;
            private Color color;
    
            public ColoredShape(Shape shape, Color color) {
                super();
                this.shape = shape;
                this.color = color;
            }
    
            public Shape getShape() {
                return shape;
            }
    
            public Color getColor() {
                return color;
            }
        }
    
        int x = -32;
        int y = 0;
        List<ColoredShape> shapes = new ArrayList<ColoredShape>();
        Graphics2D g2;
    
        private class PaintSurface extends JComponent {
    
            public PaintSurface() {
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g2 = (Graphics2D) g;
                for (ColoredShape s : shapes) {
                    g2.setColor(s.getColor());
                    g2.draw(s.getShape());
                }
    
            }
        }
    
        public MAP() throws FileNotFoundException, IOException {
            JFrame frame = new JFrame();
            frame.setTitle("Grid Maker");
            frame.setSize(400, 400);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.add(new PaintSurface(), BorderLayout.CENTER);
            frame.setVisible(true);
    
            readNextLine();
        }
    
        private void readNextLine() throws IOException {
            BufferedReader in = new BufferedReader(new StringReader("11121\n1221\n2212\n221121\n111221\n11221\n222\n2222\n"));
            String line = in.readLine();
    
            while (line != null) {
                for (int i = 0; i < line.length(); i++) {
                    char c = line.charAt(i);
                    Color color = null;
                    if (c == '1') {
                        color = Color.RED;
                    } else if (c == '2') {
                        color = Color.GREEN;
                    }
                    if (color != null) {
                        shapes.add(new ColoredShape(new Rectangle2D.Float(x, y, 32, 32), color));
                        x += 32;
                        repaint();
                    }
                }
    
                line = in.readLine();
                x = -32;
                y += 32;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I am working on a Mobile Device Management application in that i implemented
Recently I have been working with a SQL Server database and I was trying
Recently, I'm working on a Python-based project and facing a issue with dictionaries. In
Recently, I was writing a class in which I discovered that I could reduce
recently, while working on a db2 -> oracle migration project, we came across this
Recently, I was asked to compile some of my Android projects, based on the
Recently I started to experience problems with my project's cache system based on Couchbase.
Recently I started building my own big Windows 8 Store App. Working on UI
Recently I've used some features that require modern OpenGL context (specifically ARB_debug_output requires to
Recently wrote some JavaScript code, but there has been a memory leak in IE7.Are

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.