Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing question JPanel

Status
Not open for further replies.

tmoses

MIS
Dec 4, 2001
98
US
Hi, i have an existing applet animation program and i was playing around to see if i can get it to work using the JSwing classes.&nbsp;&nbsp;I extended the JPanel class and mangaged to get all the images i wanted to display.&nbsp;&nbsp;I then tried to make the JPanel class implement runnable.&nbsp;&nbsp;From their i was trying to use the Thread.stop() function to allow a pause in the execution of the JPanel.&nbsp;&nbsp;When i call the pause() function it does not display anything in the program.&nbsp;&nbsp;I am wondering how you would go about getting animation to work in the JPanel and which thread i would have to stop.&nbsp;&nbsp;I am also wondering how do you call repaint from the JPanel.<br>thanks tom<br>the following is my code<br><br>import java.awt.*;<br>import java.awt.event.*;<br>import javax.swing.*;<br><br>class MarinePanel extends JPanel implements Runnable<br>{<br><br>private Image marineback;<br>private Image marinestat;<br>private Image marinegroup;<br>private Image[] marineright = new Image[4];<br>private Image[] marineleft = new Image[4];<br><br>public MarinePanel()<br>{<br>Toolkit kit = Toolkit.getDefaultToolkit();<br>marineback = kit.getImage(&quot;marineback.gif&quot;);<br>marinestat = kit.getImage(&quot;marinestat.gif&quot;);<br>marinegroup = kit.getImage(&quot;marinegroup.gif&quot;);<br><br>marineright[0] = kit.getImage(&quot;marineright1.gif&quot;);<br>marineright[1] = kit.getImage(&quot;marineright2.gif&quot;);<br>marineright[2] = kit.getImage(&quot;marineright3.gif&quot;);<br>marineright[3] = kit.getImage(&quot;marineright4.gif&quot;);<br><br>marineleft[0] = kit.getImage(&quot;marineleft1.gif&quot;);<br>marineleft[1] = kit.getImage(&quot;marineleft2.gif&quot;);<br>marineleft[2] = kit.getImage(&quot;marineleft3.gif&quot;);<br>marineleft[3] = kit.getImage(&quot;marineleft4.gif&quot;);<br><br>MediaTracker tracker = new MediaTracker(this);<br><br>tracker.addImage(marineback,0);<br>tracker.addImage(marinestat,1);<br>tracker.addImage(marinegroup,2);<br>tracker.addImage(marineright[0],3);<br>tracker.addImage(marineright[1],4);<br>tracker.addImage(marineright[2],5);<br>tracker.addImage(marineright[3],6);<br>tracker.addImage(marineleft[0],7);<br>tracker.addImage(marineleft[1],8);<br>tracker.addImage(marineleft[2],9);<br>tracker.addImage(marineleft[3],10);<br><br>try {&nbsp;&nbsp;tracker.waitForAll(); }<br>catch (InterruptedException e) {System.out.println(&quot;Exception: &quot; + e.getMessage()); }<br> <br>}<br><br>public void run()<br>{<br><br><br>}<br><br>void pause(int time)<br> {<br> try {<br> Thread.sleep(time);<br> }&nbsp;&nbsp;catch(InterruptedException e) {}<br> }<br><br>public void walkRight(int screenwidth, int screenheight, Graphics screen)<br>{<br><font color=red><b><i><u>this function is designed to have 4 images move accross the screen, and increment the x position as it moves</font></b></i></u><br><br>int arraycntr = 0,widthcntr = 0;<br><br>for(;widthcntr &lt;= (screenwidth/2); widthcntr +=2)<br> {<br> if (arraycntr == marineright.length)<br> arraycntr = 0;<br><br> screen.drawImage(marineright[arraycntr],widthcntr,screenheight / 2,this);<br><font color=red><b><i><u>/*this is where the program needs to be stopped and the screen repainted, when i try the pause function it completely locks the computer <br>//pause(800);<br> //super.repaint();<br>*/</font></b></i></u> <br> arraycntr++;<br> }<br><br><br><br>}<br><br>public void walkLeft(int screenwidth, int screenheight, Graphics screen)<br>{<br>int arraycntr = 0,widthcntr = screenwidth/2;<br><br><br><br><br>}<br><br><br>public void paintComponent(Graphics screen)<br>{<br>super.paintComponent(screen);<br><br>//this sets the background each time this function is called it will tile any image<br>//this is specifically designed so that if the resolutino size changes so will the background<br>int screenwidth = 0, screenheight = 0, imagewidth = 0, imageheight = 0;<br><br>Toolkit tool = Toolkit.getDefaultToolkit();<br>Dimension dimen = tool.getScreenSize();<br>screenwidth = dimen.width;<br>screenheight = dimen.height;<br><br>while(imageheight &lt;= screenheight)<br> {<br> while(imagewidth &lt;= screenwidth)<br> {<br> screen.drawImage(marineback,imagewidth, imageheight, this);<br> imagewidth += marineback.getWidth(this);<br> }<br> imagewidth =0;<br> imageheight += marineback.getHeight(this);<br> }<br><br>screen.drawImage(marinestat, 0, 0,this);<br>screen.drawImage(marinegroup, screenwidth/2, screenheight/2,this);<br><br>walkRight(screenwidth,screenheight,screen);<br>}<br><br><br><br><br>}<br><br><br>class MarineFrame extends JFrame<br>{<br>public MarineFrame()<br>{<br>setTitle(&quot;Marines from StarCraft&quot;);<br><br>Toolkit tk = Toolkit.getDefaultToolkit();<br>Dimension d = tk.getScreenSize();<br>setSize(d.width,d.height);<br> <br>//this program sets up a normal window but the following code<br>//allows the program to close without forcing the os <br>//to close the prog<br>addWindowListener(new WindowAdapter()<br> {&nbsp;&nbsp;&nbsp;<br> public void windowClosing(WindowEvent e)<br> {<br> System.exit(0);<br> }<br> }<br>);<br><br><br>//this allows you to import a new image and it links it to the icon<br>//image<br>Image img = tk.getImage(&quot;marineleft1.gif&quot;);<br>setIconImage(img);<br><br>Container contentPane = getContentPane();<br>contentPane.add(new MarinePanel());<br>//contentPane.test = 60;<br>}<br><br>}<br><br>class jswingmarine<br>{<br><br><br><br><br>public static void main(String[] args)<br>{<br>JFrame frame = new MarineFrame();<br>frame.show();<br><br>}<br><br>}<br><br>/*<br>this is an attempt to make a simple animation program using various pictures and the swing<br>clases, started march<br>working on a loop to display an image tiled as the background image succes so far but it<br>only tiles one row ? 3/29/00<br><br>*/ <p>moses<br><a href=mailto:tmoses@iname.com>tmoses@iname.com</a><br><a href= </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top