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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:22:03+00:00 2026-06-16T04:22:03+00:00

I am trying to make a basic map where pressing a button moves a

  • 0

I am trying to make a basic map where pressing a button moves a character.
I am using Netbeans and so far it’s going smoothly! Except for trying to remove a JLabel from a JPanel and then add a new JLabel to it.

Here is my full code:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class startMap extends JFrame {

public int locX = 1;
public int locY = 1;
ImageIcon tile = new ImageIcon("src/tile.png");
JLabel tileLabel = new JLabel(tile);
ImageIcon berry = new ImageIcon("src/berry.png");
JLabel berryLabel = new JLabel(berry);
ImageIcon blank = new ImageIcon("src/blank.png");
JLabel blankLabel = new JLabel(blank);
JLabel testL = new JLabel("LOL");
JPanel[][] map = new JPanel[7][7];

public startMap() {
    setBackground(Color.BLACK);
    setFocusable(true);
    keyHandler kh = new keyHandler();
    addKeyListener(kh);
    mapGUI();
}

public void mapGUI() {
    JPanel mainP = new JPanel();
    mainP.setBackground(Color.BLACK);
    mainP.setLayout(new FlowLayout());
    for (int x = 0; x < 7; x++) {
        for (int i = 0; i < 7; i++) {
            map[x][i] = new JPanel();
            System.out.println("1");
            blankLabel = new JLabel(blank);
            map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
            map[x][i].add(blankLabel);
        }
    }
    for (int x = 1; x < 6; x++) {
        for (int i = 1; i < 6; i++) {
            System.out.println("2");
            map[x][i].removeAll();
            revalidate();
            repaint();
            tileLabel = new JLabel(tile);
            map[x][i].add(tileLabel);
            revalidate();
            repaint();
        }
    }
    System.out.println("3");
    map[locX][locY].removeAll();
    revalidate();
    repaint();
    map[locX][locY].add(berryLabel);
    revalidate();
    repaint();
    for (int x = 0; x < 7; x++) {
        for (int i = 0; i < 7; i++) {
            mainP.add(map[x][i]);
        }
    }

    add(mainP);
}

@Override
public void paint(Graphics g) {
    super.paint(g);

}

private class keyHandler extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            locY+=1;
            map[locX][locY].removeAll();
            revalidate();
            repaint();
            map[locX][locY].add(berryLabel);
            revalidate();
            repaint();


        }

    }
}

}

Here is what changes the squares when the user clicks Right.

Here is the KeyAdapter code:

private class keyHandler extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            locY+=1;
            map[locX][locY].removeAll();
            revalidate();
            repaint();
            map[locX][locY].add(berryLabel);
            revalidate();
            repaint();



        }

    }}

NOTE: the system out is just a debugging method I use to check what’s being called when.

So when I run it looks like this

Move to the right and revalidate $ repaint:

enter image description here

Why does the box located in 1,1 go to being gray?

Help figuring out how to make the boxes stay with a white square instead of turning back to gray.

—————————-SSCCE———————————-

Use the full code above
and this is the main class:

import javax.swing.*;

public class TestGame extends JFrame {

public static void main(String[] args) {
    startMap sm = new startMap();
    sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sm.setVisible(true);
    sm.setSize(380,415);
    sm.setResizable(false);
}
}

Fixed Version:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class startMap extends JFrame {

public int locX = 1;
public int locY = 1;
ImageIcon tile = new ImageIcon("src/tile.png");
JLabel tileLabel = new JLabel(tile);
ImageIcon berry = new ImageIcon("src/berry.png");
JLabel berryLabel = new JLabel(berry);
ImageIcon blank = new ImageIcon("src/blank.png");
JLabel blankLabel = new JLabel(blank);
JLabel testL = new JLabel("LOL");
JPanel[][] map = new JPanel[7][7];

public startMap() {
    setBackground(Color.BLACK);
    setFocusable(true);
    keyHandler kh = new keyHandler();
    addKeyListener(kh);
    mapGUI();
}

public void mapGUI() {
    JPanel mainP = new JPanel();
    mainP.setBackground(Color.BLACK);
    mainP.setLayout(new FlowLayout());
    for (int x = 0; x < 7; x++) {
        for (int i = 0; i < 7; i++) {
            map[x][i] = new JPanel();
            System.out.println("1");
            blankLabel = new JLabel(blank);
            map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
            map[x][i].add(blankLabel);
        }
    }
    for (int x = 1; x < 6; x++) {
        for (int i = 1; i < 6; i++) {
            System.out.println("2");
            map[x][i].removeAll();
            tileLabel = new JLabel(tile);
            map[x][i].add(tileLabel);
            revalidate();
            repaint();
        }
    }
    System.out.println("3");
    map[locX][locY].removeAll();
    map[locX][locY].add(berryLabel);
    revalidate();
    repaint();
    for (int x = 0; x < 7; x++) {
        for (int i = 0; i < 7; i++) {
            mainP.add(map[x][i]);
        }
    }

    add(mainP);
}

@Override
public void paint(Graphics g) {
    super.paint(g);

}

private class keyHandler extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            berryLabel = new JLabel(berry); //THIS
            tileLabel = new JLabel(tile); //And THIS had to be RE initialized idk why.
            map[locX][locY].removeAll();
            map[locX][locY].add(tileLabel);

            locY += 1;
            map[locX][locY].removeAll();
            map[locX][locY].add(berryLabel);
            revalidate();
            repaint();


        }

    }
}

}

  • 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-16T04:22:05+00:00Added an answer on June 16, 2026 at 4:22 am

    Should be called after adding / removing

    revalidate();
    repaint();
    

    Don’t call repaint() from paint()

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

Sidebar

Related Questions

I'm trying to make basic functionality after pressing start button start counter , after
I've been trying to make my own basic .obj (Wavefront) renderer using the OpenGL
/* I am trying to make a basic calculator in JAVA using swings. However,
I am trying to make a basic quotation-sharing app using Webapp. Obviously it is
Im trying to make a basic while loop example im preperation for my next
I'm trying to make a basic hover on a ul element, which would display
I am trying to make a very basic game with Java and I am
I am trying to make a Chess multiplayer game in Visual Basic. Its a
I'm trying to make a very basic php ORM as for a school project.
I am trying to make a basic simple game in C++ where the user

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.