2

I'm writing a simple game for studying purposes. Everything goes fine except one little thing...

I cant figure out how to rotate square without this ugly jumping

enter image description here

here a simplified version of my program illustrating an issue, here i use one timer, but in original program i have 2 timers one for handle game state and second for repaint:

public class soQuestion extends JLabel {

    double r;

    @Override
    public void paintComponent(Graphics g1) {
        Graphics2D g = (Graphics2D) g1;
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g.clearRect(0,0, getWidth(), getHeight());
        g.translate(getWidth()/2, getHeight()/2);
        g.rotate(r);
        g.translate(-20, -20);
        g.fillRect(0, 0, 40, 40);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        soQuestion question = new soQuestion();
        frame.add(question);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        new javax.swing.Timer(10, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                r += 0.005;
                question.repaint();
            }
        }).start();
    }
}

jumping is more visible if rotation delta values is small, for fast rotated objects is less visible..

all rendering hints i used has no effect

PS: Sorry for my english

PPS: i can provide more details if needed, how it looks in full version:

enter image description here

Stranger in the Q
  • 3,668
  • 2
  • 21
  • 26
  • 2
    You should not be changing state within the paintComponent method but rather within the timer. The paint method should be for painting alone. – Hovercraft Full Of Eels Apr 07 '19 at 14:33
  • Also, consider creating a collection of images, and then rather than doing complex calculations repeatedly, simply swap images based on the calculations all done at the start. – Hovercraft Full Of Eels Apr 07 '19 at 14:34
  • @HovercraftFullOfEels in full code i have a two timers one for game state ticks and another for repainting, this simple example only for exampple, if there are important thing for this example i can separate it.. – Stranger in the Q Apr 07 '19 at 14:34
  • Did you try with `g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);`? – Pshemo Apr 07 '19 at 14:48
  • @Pshemo thank you sir, its a copy-paste issue... – Stranger in the Q Apr 07 '19 at 14:49
  • What Hovercraft is saying is: Do not do `g.rotate(r += 0.005);` in a painting method. Do not alter `r` or any other state variable in a painting method. It’s actually quite common for painting to be called for many reasons, such as the user moving the mouse over the component. Also, a single paint operation may consist of *multiple* calls to paintComponent, with different clip regions set by the system. Change `r` in your Timer’s actionPerformed method instead. – VGR Apr 07 '19 at 15:02
  • See also this related [example](https://stackoverflow.com/a/3420651/230513). – trashgod Apr 08 '19 at 01:23

2 Answers2

2

Thanks for all participants!

It is fully my mistake and inattention. I was copied this code section from some source :

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

And before ask a question here i couldn't saw that i used hint related to text rendering.

Special thanks to mr @Pshemo for suggesting of using this rendering hint:

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

I was know about it before asking, but i'm a human and could not figure out where a mistake for an a hour and asked.

PS: sorry for my english

Stranger in the Q
  • 3,668
  • 2
  • 21
  • 26
0

I'm a new contributor, so if its not the best help, sry but i am trying :)

have you tried a solution where you are using Math.toRadiants() in your g.rotate() function? in this video: Java: Rotating and scaling images the image rotates without jumping, it's at 6:42 in the video.

So in your solution, it would look like that: g.rotate(Math.toRadiants(r += 0.005));