Member Avatar for starsinthesky

i cant figure out what's wrong with my code. can anyone help me pls?

import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;

public class MyGUI implements WindowListener, ActionListener 
{
	
	public void windowClosing(WindowEvent e) { System.exit(0); }
    public void windowClosed(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowOpened(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }
 
    private MyCanvas fCanvas;
    private TextField x;
    private TextField y;
    private TextField w;
    private TextField h;
    private TextField x1;
    private TextField y1;
    private TextField x2;
    private TextField y2;
 
    //Create frame and add components
    public MyGUI() 
    {
        //Add your code here to create a frame, add Labels, TextFields and Buttons, you need choose suitable Layout 
		JPanel JP1 = new JPanel();
		BorderLayout BL = new BorderLayout();
		fCanvas = new MyCanvas();
		JP1.setLayout(BL);
		JP1.add(fCanvas);
		
		JPanel JP3 = new JPanel();
		FlowLayout BL9 = new FlowLayout();
		JP3.setLayout(BL9);
		JLabel ex = new JLabel("         X  ");
		JTextField x = new JTextField(5);
		JP3.add(ex);
		JP3.add(x);
		
		JPanel JP4 = new JPanel();
		FlowLayout BL1 = new FlowLayout();
		JP4.setLayout( BL1 );
		JLabel wai = new JLabel("Y  ");
		JTextField y = new JTextField(5);
		JP4.add(wai);
		JP4.add(y);
		
		JPanel JP5 = new JPanel();
		BorderLayout BL2 = new BorderLayout();
		JP5.setLayout( BL2 );
		JP5.add(JP3, BorderLayout.WEST);
		JP5.add(JP4, BorderLayout.EAST);
		
		JPanel JP6 = new JPanel();
		FlowLayout BL3 = new FlowLayout();
		JP6.setLayout( BL3 );
		JLabel w = new JLabel("Width  ");
		JTextField width = new JTextField(5);
		JP6.add(w);
		JP6.add(width);
		
		JPanel JP7 = new JPanel();
		FlowLayout BL4 = new FlowLayout();
		JP7.setLayout( BL4 );
		JLabel h = new JLabel("Height  ");
		JTextField height = new JTextField(5);
		JP7.add(h);
		JP7.add(height);
		
		JPanel JP8 = new JPanel();
		BorderLayout BL5 = new BorderLayout();
		JP8.setLayout( BL5 );
		JP8.add(JP6, BorderLayout.WEST);
		JP8.add(JP7, BorderLayout.EAST);
		
		JPanel JP9 = new JPanel();
		FlowLayout BL6 = new FlowLayout();
		JP9.setLayout( BL6 );
		JButton rect = new JButton("Rectangular");
		rect.addActionListener(this); 
		rect.setActionCommand("rect");
		JButton oval = new JButton("Oval");
		oval.setActionCommand("oval");
		oval.addActionListener(this); 
		JP9.add(rect);
		JP9.add(oval);
		
		JPanel JP12 = new JPanel();
		FlowLayout BL10 = new FlowLayout();
		JP12.setLayout( BL10 );
		JLabel ex1 = new JLabel("      X1  ");
		JTextField x1 = new JTextField(5);
		JP12.add(ex1);
		JP12.add(x1);
		
		JPanel JP13 = new JPanel();
		FlowLayout BL11 = new FlowLayout();
		JP13.setLayout( BL11 );
		JLabel wai1 = new JLabel("      Y1  ");
		JTextField y1 = new JTextField(5);
		JP13.add(wai1);
		JP13.add(y1);
		
		JPanel JP14 = new JPanel();
		BorderLayout BL12 = new BorderLayout();
		JP14.setLayout( BL12 );
		JP14.add(JP12, BorderLayout.WEST);
		JP14.add(JP13, BorderLayout.EAST);
		
		JPanel JP15 = new JPanel();
		FlowLayout BL13 = new FlowLayout();
		JP15.setLayout( BL13 );
		JLabel ex2 = new JLabel("      X2  ");
		JTextField x2 = new JTextField(5);
		JP15.add(ex2);
		JP15.add(x2);
		
		JPanel JP16 = new JPanel();
		FlowLayout BL14 = new FlowLayout();
		JP16.setLayout( BL14 );
		JLabel wai2 = new JLabel("      Y2  ");
		JTextField y2 = new JTextField(5);
		JP16.add(wai2);
		JP16.add(y2);
		
		JPanel JP17 = new JPanel();
		BorderLayout BL15 = new BorderLayout();
		JP17.setLayout( BL15 );
		JP17.add(JP15, BorderLayout.WEST);
		JP17.add(JP16, BorderLayout.EAST);
		
		JPanel JP18 = new JPanel();
		FlowLayout BL16 = new FlowLayout();
		JP18.setLayout( BL16 );
		JButton line = new JButton("Line");
		line.addActionListener(this); 
		line.setActionCommand("line");
		JP18.add(line);
		
		JPanel JP11 = new JPanel();
		GridLayout GL = new GridLayout(6,1);//row,column
		JP11.setLayout( GL );
		JP11.add(JP5);
		JP11.add(JP8);
		JP11.add(JP9);
		JP11.add(JP14);
		JP11.add(JP17);
		JP11.add(JP18);
				
		JFrame frame = new JFrame("The Shaper");
		BorderLayout BL8 = new BorderLayout();
		frame.setLayout(BL8);
		frame.add(JP1, BorderLayout.WEST);
		frame.add(JP11, BorderLayout.EAST);
		
		frame.setSize(680,450);
		frame.setResizable(false);    
		frame.setLocation(310, 180);
		frame.add(JP1);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		

 }
    
  
    //Listen to the buttons' event and perform an action
    	public void actionPerformed(ActionEvent evt) 
  		{
     	//Add your code here to check which button has been clicked and draw the shape according to the button's event 
     		if(evt.getActionCommand().equals("rect"))
     		{
     			String x1 = x.getText();
     			int a = Integer.parseInt(x1);
     			String y1 = y.getText();
     			int b = Integer.parseInt(y1);
     			String width = w.getText();
     			int c = Integer.parseInt(width);
     			String height = h.getText();
     			int d = Integer.parseInt(height);
     			int temp = 1;
     			fCanvas.Draw(a,b,c,d,temp);
     			fCanvas.repaint();
     		}
            else if(evt.getActionCommand().equals("oval"))
     		{
     			String x2 = x.getText();
     			int a = Integer.parseInt(x2);
     			String y2 = y.getText();
     			int b = Integer.parseInt(y2);
     			String width2 = w.getText();
     			int c = Integer.parseInt(width2);
     			String height2 = h.getText();
     			int d = Integer.parseInt(height2);
     			int temp = 2;
     			fCanvas.Draw(a,b,c,d,temp);
     			fCanvas.repaint();
     		}    
     		else if(evt.getActionCommand().equals("line"))
     		{
     			String X1 = x1.getText();
     			int a = Integer.parseInt(X1);
     			String Y1 = y1.getText();
     			int b = Integer.parseInt(Y1);
     			String X2 = x2.getText();
     			int c = Integer.parseInt(X2);
     			String Y2 = y2.getText();
     			int d = Integer.parseInt(Y2);
     			int temp = 3;
     			fCanvas.Draw(a,b,c,d,temp);
     			fCanvas.repaint();
     		}
  		}	
    
   public static void main(String[] args) 
    {
    	MyGUI theGUI = new MyGUI();
    
    }
 }
	class MyCanvas extends Canvas 
	{ 
         //Add data members here
 		public int x1,y1,x2,y2,choice;
 		
