Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Applet issue where I am trying to scroll the word you in 10 different lines

877298
Member Posts: 4
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
-
874295 wrote:And we're having an issue reading it. Could you please use '
I am having an issue on this code...' tags? You can find out how in the FAQ pages or in the Welcome Thread at the top of the topic. Winston
-
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.
-
Thanks for the valuable hint!
Shamik
Edited by: 874295 on Jul 22, 2011 12:01 AM -
New To Java & applets are usually a disastrous mix. Start with making apps. based around frames, specifically a <tt>JFrame</tt> in this millennium.
-
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>
-
/*<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 -
..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.
-
/*<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.