import javax.swing.*;
import javax.swing.event.*;
import java.util.*;   
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.geom.*;

class proj  extends JFrame  implements  ChangeListener ,Runnable,ActionListener   //main class
{   
//           Declarations

    public int bottom;
    public Thread t;
    int padding = 4;
     JFrame f;
     JTabbedPane jt ,jt1;
    JPanel jp,jp1,jp2,jp3,jp4;
     JButton j,j1,j2;
    JLabel lab,lab1;
    JComboBox jcb,jcb1;
    //public JMenuBar jb;
    JMenuItem ji;JMenu jm;
    JColorChooser jc,jc1;
     Color col1,c2;
    public Graphics g;
    public Graphics g1;
    public Graphics2D g2d;
    JDialog dialog;
    String k;
    Line2D shape1;

// Coding
    public void run()
    {

     f = new JFrame("Graph");
    pan jp = new pan();
    pan jp1 = new pan(); ;




        jt = new JTabbedPane(JTabbedPane.TOP);
        jt.setTabPlacement(JTabbedPane.TOP);


                //panel1
            jp.setPreferredSize(new Dimension (50,50));
            jp.setLayout(new BorderLayout());
            jp.setBackground(Color.gray);
            jp.setPreferredSize(new Dimension(60,50));  

            jp4 = new JPanel();

            j = new JButton("Draw the graph");
            j.addActionListener(this);
            jp4.setBorder(BorderFactory.createRaisedBevelBorder());
            jp4.setPreferredSize(new Dimension(130,20));
            jp4.add(j);

            jp.add(jp4,BorderLayout.EAST);  
            String[] a = {"select  "};
            lab = new JLabel("x parameters");
            lab.setLocation(700,700);
            jp4.add(lab);
            jcb= new JComboBox(a);
            jp4.add(jcb);
            lab1 = new JLabel("Y parameters");
            jp4.add(lab1);
            jcb1= new JComboBox(a);
            jp4.add(jcb1);
            jt.addTab("graph1", jp);

        //panel2

        jp1.setBackground(Color.black); 
        jt.addTab("Graph2",jp1);

        JPanel jp3=new JPanel();

        //Colorchooser
        jc = new JColorChooser(Color.yellow);
        jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
        jc.getSelectionModel().addChangeListener(this);
        jp3.add(jc);
        jt.addTab("Change Grid Color", jp3);
        f.add(jt);
        f.pack();
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
        f.setSize(400,400);
        f.setVisible(true);
                }

        public void stateChanged(ChangeEvent e)
        {

        }


        public void actionPerformed(ActionEvent e)
            {   

              if((JButton)e.getSource() == j)
              {
            /*jc1 = new JColorChooser();
             JColorChooser.showDialog(jp4,"Select The Color", Color.red);
            c2=jc1.getColor();*/

            }
        }       

  //MAIN

    public static void main(String[]args)

    {
        proj lp = new proj();
        Thread t = new Thread(lp);
        t.start();
    }   

    public class pan extends JPanel
    {   
        Graphics g; 
             public void paintComponent(Graphics g)
               {    
               super.paintComponent(g);
            Color col1=jc.getColor();
            g.setColor(col1);
            g.drawRect( 30,30,800,600);
            for(int i=30;i<=620; i=i+20)
            {   
            g.drawLine(30,i,830,i);       //            }
            g.setColor(Color.white);

            for( Integer i=10;i<=620; i=i+20)
            {
                k=Integer.toString(i);
            g.drawString(k,5,i+27);

            }
            for(Integer i=10;i<=820; i=i+20)
            {
                int l = i/10;
            g.drawString(Integer.toString(l),i+17,650);

            }
            g.setColor(col1);
            for(int i =0;i<=800;i=i+20)
            g.drawLine(i+30,30,i+30,630);                       
            }
    }



}

This is my proj code...........so my requirement is that when a user presses the button (i.e strat graph) a line shud been plot on the interface.outside pan class am not able to acess g,,,so as to how can i accomplish this........plz reply....help

Recommended Answers

All 16 Replies

You need to have references to your "pan" class in the global field scope.

From there, after your JFrame is instantiated, make a call to the "pan" classes repaint method in your actionListener.

From there your button should call paint. All you need to do from there is make the pan class paint a line on the screen when this happens.

Hopefully this information is helpful to you.

no sir i could not get u ,,,,,,,,,,can u modify my code n post it

no sir i could not get u ,,,,,,,,,,can u modify my code n post it

I'll make the modification if you first place your code in code tags.

tried but geting nuu pointer exception

ya,,,,,i"ll write code tags....next when i post the code...now its very urgent,,,,need show this code 2day plz,,modify

no sir i could not get u ,,,,,,,,,,can u modify my code n post it

Excellent way to get everyone mad at you and not get any more help. This sentence suggests that you expect others to put in more effort than you are willing to to solve YOUR assignment. You don't explain what you didn't understand about Alex's post and you write a single poorly written sentence asking for someone else to do all the work.

ya,,,,,i"ll write code tags....next when i post the code...now its very urgent,,,,need show this code 2day plz,,modify

Well, if it's urgent, format it, post it using code tags, and write in real sentences.

o plz...am srtuggling here.........
to modify my code....no if u help i can understand my problem....anyways am not asking othersput efforts,,,,,,coz from 3days am struggling with this prob and seriously want to know where am wrong...am trying to code according to suugestion given by alex,,,but ending up with null ponter exception

o plz...am srtuggling here.........
to modify my code....no if u help i can understand my problem....anyways am not asking othersput efforts,,,,,,coz from 3days am struggling with this prob and seriously want to know where am wrong...am trying to code according to suugestion given by alex,,,but ending up with null ponter exception

I'll repeat. Don't use plz, u, coz, etc. It's frowned upon here. Post your code in code tags:

[code=JAVA] // paste code

[/code]

Make sure the code is formatted, point out the line number of this null pointer exception and the circumstances where it occurs.

At the line where you get the NullPointerException you are probably trying to use an object that is null and instantiated. See what the message says and you will find which object is that. Then instantiated correctly based on what is the object

And no one is going to read you entire code, to find one single mistake, when you can simply post the line and the message you get from the console.

Z:\Program Files\Java\jdk1.6.0\bin>java proj
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at proj.actionPerformed(proj.java:111)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

import javax.swing.*;
import javax.swing.event.*;
import java.util.*;   
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.geom.*;

class proj  extends JFrame  implements  ChangeListener ,Runnable,ActionListener   {	

	
	public 	int bottom;
	public	 Thread t;
		int padding = 4;
		 JFrame f;
		 JTabbedPane jt ,jt1;
		JPanel jp,jp1,jp2,jp3,jp4;
		 JButton j,j1,j2;
		JLabel lab,lab1;
		JComboBox jcb,jcb1;
		JColorChooser jc,jc1;
		 Color col1,c2;
	public	 Graphics g;
	public	 Graphics g1;
	public	 Graphics2D g2d;
	             JDialog  dialog;
	String	 k;
	Line2D	 shape1;
	f p1 = new f();
	

