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 03-25-2008, 12:56 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
Can't get my thread to sleep!
I'm working on a simple JFrame based app that fades in an image using an alpha filter. The code so far:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import static java.lang.Thread.sleep;


public class testAlpha extends JFrame
{
private Image source,result;

private testAlpha()
{
setBounds (0,0,300,325);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
image3screen thescreen = new image3screen();
c.setLayout(new BorderLayout());
c.add(thescreen, BorderLayout.CENTER);
}

public static void main (String args[])
{
testAlpha img1 = new testAlpha();
img1.setVisible(true);
}
}

class image3screen extends JPanel
{
private Image source,result;
private int alphaLevel = 0;
private AlphaFilter af;

public image3screen()
{
Toolkit tkt = Toolkit.getDefaultToolkit();
source = tkt.getImage("rico.jpg");
result = source;
repaint();

alphaLevel = 10;
filterImage();

try
{
sleep(1500);
}
catch (InterruptedException ex) {}

alphaLevel = 25;
filterImage();

try
{
sleep(1500);
}
catch (InterruptedException ex) {}

alphaLevel = 55;
filterImage();
}

private void filterImage()
{
result = createImage(new FilteredImageSource(source.getSource(),new AlphaFilter(alphaLevel)));
repaint();
}

public void paintComponent(Graphics g)
{
g.drawImage(result,0,0,this);
}
}

class AlphaFilter extends RGBImageFilter
{
private int alphaLevel;

public AlphaFilter(int alpha)
{
alphaLevel = alpha;
canFilterIndexColorModel = true;
}

public int filterRGB(int x, int y, int rgb)
{

int alpha = (rgb >> 24) & 0xff;
alpha = (alpha * alphaLevel) / 255;

return ((rgb & 0x00ffffff) | (alpha << 24));
}
}

The main thread goes to sleep, then the application launches. Some help figuring out how to make it repaint, sleep, change Alpha then repaint would be great! Cheers :{p
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-25-2008, 06:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; import static java.lang.Thread.*; public class TestAlpha extends JFrame { private TestAlpha() { setBounds (0,0,300,325); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); Image3screen thescreen = new Image3screen(); c.setLayout(new BorderLayout()); c.add(thescreen, BorderLayout.CENTER); } public static void main (String[] args) { TestAlpha img1 = new TestAlpha(); img1.setVisible(true); } } class Image3screen extends JPanel implements Runnable { private Image source, result; private int alphaLevel = 0; int alphaInc = 10; private AlphaFilter af; Thread thread; boolean filtering = false; public Image3screen() { try { source = ImageIO.read(new File(//"rico.jpg")); "taiChi.jpg")); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } result = source; // repaint(); nothing showing yet/nothing to paint alphaLevel = alphaInc; start(); } private void start() { if(!filtering) { filtering = true; thread = new Thread(this); thread.setPriority(NORM_PRIORITY); thread.start(); } } public void run() { while(filtering) { try { sleep(200); } catch (InterruptedException ex) { System.out.println("interrupted"); filtering = false; } alphaLevel += alphaInc; if(alphaLevel < 10 || alphaLevel > 90) alphaInc *= -1; //System.out.println("alphaLevel = " + alphaLevel); filterImage(); } } private void filterImage() { result = createImage(new FilteredImageSource(source.getSource(), new AlphaFilter(alphaLevel))); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(result,0,0,this); } } class AlphaFilter extends RGBImageFilter { private int alphaLevel; public AlphaFilter(int alpha) { alphaLevel = alpha; canFilterIndexColorModel = true; } public int filterRGB(int x, int y, int rgb) { int alpha = (rgb >> 24) & 0xff; alpha = (alpha * alphaLevel) / 255; return ((rgb & 0x00ffffff) | (alpha << 24)); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-25-2008, 07:14 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
Nice one!
Thanks, the comments were very helpful!
I'll have to have a closer look at the code and play about with it before I will understand it totally. Eventually I'll put it to some good use and make some graphics for my website. Cheers :{p
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
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-24-2008 12:30 AM
How to use sleep method of the Thread class Java Tip java.lang 0 04-09-2008 08:42 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM
How to use the sleep and thread? jiuhu Java Applets 4 08-07-2007 04:56 AM


All times are GMT +3. The time now is 04:37 AM.


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