 		public MyCanvas() 
    	{ 
        	setSize(400, 400);
        	setBackground(Color.CYAN);
        }
 
 		public void Draw(int a,int b,int c,int d,int temp)
 		{
 			x1=a;
 			y1=b;
 			x2=c;
 			y2=d;
 			choice=temp;
 		}
       	
       	public void paint(Graphics g) 
        {
            //Add your code here to draw a shape 
        	if(choice==1)
        	{
        		g.drawRect(x1,y1,x2,y2);
        	}
        	else if(choice==2)
        	{
        		g.drawOval(x1,y1,x2,y2);
        	}
        	else if(choice==3)
        	{
        		g.drawLine(x1,y1,x2,y2);
        	}
        }
	}

this program should paints the shape chosen in the canvas on the left side of the frame.

thanks ahead!

Recommended Answers

All 4 Replies

who knows?

<raking metal cup across iron jail bars/>
Nobody (at least the trouble I'm in)!

It might help to know a few more details.

What is it doing, that it should not be?
What is it not doing, that it should be?
What compiler messages are coming (post them in their entirety)?
What exceptions are coming (post them in their entirety)?

Member Avatar for starsinthesky

<raking metal cup across iron jail bars/>
Nobody (at least the trouble I'm in)!

It might help to know a few more details.

What is it doing, that it should not be?
What is it not doing, that it should be?
What compiler messages are coming (post them in their entirety)?
What exceptions are coming (post them in their entirety)?

when i try to input values on the textfields and click the button, e.g. rectangular, no rectangle would appear on the canvas. instead, the following error messages appear...

--------------------Configuration: <Default>--------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MyGUI.actionPerformed(MyGUI.java:181)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6134)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5899)
    at java.awt.Container.processEvent(Container.java:2023)
    at java.awt.Component.dispatchEventImpl(Component.java:4501)
    at java.awt.Container.dispatchEventImpl(Container.java:2081)
    at java.awt.Component.dispatchEvent(Component.java:4331)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
    at java.awt.Container.dispatchEventImpl(Container.java:2067)
    at java.awt.Window.dispatchEventImpl(Window.java:2458)
    at java.awt.Component.dispatchEvent(Component.java:4331)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Process completed.
...
    private TextField x;
    ...
    public MyGUI() 
    {
        ....
        JTextField x = new JTextField(5);

The "textfield" you declare as an instance variable is not the same "textfield" you define in your constructor. And it is the one you declare that gets used in the actionPerformed method (which has never been defined, and so is null, per default).

Member Avatar for starsinthesky

i didn't notice your reply right away. :)

thank you soooooooooooooooooo much!
mwah!

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.