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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:51:41+00:00 2026-05-26T09:51:41+00:00

I’ve started learning basic Java and wanted to rewrite a Game Map Generator that

  • 0

I’ve started learning basic Java and wanted to rewrite a Game Map Generator that I’ve once wrote in PHP. I’ve got part of it working fine (This is just the start), but whenever I want to display 2 things (using .add()), it will only display one of them. Heres my code;

public static void main(String[] args) {
    JFrame m1 = new JFrame();
    Container con = m1.getContentPane();
    Color c = new Color(16, 174, 0);
    con.setBackground(c);
    m1.setSize(mapWidth, mapHeight);
    m1.setTitle("ThomasMosey's Map Generator"); // Window Title
    m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    m1.add(new User());
    m1.add(new Grid());
    m1.setVisible(true);
}

It’s only part of the code, but I was wondering if it’s anything I’ve done wrong with .add there.

Thank you in advance.


This is the full code;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Map extends JFrame {
    public static int mapWidth = 576; // The Map's Width
    public static int mapHeight = 598; // The Map's Height
    public static int userX = 1;
    public static int userY = 1;
    private static final long serialVersionUID = 1L;
    public static void main(String[] args) {
        JFrame m1 = new JFrame();
        Container con = m1.getContentPane();
        Color c = new Color(16, 174, 0);
        con.setBackground(c);
        m1.setSize(mapWidth, mapHeight);
        m1.setTitle("ThomasMosey's Map Generator"); // Window Title
        m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con.add(new User(), BorderLayout.NORTH);
        con.add(new Grid(), BorderLayout.CENTER);
        m1.setVisible(true);
    }
}
// The Grid system
class Grid extends JComponent {
    private static final long serialVersionUID = 1L;
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Color gridColor = new Color(0, 84, 12);
        g2.setColor(gridColor);
        int i;
        int i2;
        for(i = 50; i <= Map.mapWidth; i += 51) {
            g2.drawLine(0, i, Map.mapWidth, i);
        }
        for(i2 = 50; i2 <= Map.mapHeight; i2 += 51) {
            g2.drawLine(i2, 0, i2, Map.mapHeight);
        }
    }
}
// Drawing the Grid but lower down to give a significant difference to check whether or not it's actually drawing on the JFrame
class User extends JComponent {
    private static final long serialVersionUID = 1L;
    public void paint(Graphics g2) {
        Graphics2D g22 = (Graphics2D) g2;
        g22.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Color gridColor = new Color(0, 84, 12);
        g22.setColor(gridColor);
        int i22;
        for(i22 = 50; i22 <= 6000; i22 += 53) {
            g22.drawLine(0, i22, Map.mapWidth, i22+1);
        }
    }
}
  • 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-26T09:51:41+00:00Added an answer on May 26, 2026 at 9:51 am
    1. The default layout for a content pane is BorderLayout.
    2. Add a component to a border layout with no constraint and it ends up in the CENTER.
    3. Only one component can appear in each part of a border layout.

    So try this instead..

    m1.add(new User(), BorderLayout.NORTH);
    m1.add(new Grid(), BorderLayout.CENTER);
    

    Part of the problem is that custom components have a default preferred size of 0x0. Try this variant.

    import java.awt.*;
    import javax.swing.*;
    
    public class Map extends JFrame {
        public static int mapWidth = 576; // The Map's Width
        public static int mapHeight = 598; // The Map's Height
        public static int userX = 1;
        public static int userY = 1;
        private static final long serialVersionUID = 1L;
        public static void main(String[] args) {
            JFrame m1 = new JFrame();
            Container con = m1.getContentPane();
            Color c = new Color(16, 174, 0);
            con.setBackground(c);
            // bad form - pack() instead
            //m1.setSize(mapWidth, mapHeight);
            m1.setTitle("ThomasMosey's Map Generator"); // Window Title
            m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            User user = new User();
            user.setPreferredSize(new Dimension(300,300));
            con.add(user, BorderLayout.NORTH);
            Grid grid = new Grid();
            grid.setPreferredSize(new Dimension(600,600));
            con.add(grid, BorderLayout.CENTER);
            m1.pack();
            m1.setVisible(true);
        }
    }
    // The Grid system
    class Grid extends JComponent {
        private static final long serialVersionUID = 1L;
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Color gridColor = new Color(0, 84, 12);
            g2.setColor(gridColor);
            int i;
            int i2;
            for(i = 50; i <= Map.mapWidth; i += 51) {
                g2.drawLine(0, i, Map.mapWidth, i);
            }
            for(i2 = 50; i2 <= Map.mapHeight; i2 += 51) {
                g2.drawLine(i2, 0, i2, Map.mapHeight);
            }
        }
    }
    // Drawing the Grid but lower down to give a significant difference to check whether or not it's actually drawing on the JFrame
    class User extends JComponent {
        private static final long serialVersionUID = 1L;
        public void paintComponent(Graphics g2) {
            Graphics2D g22 = (Graphics2D) g2;
            g22.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Color gridColor = new Color(0, 84, 12);
            g22.setColor(gridColor);
            int i22;
            for(i22 = 50; i22 <= 6000; i22 += 53) {
                g22.drawLine(0, i22, Map.mapWidth, i22+1);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.