Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make line animation smoother?
    primarykey
    data
    text
    <p>I am making a simple animation in Java and I am trying to make it as smooth as possible.</p> <p>I use only *.Double inner classes of each Shape object and I set antialiasing in the Graphics2D objects on. It all works as long as I use only the fill() method but if I also use draw() method to draw lines around the same Shape the animation of these lines is choppy - pixel by pixel.</p> <p>Each of my rectangles on the canvas has this method to paint itself. It is moved every 20ms and the whole canvas is repainted using Timer and TimerListener.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AnimationTest { public static void main(String[] args) { JFrame frm = new JFrame("Test"); frm.setBounds(200, 200, 400, 400); frm.setResizable(false); frm.setLocationRelativeTo(null); AnimationCanvas a = new AnimationCanvas(); frm.add(a); frm.setVisible(true); a.startAnimation(); } } class AnimationCanvas extends JPanel { SimpleSquare[] squares = new SimpleSquare[2]; AnimationCanvas() { squares[0] = new SimpleSquare(50, 80, true); squares[1] = new SimpleSquare(160, 80, false); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for (SimpleSquare c : squares) { c.paintSquare(g); } } Timer t; public void startAnimation() { t = new Timer(30, new Animator()); t.start(); } private class Animator implements ActionListener { @Override public void actionPerformed(ActionEvent e) { squares[0].y += 0.10; squares[1].y += 0.10; repaint(); } } } class SimpleSquare { double x; double y; Color color = Color.black; boolean fill; SimpleSquare(double x, double y, boolean fill) { this.x = x; this.y = y; this.fill = fill; } void paintSquare(Graphics g) { ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Shape s = new Rectangle.Double(x, y, 100, 100); g.setColor(color); ((Graphics2D) g).setStroke(new BasicStroke(2)); if (fill) { ((Graphics2D) g).fill(s); } else { ((Graphics2D) g).draw(s); } } } </code></pre> <p>Is there any way to fix this problem? I looked around for quite a while.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload