0

I was going to make a game where a baby 1 passes through the road to meet another baby 2 by using a JButton (act). But when I use act button till the baby 1 touches baby 2 then baby 2 disappear. I want them to be displayed one over another.

package activity;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FacGrid extends JFrame implements ActionListener {

    JButton t[][] = new JButton[9][9];
    int baby1_posX = 6, baby1_posY = 2;
    int baby2_posX = 6, baby2_posY = 8;
    int ball_posX = 6, ball_posY = 3;
    int road_posX = 0, road_posY = 4;
    ImageIcon road = new ImageIcon("./image/road.jpg");
    ImageIcon baby1 = new ImageIcon("./image/baby 1.png");
    ImageIcon baby 2 = new ImageIcon("./image/baby2.png");
    ImageIcon ball = new ImageIcon("./image/ball.png");
    public static void main(String[] args) {
        Fac frame= new Fac();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900,700);
        frame.setTitle("GUI");
        frame.setResizable(false);
        frame.createGUI();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

    }
    void createGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(null);

        JPanel Panel =new JPanel();
        Panel.setBounds(30,20,350,300);

        Panel.setLayout(new GridLayout(9,9));
        Panel.setBackground(Color.WHITE);
        for(int i=0 ; i<9 ; i++) {
            for(int j=0 ; j<9 ; j++) {
                JButton tile = new JButton();
                t[i][j] = tile;
                Panel.add(t[i][j]);
                tile.setBorderPainted(false);
                tile.setBackground(Color.WHITE);

            }


        }

        t[baby1_posX][baby1_posY].setIcon(baby1);
        t[baby2_posX][baby2_posY].setIcon(baby2);
        t[ball_posX][ball_posY].setIcon(ball);

        for(int road_posX=0 ; road_posX<9 ; road_posX++ ) {
            t[road_posX][road_posY].setIcon(road);


        }



        window.add(Panel);


        JButton button = new JButton ("Act");
        button.setBounds(700, 20, 80, 30);
        button.addActionListener(this);
        window.add(button);


    }
    @Override
    public void actionPerformed(ActionEvent arg0) {

        t[ball_posX][ball_posY].setIcon(null);
        ball_posY=ball_posY+1;

        t[ball_posX][ball_posY].setIcon(ball);

    }
}
  • List item
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
John Bista
  • 11
  • 4
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, .. – Andrew Thompson Sep 03 '20 at 12:29
  • .. `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 03 '20 at 12:29
  • Maybe you can use the [Compound Icon](https://tips4java.wordpress.com/2009/03/29/compound-icon/). It allows you to combine multiple Icons into a single Icon. – camickr Sep 03 '20 at 14:21

0 Answers0