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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:08:11+00:00 2026-06-13T07:08:11+00:00

I have developed a little swing application in which i have drawn a square.Now

  • 0

I have developed a little swing application in which i have drawn a square.Now i want to rotate this square at its center using Thread.The problem i’m having is how to get reference to that square in my rotateSquare() method.
(Actually i need a method,if possible, to rotate the same square instead of wiping out the entire content pane and drawing another rotated square at its position).

Here is my code:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class Rotation extends JFrame implements Runnable{
Thread t;
Rotation()
{
    super("Animation of rotation about center");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);

    setContentPane(new Container(){
        public void paint(Graphics g)
        {
            Graphics2D g2=(Graphics2D)g.create();
            g2.setBackground(Color.WHITE);
            g2.clearRect(0, 0, getWidth(), getHeight());

            g2.setColor(Color.GRAY);
            g2.fillRect(100, 100, 100, 100);
        }
    });

    t=new Thread(this,"pr");
    t.start();


    setVisible(true);
    //setOpacity(0.8f);
}
public void run()
{
    try{
        for(;;)
        {
            Thread.sleep(100);
            SwingUtilities.invokeLater(new Runnable(){public void run(){
                rotateSquare();
            }});
        }
    }catch(InterruptedException e){System.out.println("Thread interrupted");}
}
public void rotateSquare();
{

}
public static void main(String args[])
{
    //setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}

}
  • 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-13T07:08:12+00:00Added an answer on June 13, 2026 at 7:08 am
    • see AffineTransform

    • add parameters AffineTransform.rotate(intDegrees);

    • fit rotated Object to the parents bounds myAffineTransform.translate(getWidth(), getHeight())

    • then returns back Graphics2D#transform(myAffineTransform);

    • use Swing Timer instead of Runnable#Thread with Thread.sleep(int), that caused freeze or flickering for Graphics2D and locked EventDispatchThread untill endless Thread ended (never ending in your case)

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

Sidebar

Related Questions

I have developed a website using Razor (Microsoft WebMatrix) and now I want to
I have developed a website, using Twitter API. Now a little but important problem
I have developed an application in mootools. But its going little slow coz of
I'm a little confused. We have developed an net 4.0 application. We use nhibernate
I have developed a little game using XNA 4.0 and C# (new Project->Windows Game
I have a very unreasonable requirement from my overlord. I developed a little application
I have an application developed using Adobe AIR. If the target machine is using
I have a little problem. I have developed a midlet application that call some
I have developed a Silverlight control which shows a list of documents. This takes
I am pretty new to WPF. I have developed a simple application using WPF.

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.