		public void run()
		{
	
			 f = new JFrame("Graph");
			pan jp = new pan();
			pan jp1 = new pan(); ;
		

			jt = new JTabbedPane(JTabbedPane.TOP);
			jt.setTabPlacement(JTabbedPane.TOP);
		 
		
	       	jp.setPreferredSize(new Dimension (50,50));
			jp.setLayout(new BorderLayout());
			jp.setBackground(Color.gray);
			jp.setPreferredSize(new Dimension(60,50));	
		
			jp4 = new JPanel();
			j = new JButton("Draw the graph");
			j.addActionListener(this);
			jp4.setBorder(BorderFactory.createRaisedBevelBorder());
			jp4.setPreferredSize(new Dimension(130,20));
			jp4.add(j);
		
			jp.add(jp4,BorderLayout.EAST);	
			String[] a = {"select  "};
			lab = new JLabel("x parameters");
			lab.setLocation(700,700);
			jp4.add(lab);
			jcb= new JComboBox(a);
			jp4.add(jcb);
			lab1 = new JLabel("Y parameters");
			jp4.add(lab1);
			jcb1= new JComboBox(a);
			jp4.add(jcb1);
			jt.addTab("graph1", jp);
			
		
		
			jp1.setBackground(Color.black);	
			jt.addTab("Graph2",jp1);
			JPanel jp3=new JPanel();

		
			jc = new JColorChooser(Color.yellow);
			jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
			jc.getSelectionModel().addChangeListener(this);
			jp3.add(jc);
			jt.addTab("Change Grid Color", jp3);
			f.add(jt);
			f.pack();
			f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
			f.setSize(400,400);
			f.setVisible(true);
	           	 }
		
		public void stateChanged(ChangeEvent e)
		{
			
		}

		
		public void actionPerformed(ActionEvent e)
		{	
		 	
			  if((JButton)e.getSource() == j)
		 	  {
			/*jc1 = new JColorChooser();
			 JColorChooser.showDialog(jp4,"Select The Color", Color.red);
			c2=jc1.getColor();*/
			
			p1.repaint();
			
			
		   	 }
		}		

  //MAIN
		
		public static void main(String[]args)
	
		{
			proj lp = new proj();
			Thread t = new Thread(lp);
			t.start();
		}	

		public class pan extends JPanel
		{	
			Graphics g;	
			 public void paintComponent(Graphics g)
			  {	
			   	super.paintComponent(g);
				Color col1=jc.getColor();
				g.setColor(col1);
				g.drawRect( 30,30,800,600);//600 = width 400 = height
				for(int i=30;i<=620; i=i+20)
				{	
					g.drawLine(30,i,830,i);       				}
				g.setColor(Color.white);
			
				for( Integer i=10;i<=620; i=i+20)
				{
					k=Integer.toString(i);
					g.drawString(k,5,i+27);
			
				}
				for(Integer i=10;i<=820; i=i+20)
				{
					int l = i/10;
					g.drawString(Integer.toString(l),i+17,650);
				}
				g.setColor(col1);
				for(int i =0;i<=800;i=i+20)
				g.drawLine(i+30,30,i+30,630);            			     }
			
		}	
}

error when button is clicked

First of all we cannot count 111 lines. Post the part of the code that is around line 111, stating which one is line 111.

Also there is a symbol: '#' above this posting form. Press it. Then in the first tag add this:
=JAVA, no empty spaces.
Then post your code between these tags

import javax.swing.*;
import javax.swing.event.*;
import java.util.*;   
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.geom.*;

class proj  extends JFrame  implements  ChangeListener ,Runnable,ActionListener   {	

	
	public 	int bottom;
	public	 Thread t;
		int padding = 4;
		 JFrame f;
		 JTabbedPane jt ,jt1;
		JPanel jp,jp1,jp2,jp3,jp4;
		 JButton j,j1,j2;
		JLabel lab,lab1;
		JComboBox jcb,jcb1;
		JColorChooser jc,jc1;
		 Color col1,c2;
	public	 Graphics g;
	public	 Graphics g1;
	public	 Graphics2D g2d;
	             JDialog  dialog;
	String	 k;
	Line2D	 shape1;
	f p1 = new f();
	

