Forum Stats

  • 3,826,113 Users
  • 2,260,598 Discussions
  • 7,896,790 Comments

Discussions

Applet issue where I am trying to scroll the word you in 10 different lines

877298
877298 Member Posts: 4
edited Jul 22, 2011 12:14PM in New To Java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*<applet code="Games.class" width=400 height=200>
</applet>*/

public class Games extends Applet implements Runnable,ActionListener
{
 Panel p;
 int Xpos=0;
 int i=0;
 Button b1,b2;
 Thread t[]=new Thread[10];  
 Label label;
  public void init()
    {
        p = new Panel();
        
        label=new Label("Car Racing");
         b1=new Button("Start");
         b2=new Button("Stop");
         setLayout( new BorderLayout());
        add(p);
	p.add(label,BorderLayout.PAGE_START);
        p.add(b1,BorderLayout.BEFORE_LINE_BEGINS);
        p.add(b2,BorderLayout.LINE_START);
        b1.setVisible(true);
        b2.setVisible(true);
       
          b1.addActionListener(this);
          b2.addActionListener(this);
    }

 public void actionPerformed(ActionEvent ae)
    {
     if(ae.getSource()==b1)
        {
       
       for(i=0;i<10;i++)
          {
               if (t[i] == null)
                   t<em>.start();<br />
<br />
	}<br />
}<br />
else if(ae.getSource()==b2)<br />
	{<br />
	for(i=0;i<10;i++)<br />
{<br />
if(t[i] != null)<br />
t[i]=null;<br />
		}<br />
}<br />
<br />
}<br />
<br />
public void start(Graphics g)<br />
{<br />
paint(g);<br />
}<br />
public void paint(Graphics g)<br />
{  <br />
for(int j=40;j<200;j=j+20)<br />
{<br />
g.drawLine(20,j,400,j);<br />
<br />
g.drawString("You",Xpos,j);<br />
<br />
if(Xpos>40 || Xpos<=400)<br />
{<br />
Xpos++;<br />
}<br />
if(Xpos>400 || Xpos<=0)<br />
{<br />
Xpos=40;<br />
}<br />
<br />
}<br />
}<br />
public void run()<br />
{<br />
try<br />
{<br />
repaint();<br />
for(i=0;i<10;i++)<br />
t[i].sleep(10);<br />
<br />
}<br />
catch(InterruptedException e)<br />
{<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
}<pre class="jive-pre"><code class="jive-code">-----------------------------------------------------
I am having an issue on this code...gives null pointer exception

Edited by: 874295 on Jul 21, 2011 11:59 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Tagged:

Answers

  • YoungWinston
    YoungWinston Member Posts: 4,310
    edited Jul 22, 2011 2:52AM
    874295 wrote:
    I am having an issue on this code...
    And we're having an issue reading it. Could you please use '
    ' tags? You can find out how in the FAQ pages or in the Welcome Thread at the top of the topic.
    
    Winston                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    YoungWinston
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Well, the stacktrace will tell you which line is throwing the null pointer. Go to that line and make sure that you're not trying to use null for something.
  • 877298
    877298 Member Posts: 4
    edited Jul 22, 2011 3:01AM
    Thanks for the valuable hint!

    Shamik

    Edited by: 874295 on Jul 22, 2011 12:01 AM
  • 793415
    793415 Member Posts: 7,279 Bronze Badge
    edited Jul 22, 2011 3:02AM
    New To Java & applets are usually a disastrous mix. Start with making apps. based around frames, specifically a <tt>JFrame</tt> in this millennium.
  • 793415
    793415 Member Posts: 7,279 Bronze Badge
    edited Jul 22, 2011 8:31AM
    Some notes:
    <ol>
    <li>I don't know why you think you need *10* <tt>Thread</tt> instances. One should be enough.
    <li>The panel containing the components is filling the content area of the applet, so the custom rendering cannot ever be seen.
    <li>It is best to render in a container like <tt>Panel</tt> or <tt>JPanel</tt> or <tt>JComponent</tt> then add that container to the top-level container (e.g. applet or frame).
    <li>Using Swing it is easier to get animation by using a <tt>javax.swing.Timer</tt>.
    <li>The <tt>start(Graphics)</tt> method seems to be redundant.
    <li>My earlier comment re mixing New To Java & applets. Now I realize there is also custom rendering & animation involved, raise that conclusion an order of magnitude. Start with something simpler!
    <li>Good call on formatting the code.
    <li>Please don't mix tabs and spaces in posted source. 2-4 space indent for each code block level is generally considered the best. But whatever you choose, keep it consistent. (A)
    </ol>

    A) This is the source, reformatted with 4 spaces (+ a few odd changes I made, prior to posting).
    /*<applet code="Games.class" width=400 height=200>
    </applet>*/
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Games extends Applet implements Runnable,ActionListener
    {
        Panel p;
        int Xpos=0;
        int i=0;
        Button b1,b2;
        Thread t[]=new Thread[10];
        Label label;
    
        public void init()
        {
            p = new Panel();
    
            label=new Label("Car Racing");
            b1=new Button("Start");
            b2=new Button("Stop");
            setLayout( new BorderLayout());
            //add(p);
            p.add(label,BorderLayout.PAGE_START);
            p.add(b1,BorderLayout.BEFORE_LINE_BEGINS);
            p.add(b2,BorderLayout.LINE_START);
            b1.setVisible(true);
            b2.setVisible(true);
    
            b1.addActionListener(this);
            b2.addActionListener(this);
            repaint();
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==b1)
            {
    
                for(i=0;i<10;i++)
                {
                    if (t[i] == null)
                    {
                        t[i] = new Thread(this);
                        t<em>.start();<br />
    }<br />
    <br />
    }<br />
    }<br />
    else if(ae.getSource()==b2)<br />
    {<br />
    for(i=0;i<10;i++)<br />
    {<br />
    if(t[i] != null)<br />
    t[i]=null;<br />
    }<br />
    }<br />
    }<br />
    <br />
    /*<br />
    public void start(Graphics g)<br />
    {<br />
    paint(g);<br />
    }<br />
    */<br />
    <br />
    public void run()<br />
    {<br />
    try<br />
    {<br />
    repaint();<br />
    for(i=0;i<10;i++)<br />
    t[i].sleep(10);<br />
    }<br />
    catch(InterruptedException e)<br />
    {<br />
    e.printStackTrace();<br />
    }<br />
    }<br />
    }<br />
    <br />
    <br />
    Edited by: Andrew Thompson on Jul 22, 2011 5:35 PM<br />
    <br />
    Edited by: Andrew Thompson on Jul 22, 2011 10:30 PM </em>
  • 877298
    877298 Member Posts: 4
    /*<applet code="Games.class" width=400 height=200>
    </applet>*/
     
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Games extends Applet implements Runnable,ActionListener
    {
        Panel p;
        int Xpos=20;
        int i=0;
        Button b1,b2;
        Thread t=new Thread();
        Label label;
     
        public void init()
        {
            p = new Panel();
     
            label=new Label("Car Racing");
            b1=new Button("Start");
            b2=new Button("Stop");
            setLayout( new BorderLayout());
            add(p);
            p.add(label,BorderLayout.PAGE_START);
            p.add(b1,BorderLayout.BEFORE_LINE_BEGINS);
            p.add(b2,BorderLayout.LINE_START);
            b1.setVisible(true);
            b2.setVisible(true);
     
            b1.addActionListener(this);
            b2.addActionListener(this);
            repaint();
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==b1)
            {        if (t == null)
                    {
                        t = new Thread(this);
                        t.run();
                    }
     
               
            }
            else if(ae.getSource()==b2)
              {   if(t!= null)
    	  t=null;
                 }
        }
     public void paint(Graphics g)
    {  
      for(int j=20;j<200;j=j+20)
      {
        g.drawLine(20,j,400,j);
    
      g.drawString("You",Xpos,j);
      
       if(Xpos>40 || Xpos<=400)
        {
         Xpos++;
         }
        if(Xpos>400 || Xpos<=0)
        {
          Xpos=40;
         }
     }
    }
     
        public void run()
        {
            try
            {
                      repaint();
                       t.sleep(10);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    ---------------------------------------
    Hi Andrew,
    When I change it to one thread, and try to implement it then also it does not run properly.Can you advice.
    Regards,
    Shamik
  • 793415
    793415 Member Posts: 7,279 Bronze Badge
    ..When I change it to one thread, and try to implement it then also it does not run properly.
    I edited my answer from an unordered list to an ordered list. That clearly demonstrates that it has 8 points, of which you have acted on 1.
    Can you advice.
    I advise you to act on points 2-8.
  • 877298
    877298 Member Posts: 4
    /*<applet code="Game.class" width=400 height=200>
    </applet>*/
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Game extends Applet implements Runnable,ActionListener
    {
     Thread t;
      int i=0,j=0;
       Button b1,b2;
    	Label label;
    int Xpos=0; 
    public void init()
        {
    	t=new Thread();     
     label=new Label("Car Racing");
         b1=new Button("Start");
          b1.setBounds(150,180,50,20);
          b2=new Button("Stop");
          b2.setBounds(220,180,50,20);
             setLayout(null);
    	//b1.setVisible(true);
    	//b2.setVisible(true);
             add(label);
             add(b1);
             add(b2);
             b1.addActionListener(this);
             b2.addActionListener(this);
     
    }
         
     
     public void actionPerformed(ActionEvent ae)
        {
         if(ae.getSource()==b1)
            {
                
            if (t == null)
            {
              t=new Thread(this); 
              t.start();        
            }
         }
    else if(ae.getSource()==b2)
            {
                if(t!=null)     
                  t = null;
              }
        }
    
    public void paint(Graphics g)
    {
      for(int j=0;j<200;j=j+20)
      {
        g.setColor(Color.cyan);
        g.drawLine(0,j,400,j);
       g.setColor(Color.black);
      g.drawString("You",Xpos,j);
        if(Xpos>=0 && Xpos<=400)
          {
         Xpos++;
         }
        if(Xpos>400 || Xpos<0)
        {
          Xpos=0;
         }
     }  
     
    }
    
    public void run()
        {
        while(true)
        {
          repaint();
       try
       {
            t.sleep(100);
               
        }
       catch(InterruptedException e1)
       {
           e1.printStackTrace();
       }
            
    }
    }
    }
    -------------------------------------------
    Hi Andrew,
    Thanks for your help. I solved it myself.
    Regards,
    Shamik
This discussion has been closed.