Results 1 to 8 of 8
  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default [SOLVED] Can't figure out my thread/awt problem

    Java 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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.

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.
    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 04:32 PM.

  6. #6
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I just figured it out.
    My IP address is 127.0.0.1

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Println won't help because it goes too fast
    If you save the output from println you can view it at your leisure.

Similar Threads

  1. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 10:35 PM
  2. [SOLVED] Thread in GUI Problem
    By terox13 in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-27-2008, 08:32 PM
  3. [SOLVED] HELP! Thread Problem
    By nvidia in forum NetBeans
    Replies: 2
    Last Post: 05-24-2008, 04:56 AM
  4. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •