Have you converted the Graphics object to Graphics2D and set RenderHints ? iee
Graphics2D g2d=(Graphics2D)g; g2d.setRenderingHint(RenderHints.KEY_ANTIALIASING,RenderHints.VALUE_ANTIALIASING _ON);
This will add some nice anti aliasing effects and may make the line more straight.
In addition, increasing the stroke width through Graphics2D#setStroke will cause the edges of the gears to disappear, as it will now become thicker.
See this example (click, drag and release the mouse to create a row):
With g2d.setRenderingHint(..) and g2d.setStroke(..) in paintComponent(..) commented out:

With g2d.setRenderingHint(..) and g2d.setStroke(..) uncommented:

import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class Test { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Test gui = new Test(); } }); } public Test() { initComponents(); } private void initComponents() { JFrame frame = new JFrame("Line Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MyPanel()); frame.pack(); frame.setVisible(true); } } class MyPanel extends JPanel { Point point1; Point point2; Line2D line2d; public MyPanel() { addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent me) { super.mousePressed(me); point1 = me.getPoint(); } }); addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent me) { super.mouseDragged(me); point2 = me.getPoint(); line2d = new Line2D.Double(point1, point2); repaint(); } }); } @Override public Dimension getPreferredSize() { return new Dimension(300, 300); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g;
If the above does not help, publishing SSCCE will allow us to check and see what could be a mistake / make it better.
source share