Here i created four buttons. If i clicked second one i should able to draw a line.

please tell me what is the mistake..

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Polygon extends JFrame implements ActionListener
{
	JFrame f;
	public JPanel jp1,jp2,jp3;
	JButton b1,b2,b3,b4,b5;
	JTextField txt;
	int x,y;
	Polygon()
	{
		super("Polygon");
		setSize(1000,1000);
		setVisible(true);
		setDefaultCloseOperation(3);
	}
	void createPanel()
	{
		b1 = new JButton("Oval");
		b2 = new JButton("Rect");
		b3 = new JButton("Line");
		b4 = new JButton("Clear");
		txt = new JTextField(20);
		jp1 = new JPanel();
		jp2 = new JPanel();
		//jp3 = new Jpanel();
		add(jp1,BorderLayout.WEST);
		add(jp2,BorderLayout.CENTER);
		//add(jp3.BorderLayout.NORTH);
		GridLayout gl = new GridLayout(5,1);
		jp1.setLayout(gl);
		jp1.add(b1);
		jp1.add(b2);
		jp1.add(b3);
		jp1.add(b4);
		jp2.add(txt);
		b2.addActionListener(this);
	}
	public void actionPerformed(ActionEvent ae)
	{
		getContentPane().add(jp2);
		txt.setText("Hai");
		jp2.addMouseMotionListener(new MouseMotionAdapter()
		{
			public void mouseEntered(MouseEvent me)
			{
				x = me.getX();
				y = me.getY();
				repaint();
			}
			public void paint(Graphics g)
			{
				Graphics gg = jp2.getGraphics();
				gg.drawString(".",x,y);
			}
		});
	}		
	public static void main(String a[])
	{
		Polygon p = new Polygon();
		p.createPanel();
	}
}

Please explain what the code is doing or not doing.
Try debugging the code by adding print outs to show where the execution flow is going.

Please put your code in code tags. Use icon above to right. Info here also: http://www.java-forums.org/misc.php?do=bbcode#code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.