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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:27:22+00:00 2026-05-27T10:27:22+00:00

I am trying to create a simple game wherein a box(alien) is going on

  • 0

I am trying to create a simple game wherein a box(alien) is going on each row(left to right / right to left) then going downward and in a mouse click there will be a ball(fire) and when the box and ball meets, the player wins else the alien invades the planet.

I got the idea of the game from
[http://www.stanford.edu/class/cs106a/cgi-bin/classhandouts/23-ufo-game.pdf][1]
stanford lecture
[1]:

so i tried making like that using my code.

    package spaceInvader;

import javax.swing.JFrame;

public class Main {
public static void main(String args[]) throws InterruptedException{
    JFrame jf = new JFrame("YY");
    Space_GUI sg = new Space_GUI();
    jf.add(sg);
    jf.setSize(500,500);
    jf.setVisible(true);
    jf.setContentPane(sg);
    Thread.sleep(2000);
    sg.rc.move();

}
}




package spaceInvader;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Space_GUI extends JPanel{
    public RectangleComponent rc;

 public Space_GUI(){
    rc = new RectangleComponent();
    add(rc);    
}

}




package spaceInvader;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class RectangleComponent extends JComponent{

    private int _xCoord;
    private int _yCoord;
    private  static final int SIZE = 50;
    public RectangleComponent(){
        _xCoord = 10;
        _yCoord = 10;
        repaint();
    }

    public void move(){
        _xCoord = 20;
        _yCoord = 20;
        repaint();
    }
    public void paintComponent(Graphics g){
        _xCoord = getWidth()/2;
        _yCoord = getHeight()/2;
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawRect(_xCoord, _yCoord, SIZE, SIZE);
        g.fillRect(_xCoord, _yCoord, SIZE, SIZE);
    }
}

in my code i am trying to just show the box but then there is none showing. I tried adding up JButton and only that button shows , it doesnt show the box.

  • 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-27T10:27:23+00:00Added an answer on May 27, 2026 at 10:27 am

    OK for some reason I’m seeing the RectangleComponent code now, and I see problems:

    • RectangleComponent extends JComponent, and a JComponent’s preferredSize is [0, 0], so if you don’t do anything about this, nothing will of course show.
    • One solution is to give RectangleComponent a getPreferredSize method that tells the layout managers what size it should be.
    • Another possible solution is to use a different layout manager for the Space_GUI JPanel that holds the RectangleComponent object. For instance if you had it use BorderLayout and added RectangleComponent BorderLayout.CENTER, then the RectangleComponent would fill the Space_GUI JPanel.
    • Another problem is that you’re careful to set the _xCoord and _yCoord variables and to then change them in move(), but it is all for naught since you set them to something completely different in RectangleComponent’s paintComponent method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at trying to create a simple 'slider puzzle' game. You've seen the
My friend and I are trying to create a simple RPG style game, in
So I'm trying to create a simple multi-threaded game engine for the game I
I'm trying to create a simple game, but I can't find a certain memory
I'm trying to create a simple game in Pascal. It uses the console. The
I'm trying to create a simple game where the point is to collect as
So I am trying to create a real simple dice game...more like a dice
I'm trying to create simple 2D action game, something similar to well known Fancy
I'm trying to create a very simple version of the game Simon with the
I am messing around with some Javascript game development, trying to create a simple

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.