import javax.swing.*; import java.awt.Graphics; import java.awt.Color; public class MyDrawing_Start extends JFrame { public MyDrawing_Start() { add (new MyPanel()); } public static void main (String [] args){ MyDrawing_Start frame = new MyDrawing_Start(); frame.setTitle("My drawing demo"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } } class MyPanel extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); // For color use the either the next 2 lines or g.setColor(Color.BLUE); Color myColor = new Color(0, 0, 255); g.setColor(Color.RED); for(int i= 20, j=20; i<=100; i+=5, j +=10 ) { g.drawLine(100,150,100,50); } g.setColor(Color.BLUE); for(int i=100, j=100; i<=200; i++, j+= 10) g.drawLine(i,j,50,50); } }
--- Update ---
Hi I need help to draw something with lines and loops. I have the looping down but I have never drawn anything with this. The lines go out of control and don't form anything coherent. Can someone help me draw a smiley face, 2 lines for the eyes and one for the mouth, I tried and my lines were never ending in some cases.