		public void run()
		{
	
			 f = new JFrame("Graph");
			pan jp = new pan();
			pan jp1 = new pan(); ;
		

			jt = new JTabbedPane(JTabbedPane.TOP);
			jt.setTabPlacement(JTabbedPane.TOP);
		 
		
	       	jp.setPreferredSize(new Dimension (50,50));
			jp.setLayout(new BorderLayout());
			jp.setBackground(Color.gray);
			jp.setPreferredSize(new Dimension(60,50));	
		
			jp4 = new JPanel();
			j = new JButton("Draw the graph");
			j.addActionListener(this);
			jp4.setBorder(BorderFactory.createRaisedBevelBorder());
			jp4.setPreferredSize(new Dimension(130,20));
			jp4.add(j);
		
			jp.add(jp4,BorderLayout.EAST);	
			String[] a = {"select  "};
			lab = new JLabel("x parameters");
			lab.setLocation(700,700);
			jp4.add(lab);
			jcb= new JComboBox(a);
			jp4.add(jcb);
			lab1 = new JLabel("Y parameters");
			jp4.add(lab1);
			jcb1= new JComboBox(a);
			jp4.add(jcb1);
			jt.addTab("graph1", jp);
			
		
		
			jp1.setBackground(Color.black);	
			jt.addTab("Graph2",jp1);
			JPanel jp3=new JPanel();

		
			jc = new JColorChooser(Color.yellow);
			jc.createDialog(jp3,"Choose Background Color",true, jc, null,null);
			jc.getSelectionModel().addChangeListener(this);
			jp3.add(jc);
			jt.addTab("Change Grid Color", jp3);
			f.add(jt);
			f.pack();
			f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
			f.setSize(400,400);
			f.setVisible(true);
	           	 }
		
		public void stateChanged(ChangeEvent e)
		{
			
		}

		
		public void actionPerformed(ActionEvent e)
		{	
		 	
			  if((JButton)e.getSource() == j)
		 	  {
			/*jc1 = new JColorChooser();
			 JColorChooser.showDialog(jp4,"Select The Color", Color.red);
			c2=jc1.getColor();*/
			
			p1.repaint();
			
			
		   	 }
		}		

  //MAIN
		
		public static void main(String[]args)
	
		{
			proj lp = new proj();
			Thread t = new Thread(lp);
			t.start();
		}	

		public class pan extends JPanel
		{	
			Graphics g;	
			 public void paintComponent(Graphics g)
			  {	
			   	super.paintComponent(g);
				Color col1=jc.getColor();
				g.setColor(col1);
				g.drawRect( 30,30,800,600);//600 = width 400 = height
				for(int i=30;i<=620; i=i+20)
				{	
					g.drawLine(30,i,830,i);       				}
				g.setColor(Color.white);
			
				for( Integer i=10;i<=620; i=i+20)
				{
					k=Integer.toString(i);
					g.drawString(k,5,i+27);
			
				}
				for(Integer i=10;i<=820; i=i+20)
				{
					int l = i/10;
					g.drawString(Integer.toString(l),i+17,650);
				}
				g.setColor(col1);
				for(int i =0;i<=800;i=i+20)
				g.drawLine(i+30,30,i+30,630);            			     }
			
		}	
}

And where do you get the error, because at line 111 I see only:

public static void main(String[]args)

And besides if you look at the code:

public void actionPerformed(ActionEvent e)
{
 if((JButton)e.getSource() == j)
 {
 /*jc1 = new JColorChooser();
 JColorChooser.showDialog(jp4,"Select The Color", Color.red);
 c2=jc1.getColor();*/
 p1.repaint();
 }
}

You have most of it comment it out, meaning that nothing will happen when you click the button.
By the way, the repaint() method, which I believe belongs to the class:
f p1 = new f();
You didn't post code for that class, and it is the only thing you call inside the actionPerformed method

As Java Addict mentioned, we can't run it without the f class. You took his "No empty spaces" too literally. Don't put that in the brackets. What he meant was that you did something like this before:

[ code = JAVA ] // paste code here

[/code]

rather than this:

[code=JAVA] // paste code here

[/code]

There are no spaces in the second group of code tags and that's the way you want it. Hit the "Preview" button before you post. If you don't get code tags with color highlighting, check back with the first code tag to make sure it's right and make sure you remembered to put the ending code tag too.

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.