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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:12:57+00:00 2026-06-12T08:12:57+00:00

I am new to Java, and the whole working with arrays + using classes

  • 0

I am new to Java, and the whole working with arrays + using classes and their methods to populate and display the arrays. I have spent the last 2-3 hours, working on this and checking google for various answers and none have seemed to help, I am still stuck. I don’t really know how to use the methods from the rectangle1 class, when using arrays.

Main class:

public static void main(String[] args) {
    GameBoard frame = new GameBoard();
}

GameBoard Class

public class GameBoard extends JFrame {

    public GameBoard() {

        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 195, 215);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Rectangle1 board[][] = new Rectangle1[3][3];
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                board[row][col] = new Rectangle1(0, 0, 195, 215);
                if ((row + col) % 2 == 0) {
                    Graphics g = null;    
                    board[row][col].paint(g);
                    g.setColor(Color.yellow);
                    g.fillRect(0, 0, 195, 215);
                } else {
                    Graphics h = null;
                    board[row][col].paint(h);
                    h.setColor(Color.red);
                    h.fillRect(0, 0, 195, 215);
                }
                frame.add(board[row][col]);
            }    
        }  

    }
}

Rectangle1 class.

public class Rectangle1 extends JComponent  {

    /** post:   getX() == x  and  getY() == y
     *          and  getWidth() == w  and getHeight() == h
     *          and  getBackground() == Color.black
     */
    public Rectangle1(int x, int y, int w, int h)  {
        super();
        setBounds(x, y, w, h);
        setBackground(Color.black);
    }

    /** post:   this method draws a filled Rectangle
     *          and  the upper left corner is (getX(), getY()) 
     *          and  the rectangle's dimensions are getWidth() and getHeight()
     *          and  the rectangle's color is getBackground()
     */
    public void paint(Graphics g)  {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
        paintChildren(g);
   }

}
  • 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-12T08:12:58+00:00Added an answer on June 12, 2026 at 8:12 am

    Firstly, you are fighting the layout manager, calling something like setBounds(x, y, w, h) in you Rectangle class is going to overridden by the layout manager of the parent container (the frame in this case)

    Secondly, you are not responsible for painting, do this;

    board[row][col].paint(g);
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 195, 215);
    

    …is wrong. If it doesn’t end up with you programming crashing, then your lucky.

    While I try and figure out a solution, you might like to take some time and read through

    • Creating a GUI With JFC/Swing, and Laying Out Components Within a Container
    • 2D Graphics
    • Performing Custom Painting
    • Painting in AWT and Swing

    While on my rant…

    This;
    public void paint(Graphics g) {
    g.setColor( getBackground() );
    g.fillRect(0, 0, getWidth()-1, getHeight()-1);
    paintChildren(g);
    }

    is inappropriate. You MUST call super.paint(), there’s to much going on in the background for you to override this method without calling it’s super.

    In fact, you should avoid overriding paint where ever possible and user paintComponent instead.

    Also, coordinates in Swing are relative to the component. That is, the top/left corner of any component (within the context of the component) is always 0x0

    Here’s a working example

    public class TestBoard {
    
        public static void main(String[] args) {
            new TestBoard();
        }
    
        public TestBoard() {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                    new GameBoard();
    
                }
            });
    
        }
    
        public class GameBoard extends JFrame {
    
            public GameBoard() {
    
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
    
                Rectangle1 board[][] = new Rectangle1[3][3];
                for (int row = 0; row < board.length; row++) {
    
                    gbc.gridx = 0;
    
                    for (int col = 0; col < board[row].length; col++) {
                        board[row][col] = new Rectangle1(195, 215);
                        if ((row + col) % 2 == 0) {
                            board[row][col].setBackground(Color.YELLOW);
                        } else {
                            board[row][col].setBackground(Color.RED);
                        }
                        frame.add(board[row][col], gbc);
                        gbc.gridx++;
                    }
    
                    gbc.gridy++;
    
                }
    
                frame.pack();
                frame.setVisible(true);
    
            }
        }
    
        public class Rectangle1 extends JPanel {
    
            public Rectangle1(int w, int h) {
                setPreferredSize(new Dimension(w, h));
            }
    
        }
    }
    

    Because of the nature of your requirements, I’ve been forced to use GridBagLayout which is not the simplest of layout managers to use.

    JComponents are transparent by nature and you are required to fill them, better to use JPanel

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

Sidebar

Related Questions

I am pretty new to the whole Java and OSGi world and I have
new to Laravel (coming from Java spring), while using Route::get('/', function() { return Hello
I am working on a Server Client program in Java, using Sockets. I ask
I'm new to Java after working for a few years in PHP and I'm
I have a question regarding ResultSet objects in Java and recursion. I was working
I'm working on a project that uses both .net and java, using zeromq to
I'm new to Java, and I need some help working on this program. This
I'm new to Java, and while reading documentation so far I can't find any
When I start a new Java project in eclipse, the first popup screen allow
We are starting a new Java EE project and am looking for suggestions regarding

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.