I can't see what you're talking about in that picture. I took the code you posted and made it runnable. What's wrong with this?
import java.awt.*; import java.awt.geom.*; import javax.swing.JFrame; import javax.swing.JPanel; class TestClass extends JPanel { private int x = 50, y = 10; public TestClass() { x = 50; y = 10; setPreferredSize( new Dimension( 380, 80 ) ); setBackground(Color.GRAY); } @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paintComponent(g2); BasicStroke pen = new BasicStroke(4F); g2.setStroke(pen); //Square Rectangle rect1 = new Rectangle(x,y,50,50); g2.setColor(Color.BLACK); g2.draw(rect1); //Circle Ellipse2D.Float circle1 = new Ellipse2D.Float(x+100,y,50,50); g2.setColor(Color.BLUE); g2.draw(circle1); int xCoords[] = {x+200,x+200,x+260}; int yCoords[] = {y,y+50,y+50}; //Triangle Polygon poly1 = new Polygon(xCoords, yCoords,3); g2.setColor(Color.RED); g2.draw(poly1); } public static void main( String[] args ) { SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add( new TestClass() ); frame.pack(); frame.setVisible( true ); } // end method run() } ); } // end method main() } // end class TestClass