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

  • Home
  • SEARCH
  • 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 8118477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:29:57+00:00 2026-06-06T04:29:57+00:00

I wish to paint and repaint a JPanel, which may be rotated 90 degrees

  • 0

I wish to paint and repaint a JPanel, which may be rotated 90 degrees with images.
The initial painting does not cause any issues. When I try to repaint() the panel it will not do this immediately, but wait for a second repaint().

My second issue is that, when i turn my panel vertical as stated in the below code, it will not repaint at all. I can’t see the image red, yellow or green at least, but the image “light” remains.

Thank you for taking your time to read this.

public void paintComponent(Graphics g)
{

    super.paintComponent(g); 

    Graphics2D g2d = (Graphics2D) g;
    if(vertical){

        g2d.translate(this.getWidth() / 2, this.getHeight() / 2); 
        g2d.rotate(-Math.PI / 2);

        g2d.translate(-img_light.getWidth(null) / 2, -img_light.getHeight(null) / 2);
        g2d.drawImage(img_light, 0, 0, this);   

        if(showRed){
            g2d.translate(-img_red.getWidth(null) / 2, -img_red.getHeight(null) / 2);   
            g2d.drawImage(img_red, 0, 0, null);
        }
        if(showYellow){
            g2d.translate(-img_yellow.getWidth(null) / 2, -img_yellow.getHeight(null) / 2);
            g2d.drawImage(img_yellow, 0, 0, null);
        }
        if(showGreen){
            g2d.translate(-img_green.getWidth(null) / 2, -img_green.getHeight(null) / 2); 
            g2d.drawImage(img_green, 0, 0, null);
        }
    }else{
        g2d.drawImage(img_light, 0, 0, this);   

        if(showRed){
            g2d.drawImage(img_red, 0, 0, null);
        }
        if(showYellow){
            g2d.drawImage(img_yellow, 0, 0, null);
        }
        if(showGreen){
            g2d.drawImage(img_green, 0, 0, null);
        }


 }   
    }
  • 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-06T04:29:58+00:00Added an answer on June 6, 2026 at 4:29 am
    • have look at tutorial Graphics2D and two methods about Graphics2d#rotate()

    • then you should be so able

    code (initial by @CtrAltDelete ???)

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.Timer;
    import javax.swing.filechooser.*;
    
    public class RotatableImageComponent extends JComponent {
    
        private static final long serialVersionUID = 1L;
        private Image image;
        private double angle = 0;
        private MyObservable myObservable;
    
        public RotatableImageComponent() {
            myObservable = new MyObservable();
        }
    
        public RotatableImageComponent(Image image) {
            this();
            this.image = image;
        }
    
        public Image getImage() {
            return image;
        }
    
        public void setImage(Image image) {
            this.image = image;
        }
    
        public double getAngle() {
            return angle;
        }
    
        public void setAngle(double angle) {
            if (angle == this.angle) {
                return;
            }
            this.angle = angle;
            double circle = Math.PI * 2;
            while (angle < 0) {
                angle += circle;
            }
            while (angle > circle) {
                angle -= circle;
            }
            if (myObservable != null) {
                myObservable.setChanged();
                myObservable.notifyObservers(this);
            }
            repaint();
        }
    
        /**
         * In the rotation events sent to the listener(s), the second argument
         * (the value) will be a reference to the RotatableImageComponent. One then
         * should call getAngle() to get the new value.
         * @param o
         */
        public void addRotationListener(Observer o) {
            myObservable.addObserver(o);
        }
    
        public void removeRotationListener(Observer o) {
            myObservable.deleteObserver(o);
        }
    
        public void rotateClockwise(double rotation) {
            setAngle(getAngle() + rotation);
        }
    
        public void rotateCounterClockwise(double rotation) {
            //setAngle(getAngle() - rotation);
            rotateClockwise(-rotation);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            if (image == null) {
                super.paintComponent(g);
                return;
            }
            Graphics2D g2 = (Graphics2D) g;
            AffineTransform trans = AffineTransform.getTranslateInstance(getWidth() / 2, getHeight() / 2);
            trans.rotate(angle);
            trans.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
            g2.transform(trans);
            g2.drawImage(image, 0, 0, null);
        }
    
        @Override
        public Dimension getPreferredSize() {
            if (image == null) {
                return super.getPreferredSize();
            }
            int wid = image.getWidth(null);
            int ht = image.getHeight(null);
            int dist = (int) Math.ceil(Math.sqrt(wid * wid + ht * ht));
            return new Dimension(dist, dist);
        }
    
        public static class TimedRotation {
    
            private RotatableImageComponent comp;
            private long totalTime, startTime;
            private double toRotate, startRotation;
            private int interval;
            public Timer myTimer;
            private myAction mAction;
    
            public TimedRotation(RotatableImageComponent comp, double toRotate, long totalTime, int interval) {
                //super(interval, new myAction());
                this.comp = comp;
                this.totalTime = totalTime;
                this.toRotate = toRotate;
                this.startRotation = comp.getAngle();
                this.interval = interval;
            }
    
            public void start() {
                if (mAction == null) {
                    mAction = new myAction();
                }
                if (myTimer == null) {
                    myTimer = new Timer(interval, new myAction());
                    myTimer.setRepeats(true);
                } else {
                    myTimer.setDelay(interval);
                }
                myTimer.start();
                startTime = System.currentTimeMillis();
            }
    
            public void stop() {
                myTimer.stop();
            }
    
            private class myAction implements ActionListener {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    long now = System.currentTimeMillis();
                    if (totalTime <= (now - startTime)) {
                        comp.setAngle(startRotation + toRotate);
                        stop();
                        return;
                    }
                    double percent = (double) (now - startTime) / totalTime;
                    double rotation = toRotate * percent;
                    comp.setAngle(startRotation + rotation);
                }
            }
        }
    
        private class MyObservable extends Observable {
    
            @Override
            protected void setChanged() {
                super.setChanged();
            }
        }
    
        public static class RotationKeys extends KeyAdapter {
    
            private RotatableImageComponent comp;
            private double rotationAmt;
    
            public RotationKeys(RotatableImageComponent comp, double rotationAmt) {
                this.comp = comp;
                this.rotationAmt = rotationAmt;
            }
    
            public RotationKeys(RotatableImageComponent comp) {
                this(comp, Math.PI / 90);
            }
    
            @Override
            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                    comp.rotateCounterClockwise(rotationAmt);
                } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                    comp.rotateClockwise(rotationAmt);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
                        JFileChooser chooser = new JFileChooser();
                        chooser.addChoosableFileFilter(filter);
                        if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                            File f = chooser.getSelectedFile();
                            BufferedImage im = ImageIO.read(f);
                            final RotatableImageComponent c = new RotatableImageComponent(im);
                            c.addRotationListener(new Observer() {
    
                                @Override
                                public void update(Observable arg0, Object arg1) {
                                    System.out.println("Angle changed: " + ((RotatableImageComponent) arg1).getAngle());
                                }
                            });
                            JPanel controls = new JPanel(new FlowLayout());
                            final JTextField rotation = new JTextField();
                            rotation.setText("30");
                            controls.add(new JLabel("Rotation(degrees)"));
                            controls.add(rotation);
                            final JTextField time = new JTextField();
                            time.setText("1000");
                            time.setColumns(6);
                            rotation.setColumns(7);
                            controls.add(new JLabel("Time(millis)"));
                            controls.add(time);
                            JButton go = new JButton("Go");
                            go.addActionListener(new ActionListener() {
    
                                @Override
                                public void actionPerformed(ActionEvent ae) {
                                    TimedRotation tr = new TimedRotation(c,
                                            Double.parseDouble(rotation.getText()) / 180 * Math.PI,
                                            Integer.parseInt(time.getText()), 50);
                                    tr.start();
                                }
                            });
                            controls.add(go);
                            RotationKeys keys = new RotationKeys(c);
                            c.addKeyListener(keys);
                            c.setFocusable(true);
                            JFrame jf1 = new JFrame();
                            jf1.getContentPane().add(c);
                            JFrame jf2 = new JFrame();
                            jf2.getContentPane().add(controls);
                            jf1.pack();
                            jf2.pack();
                            jf1.setLocation(100, 100);
                            jf2.setLocation(400, 100);
                            jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            jf2.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                            jf1.setVisible(true);
                            jf2.setVisible(true);
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app where users select images they wish to print, the print
I have a form on which I have a number of textboxes. I wish
I wish to print a Stack<Integer> object as nicely as the Eclipse debugger does
I have subclassed java.awt.Frame and have overridden the paint() method as I wish to
I wish to write a C program which obtains the system time and hence
I wish to print out double as the following rules : 1) No scietific
I wish to determine the intersection point between a ray and a box. The
I wish to determine if a Point P(x,y,z) is inside a 2D circle in
I wish to render a scene that contains one box and a point light
I wish to send an email from my localhost machine (using PHPs mail function)

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.