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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:34:47+00:00 2026-06-13T18:34:47+00:00

I have developed a small swing application in which there is a square rotating

  • 0

I have developed a small swing application in which there is a square rotating in the upper half and there is a button in the lower half which can stop/run the square to rotate.
I have used the GridLayout to place the rotating square and the button.
(Another alternative is to use 2 JPanels ,one is with rotating square and second contains the button.Using this button appears of proper size.)

Here is the code :-

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Rotation {
JButton jbtn=new JButton("Stop");
component jpn2=new component();    //created a JPanel named jpn2 and got a reference to its timer object.
Timer timer=jpn2.timer;
Rotation()
{


    JFrame jfrm=new JFrame("Rotating a square about a center");
    jfrm.setSize(400,400);
    jfrm.setLayout(new GridLayout(2,1));
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JPanel jpnl=new JPanel();

    //jpnl.add(jbtn);

    jbtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Stop"))
        {
            timer.stop();
            jbtn.setText("Spin");
        }
        if(e.getActionCommand().equals("Spin"))
        {
            timer.start();
            jbtn.setText("Stop");
        }

    }});

    jfrm.add(jpn2);
    jfrm.add(jbtn);

    //jfrm.add(new JButton("Click"));
    jfrm.setVisible(true);
    //jfrm.setOpacity(0.8f);
}
public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
{
    //JFrame.setDefaultLookAndFeelDecorated(true);
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
}

}

class component extends JPanel implements ActionListener
{
Timer timer;
int theta=0;
component()
{
    timer=new Timer(10,this);
    timer.start();
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g;
    g2.rotate(theta,100,100);
    g2.fillRect(50, 50, 100,100);
}
public void actionPerformed(ActionEvent e)
{
    //Changing a global variable and then drawing the rectangle again and hence indirectly the square rotates.
    theta=theta+10;
    if(theta==360)
        theta=0;
    repaint();
}
}

Here is the output:-

output

But my confusion is when i decided to use FlowLayout instead of GridLayout i’m getting only the button and no rotating square.
By,as far as i have read,FlowLayout places components in a row and if space is less than it uses multiple rows.
Can anyone resolve this small stupid problem of mine which currently i am not able to resolve.

  • 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-13T18:34:48+00:00Added an answer on June 13, 2026 at 6:34 pm

    As others have said (+1 to mKorbel and HFOE)

    • The problem is you use setSize(..) rather call pack() before setting JFrame to visible.

    • You will also have to override getPrefferedSize(..) in JPanel class which will return the size of your square multiplied by 2 (or else when it rotates it wont fit).

    • On a side note dont throw any excpetion in main(..) never a good thing.

    see below for the code (uses FlowLayout but also works with GridLayout):

    Using new FlowLayout():

    enter image description here

    Using new GridLayout(2,1):

    enter image description here

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Rotation {
    
        JButton jbtn = new JButton("Stop");
        component jpn2 = new component();    //created a JPanel named jpn2 and got a reference to its timer object.
        Timer timer = jpn2.timer;
    
        Rotation() {
    
    
            JFrame jfrm = new JFrame("Rotating a square about a center");
    
            // jfrm.setLayout(new FlowLayout());
            jfrm.setLayout(new GridLayout(2, 1));
    
            jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            jbtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getActionCommand().equals("Stop")) {
                        timer.stop();
                        jbtn.setText("Spin");
                    }
                    if (e.getActionCommand().equals("Spin")) {
                        timer.start();
                        jbtn.setText("Stop");
                    }
    
                }
            });
    
            jfrm.add(jpn2);
            jfrm.add(jbtn);
    
            jfrm.pack();
            jfrm.setVisible(true);
        }
    
        public static void main(String args[]) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    
                ex.printStackTrace();
            }
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Rotation();
                }
            });
        }
    }
    
    class component extends JPanel implements ActionListener {
    
        Timer timer;
        int theta = 0;
        int width = 100, height = 100;
    
        component() {
            timer = new Timer(10, this);
            timer.start();
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.rotate(theta, 100, 100);
            g2.fillRect(50, 50, width, height);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(width * 2, height * 2);//multiply by 2 to fit while rotating
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            //Changing a global variable and then drawing the rectangle again and hence indirectly the square rotates.
            theta = theta + 10;
            if (theta == 360) {
                theta = 0;
            }
            repaint();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a small administration page for game servers which you can add/remove/edit
I have developed a small component which can be put in to any website.
I have developed a small survey application with codeigniter. Which works fine on my
I have successfully developed a small iPhone+Monotouch (latest version) application with Monodevelop 2.8, which
I have developed a small C# form application which calls a web service. Everything
I have developed a small application in Qt Creator on Ubuntu 12.04 which I
I have developed a small application and now i want to protect it. I
i have developed a small application and was working fine on developing machine but
We have developed a small web application for a client. We decided on the
I have developed three different versions(as different projects) of an app(large,normal,small). How can I

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.