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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:16:09+00:00 2026-06-12T20:16:09+00:00

import javax.swing.*; import java.awt.*; public class JFrameAnimationTest extends JFrame { public static void main(String[]

  • 0
 import javax.swing.*;
 import java.awt.*;

 public class JFrameAnimationTest extends JFrame {
     public static void main(String[] args) throws Exception{
        AnimationPanel animation = new AnimationPanel();
        JFrameAnimationTest frame = new JFrameAnimationTest();
        frame.setSize(600, 480);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(animation);
        frame.setVisible(true);        
        for(int i = 0; i < 100; i++) {
            animation.incX(1);
            //animation.incY(1);
            animation.repaint();
            Thread.sleep(10);
        }
    }
}

class AnimationPanel extends JPanel {

    int x = 10;
    int y = 10;   

  public AnimationPanel() {        
  }

  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.BLUE);
      g.drawRect(x, y, 20, 20);
      g.fillRect(x, y, 20, 20);
  }

  protected void incX(int X) {
      x += X;
  }

  protected void incY(int Y) {
      y += Y;
  }
}

So anyways theres my code. It probably looks a bit jumbled as I am not used to stackoverflow just yet so I apologize.

Here’s my question: This program makes this small rectangle slowly move to the right; how can I add rotation to the rectangles movement during that time period?

  • 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-12T20:16:11+00:00Added an answer on June 12, 2026 at 8:16 pm

    Note: I haven’t actually compiled this code, but you get the gist.

    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );
        Graphics2D g2d = (Graphics2D) g;
    
        // The 20x20 rectangle that you want to draw
        Rectangle2D rect = new Rectangle2D.Double( 0, 0, 20, 20 );
    
        // This transform is used to modify the rectangle (an affine
        // transform is a way to do operations like translations, rotations,
        // scalings, etc...)
        AffineTransform transform = new AffineTransform();
    
        // 3rd operation performed: translate the rectangle to the desired
        // x and y position
        transform.translate( x + 10, y + 10 );
    
        // 2nd operation performed: rotate the rectangle around the origin
        transform.rotate( rotation );
    
        // 1st operation performed: translate the rectangle such that it is
        // centered on the origin
        transform.translate( -10, -10 );
    
        // Apply the affine transform
        Shape s = transform.createTransformedShape( rect );
    
        // Fill the shape with the current paint
        g2d.fill( s );
    
        // Stroke the edge of the shape with the current paint
        g2d.draw( s );
    }
    

    Also note that you should really be using something like a javax.swing.Timer when you modify x, y, and rotation and when you call repaint(). That way all of it happens on the event dispatch thread.

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

Sidebar

Related Questions

import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
Code: import java.awt.Dimension; import javax.swing.*; public class Game extends JFrame { private static final
import javax.swing.*; import java.awt.*; public class Fr extends JFrame{ Fr(String s){ super(s); setSize(200,300); setVisible(true);
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Code : import javax.swing.*; import java.awt.*; public class firstGUI extends JPanel { public static
import java.awt.Color; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyDrawPanel extends
import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JOptionPane; public class NewJFrame extends javax.swing.JFrame {
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class displayFullScreen extends JFrame { private JLabel
import java.awt.Graphics; import javax.swing.*; public class Demo { JFrame jf; JLabel[] labels; JPanel panel;
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static

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.