Java error? Why draws a rectangle instead of an ellipse?

The code below draws a rectangle and 2 ellipses.

enter image description here

For now, you need to draw 3 ellipses.

My OS is Windows 7 prof 64 bit

My Java is 1.6 x 86, 1.7 x64 is also tested.

Why?

package tests; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import net.miginfocom.swing.MigLayout; public class AntialiacingScaleTester { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JPanel circlePanel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.RED); g2d.setStroke(new BasicStroke(1)); //g2d.drawOval(0, 0, 200, 200); g2d.draw(new Ellipse2D.Double(0, 0, 200, 200)); AffineTransform old = g2d.getTransform(); g2d.setColor(Color.GREEN); g2d.scale(1000, 1000); g2d.setStroke(new BasicStroke(0.001f)); g2d.draw(new Ellipse2D.Double(0, 0, 0.225, 0.225)); g2d.setColor(Color.BLUE); g2d.scale(10, 10); g2d.setStroke(new BasicStroke(0.001f)); g2d.draw(new Ellipse2D.Double(0, 0, 0.025, 0.025)); g2d.setTransform(old); } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setLayout(new MigLayout("fill")); //frame.add(circlePanel, "w 300, h 300, grow"); //frame.add(circlePanel); frame.setLayout(null); circlePanel.setBounds(new Rectangle(0, 0, 300, 300)); frame.add(circlePanel); frame.setBounds(0, 0, 350, 300); //frame.pack(); frame.setVisible(true); } }); } } 
+4
source share
2 answers

I copied / pasted your code, and it drew 2 ellipses you wrote about, the only change I made was to replace your MigLayout with null , manually set the frame and panel sizes and delete the frame.pack() call:

 import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Test { /** * @param args */ public static void main(String[] args) { JPanel circlePanel = new JPanel() { @Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(1)); //g2d.drawOval(0, 0, 200, 200); g2d.draw(new Ellipse2D.Double(0, 0, 200, 200)); AffineTransform old = g2d.getTransform(); g2d.scale(10000, 10000); g2d.setStroke(new BasicStroke(0.001f)); g2d.draw(new Ellipse2D.Double(0, 0, 0.025, 0.025)); g2d.setTransform(old); } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); circlePanel.setBounds(new Rectangle(0, 0, 300, 300)); frame.add(circlePanel); frame.setBounds(0, 0, 350, 300); //frame.pack(); frame.setVisible(true); } } 

enter image description here

Update: I can reproduce the problem using Oracle JDK (Java version 1.8.0-ea) instead of OpenJDK. Having received the diamond shape, as indicated in other answers, the scale factor is the main reason for the shape deforming, I don’t know if this should be a suitable behavior, so there must be a mistake in one of these JREs. The following test program works great for both JREs:

 import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Test { /** * @param args */ public static void main(String[] args) { JPanel circlePanel = new JPanel() { @Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(1)); //g2d.drawOval(0, 0, 200, 200); g2d.draw(new Ellipse2D.Double(0, 0, 200, 200)); AffineTransform old = g2d.getTransform(); g2d.scale(10, 10); g2d.setStroke(new BasicStroke(1.0f)); g2d.draw(new Ellipse2D.Double(0, 0, 25.0, 25.0)); g2d.setTransform(old); } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); circlePanel.setBounds(new Rectangle(0, 0, 300, 300)); frame.add(circlePanel); frame.setBounds(0, 0, 350, 300); //frame.pack(); frame.setVisible(true); } } 
+2
source

Replication in Java 7, Windows 7 in Eclipse, removal of the Layout Manager.

I feel this is due to a combination of high scaling and inaccuracies at small floating point values, reducing the number of points generated.

If you substitute values ​​between 0.0363 and 0.0362, the rendering API is interrupted. It no longer generates an arc, but instead a square.

The workaround is to stop combining huge scaling with small sized objects. Zoom out and zoom in.

+1
source

All Articles