Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-29-2008, 01:28 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
[SOLVED] Can't figure out my thread/awt problem
Code:
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.RescaleOp; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Window extends Applet implements Runnable { /** * */ private static final long serialVersionUID = -8255319694373975038L; private static final float MAX_ALPHA = 3; private static final float MIN_ALPHA = -.5f; private float alpha; private float changeOfAlpha; private int wordYPosition; private int wordXPosition; private int wordYSpeed; private int wordXSpeed; private PictureGenerator myImageGenerator; private WordGenerator myWordGenerator; private Thread myThread; private BufferedImage myImage; private Image doubleImage; private Graphics doubleGraphic; private boolean isDrawingPicture; private float[] scales = { 1f, 1f, 1f, 0.5f }; private float[] offsets = new float[4]; private RescaleOp rop; public void init() { this.setSize(400, 300); this.setBackground(Color.WHITE); alpha = MAX_ALPHA; changeOfAlpha = -.001f; isDrawingPicture = true; myImageGenerator = new PictureGenerator(); myWordGenerator = new WordGenerator(); wordXPosition = 0; wordYPosition = (int)((Math.random() * 200) + 100); wordXSpeed = 1; wordYSpeed = 1; this.setupImage(); this.start(); } public void start(){ myThread = new Thread(this); myThread.start(); } public void stop() { myThread = null; } public void run() { while(true) { while(isDrawingPicture) { try { Thread.sleep(10); if(alpha < MIN_ALPHA) { this.setupImage(); changeOfAlpha = changeOfAlpha * -1; isDrawingPicture = false; alpha = -.4f; } else if(alpha > MAX_ALPHA) { changeOfAlpha = changeOfAlpha * -1; } if(isDrawingPicture) { alpha += changeOfAlpha; setOpacity(alpha); this.repaint(); } } catch (InterruptedException e) { e.printStackTrace(); } } while(!isDrawingPicture) { try { Thread.sleep(10); this.checkWordPosition(); if(!isDrawingPicture) { wordXPosition += wordXSpeed; wordYPosition += wordYSpeed; this.repaint(); } } catch (InterruptedException e) { e.printStackTrace(); } } } } private void checkWordPosition() { if(wordXPosition > 400) { this.resetWord(); } if(wordYPosition > 300) { wordYSpeed = wordYSpeed * -1; } else if(wordYPosition < 0) { wordYSpeed = wordYSpeed * -1; } } public void update(Graphics g) { if (doubleImage == null) { doubleImage = createImage (this.getSize().width, this.getSize().height); doubleGraphic = doubleImage.getGraphics (); } // clear screen in background doubleGraphic.setColor (getBackground ()); doubleGraphic.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background doubleGraphic.setColor (getForeground()); paint (doubleGraphic); // draw image on the screen g.drawImage (doubleImage, 0, 0, this); } public void paint(Graphics g) { if(isDrawingPicture) { Graphics2D g2d = (Graphics2D)g; g2d.drawImage(myImage, rop, 0, 0); } else { g.drawString("Hello World", wordXPosition, wordYPosition); } } public void resetWord() { wordXPosition = 0; wordYPosition = (int)((Math.random() * 200) + 100); isDrawingPicture = true; } private void setupImage() { try { BufferedImage tempImage = ImageIO.read(new File(myImageGenerator.getNewPicture())); int width = tempImage.getWidth(null); int height = tempImage.getHeight(null); myImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics tempGraphics = myImage.getGraphics(); tempGraphics.drawImage(tempImage, 0, 0, null); } catch (IOException e) { System.out.println("Image could not be read"); System.exit(1); } this.setOpacity(alpha); } private void setOpacity(float opacity) { scales[3] = opacity; rop = new RescaleOp(scales, offsets, null); } }
It keeps restarting the word sequence a few time some of the time before switching back to the picture.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-29-2008, 03:28 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
What's it supposed to do?
Does it ouput anything that you could copy here to show what you mean?
A way to debug code is to add println() statements to show what and where on the console.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-29-2008, 05:04 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 462
fishtoprecords is on a distinguished road
Plus, threading is complicated, so simplify your example code until you get the threading working, putting out the System.out.println stuff that @norm says. Once that works, then plug it into the GUI.

BTW, why AWT instead of Swing? AWT has been obsolete for nearly a decade.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-29-2008, 04:43 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I know how to debug code. The problem is I can't figure out why it's doing what it's doing. I think it has something to do with the thread. Btw swing uses awt and if you do any animaition based graphics you have to use awt. So I fail to see how awt is obsolete. Also swing hasn't been out that long. Also swing is slow and ugly looking and is only good for quick GUI work.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-29-2008, 06:17 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
I can't figure out why it's doing what it's doing.
Using println() should show you where the code is going and how the values of the variables change. Given that you should be able to see what's wrong with your code.
Quote:
It keeps restarting the word sequence
What values of the variables in your code would allow this to happen?

Also what is your program supposed to do? I can't find any comments to explain its purpose.

Last edited by Norm : 07-29-2008 at 06:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-29-2008, 06:33 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Nothing It works fine in debug when I go line by line but when I let it run normally it stops working correctly. I'm not sure if there is an easy way to do this. Println won't help because it goes too fast to tell what its doing.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-29-2008, 06:41 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I just figured it out.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-29-2008, 07:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
Println won't help because it goes too fast
If you save the output from println you can view it at your leisure.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Thread in GUI Problem terox13 Threads and Synchronization 3 05-27-2008 10:32 PM
[SOLVED] HELP! Thread Problem nvidia NetBeans 4 05-24-2008 06:57 AM
Animation Delay - Thread problem wererabit Advanced Java 1 04-17-2008 10:41 AM
Problem using thread +rmi in my homework IbrahimAbbas Threads and Synchronization 10 04-14-2008 11:24 PM
I can't figure this out silvia New To Java 3 07-20-2007 06:38 AM


All times are GMT +3. The time now is 04:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org