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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:00:46+00:00 2026-06-14T19:00:46+00:00

Hi! I have the code below using previous Stackoverflow posts. I want to just

  • 0

Hi!

I have the code below using previous Stackoverflow posts.

I want to just rotate the rectangle by some angle and make it move in sin wave.

This code rotates the whole sin wave too.

I understand why it is happening , but I don’t know how to achieve my intention.

please help!!!

Thanks a lot for taking time.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Withrotation {

    public static int i = 1;
    public static Ticker t;
    public static Repainter r;
    public static int newx, newy;

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Wavy!");
        final WavyPanel wp = new WavyPanel();
        frame.getContentPane().add(wp, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t = new Ticker(wp);
        r = new Repainter(wp);
        frame.pack();
        frame.setVisible(true);
        final Timer tickTimer = new Timer();
        final Timer paintTimer = new Timer();
        paintTimer.schedule(r, 1000, 50);
        tickTimer.schedule(t, 1000, 10);
    }

    private static class WavyPanel extends JPanel {

        private final Dimension size = new Dimension(640, 480);
        private int amplitude = 50;
        private int frequency = 5;
        private double x1 = 0;
        private double y1 = 500;
        private int yBase = 0;

        WavyPanel() {
            super(true);
        }

        @Override
        protected void paintComponent(final Graphics g) {
            final Graphics2D g2 = (Graphics2D) g;
            AffineTransform old = g2.getTransform();
            g2.rotate(Math.toRadians(-30));

            g2.clearRect(0, 0, this.getSize().width, this.getSize().height);
            g2.setColor(Color.BLACK);
            g2.fillRect((int) x1, (int) y1, 20, 80);
            g2.setTransform(old);


        }

        @Override
        public Dimension getPreferredSize() {
            return size;
        }

        @Override
        public Dimension getMinimumSize() {
            return size;
        }

        @Override
        public Dimension getMaximumSize() {
            return size;
        }

        public void tick() {
            x1 = x1 + 1;
            final int waveLength = size.width / frequency;
            yBase = (++yBase) % waveLength;
            final double normalized = (double) yBase / (double) waveLength;
            final double radians = normalized * Math.PI * 2;
            final double sine = Math.sin(radians);
            y1 = (int) (sine * amplitude);




        }
    }

    private static class Ticker extends TimerTask {

        private final WavyPanel panel;

        Ticker(final WavyPanel panel) {

            this.panel = panel;

        }

        @Override
        public void run() {
            panel.tick();
        }
    }

    private static class Repainter extends TimerTask {

        private final WavyPanel panel;

        Repainter(final WavyPanel panel) {

            this.panel = panel;

        }

        @Override
        public void run() {
            panel.repaint();

        }
    }
}
  • 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-14T19:00:48+00:00Added an answer on June 14, 2026 at 7:00 pm

    +1 for SSCCE

    1) Dont forget to have call to super.paintComponent(); as first statement in your overridden paintComponent(..) method.

    2) Swing UI should be created on EDT and used in conjunction with Swing Timers

    3) Java variable naming convention for classes is uppercase letter for each new word i.e WithRotation.

    4) No need for frame.getContentPane.add(..) simply use add(..) as all calls are forwarded to its contentPane.

    Here is the example I made (basically your code with above fixes implemented), which only rotates the rectangle which follows the graph and not the whole graphics object using AffineTransform#createTransformedShape():

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.geom.AffineTransform;
    import javax.swing.AbstractAction;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class WithRotation {
    
        private JFrame frame;
        private WavyPanel wp;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new WithRotation();
                }
            });
        }
    
        public WithRotation() {
            initComponents();
        }
    
        private void initComponents() {
            frame = new JFrame("Wavy!");
            wp = new WavyPanel();
            frame.add(wp, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
    
            createAndStartTimers();
        }
    
        private void createAndStartTimers() {
            new Timer(50, new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
    
                    wp.repaint();
                }
            }).start();
            new Timer(10, new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent ae) {
    
                    wp.tick();
                }
            }).start();
        }
    
        class WavyPanel extends JPanel {
    
            private final Dimension size = new Dimension(640, 480);
            private int amplitude = 50;
            private int frequency = 5;
            private double x1 = 0;
            private double y1 = 500;
            private int yBase = 0;
    
            WavyPanel() {
                super(true);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
    
                g2.clearRect(0, 0, this.getSize().width, this.getSize().height);
                g2.setColor(Color.BLACK);
    
                Rectangle rect = new Rectangle((int) x1, (int) y1, 20, 80);
    
                AffineTransform transform = new AffineTransform();
                transform.rotate(Math.toRadians(-30), rect.getX() + rect.width / 2, rect.getY() + rect.height / 2);
    
                Shape transformed = transform.createTransformedShape(rect);
                g2.fill(transformed);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return size;
            }
    
            @Override
            public Dimension getMinimumSize() {
                return size;
            }
    
            @Override
            public Dimension getMaximumSize() {
                return size;
            }
    
            public void tick() {
                x1 = x1 + 1;
                final int waveLength = size.width / frequency;
                yBase = (++yBase) % waveLength;
                final double normalized = (double) yBase / (double) waveLength;
                final double radians = normalized * Math.PI * 2;
                final double sine = Math.sin(radians);
                y1 = (int) (sine * amplitude);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below: using (SqlCommand command = new SqlCommand()) { command.CommandType =
I have the code below: string SQL = select * from + TableName; using
I have tried using the code below but it only display results in Chrome
I have strings formatted using the code below String.Format({0,-10} {1,10}, Kills:, kills); String.Format({0,-10} {1,10},
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have a number of nodejs applications running on Express using the code below.
I am using vb.net code. I have grid view, please see the code below:
My code is below. I have an array, and by using this array I
Little trouble in using css, in the below code, i have used odd and
I have the below code: CODE BEHIND: using System; using System.Collections.Generic; using System.Linq; using

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.