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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:38:43+00:00 2026-05-31T07:38:43+00:00

I am trying to get a box to move in a JFrame by repainting

  • 0

I am trying to get a box to move in a JFrame by repainting it, but for some reason i cannot figure out it will not work. I know its probably something really stupid but here are my classes: (also sorry for the formatting its a pain in the ass…) It gives me an error on the frame.add(square) line.

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class Runner extends JFrame{

final static int FRAME_WIDTH = 1000;
final static int FRAME_HEIGHT = 600;
final static int BOX_WIDTH = 50;
final static int BOX_HEIGHT = 50;

public static void main (String[] args){

    JFrame frame = new JFrame();
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("Animation");
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setVisible(true);

    Component square = new Component();
    Dimensions.setBoxDimensions(BOX_WIDTH, BOX_HEIGHT);

    frame.add(square);
    frame.setVisible(true);

    for (int i = 0; i < 100; i++){
        Dimensions.setPosition(i,i);
        square.repaint();
        frame.setVisible(true);
    }
}
}



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;

public class Component extends JFrame{

public void paintComponent (Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth,            Dimensions.boxHeight);
}
}





public class Dimensions {

public static int boxHeight = 50;
public static int boxWidth = 50;
public static int xPos = 0;
public static int yPos = 0;

public static void setBoxDimensions(int width, int height){
    boxHeight = height;
    boxWidth = width;
}
public static void setPosition(int x, int y){
    xPos = x;
    yPos = y;
}
}
  • 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-31T07:38:45+00:00Added an answer on May 31, 2026 at 7:38 am

    Since your component class is a JFrame you cannot add teh JFrame to a JFrame. You may want to add a JPanel to the JFrame and paint on the JPanel instead.

    Or you may be intending to extend java.awt.Component instead?

     import javax.swing.JPanel;
     public class MyComponent extends JPanel{
    
         public void paintComponent (Graphics g){
               Graphics2D g2 = (Graphics2D) g;
               g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth,                          Dimensions.boxHeight);
      }
      }
    

    And when you want a Component you can do this:

      JPanel square = new MyComponent();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am having some issues trying to get check box values. For a
I am trying to get the message box to show the elements but messagebox
I am having a terrible time trying to get an input box like the
I'm new to jquery. I'm trying to get an alert box to popup with
I am trying to get the value of a EditText in a dialog box.
Hi I am trying to get a connection from windows a unix box and
I am trying to get the following form submission to show alert box if
I have a list box and i am trying to get currently checked item
I am trying get Struts 2 and Tiles to work and I am using
I'm trying to make a box that allows you to select some variables, and

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.