Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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-03-2008, 08:01 PM
Member
 
Join Date: May 2008
Posts: 22
amith is on a distinguished road
where to add thread in the program
in the above program, the main motive of the program is to keep one image fixed at the bottom (the fixed one is red) and start a thread for the image(the blue one) which u can see the code in the comment lines of the program given below .i just want to keep the fixed image and the other image(blue) in thread so that the two images should be visible to me , my program should look like one image(red) is fixed at the bottom and other image (blue) coming from top to bottom and both should be visible to me but they are not if one is fixed then i can see only that image i cant see the other image could u help out with this code and how to use thread in this program.


import java.util.*;
import java.awt.*;
import javax.swing.*;
class load extends JFrame
{

Image r;
Image b;

int y=329;
int floor=329;
int x=3;
load()
{

super("some name");
r=new ImageIcon("red.png").getImage();
b=new ImageIcon("blue.png").getImage();

getContentPane().add(new display());
setVisible(true);
setSize(175,400);
setResizable(false);
}

class display extends JPanel //implements Runnable
{
//Thread t=new Thread(this);
display()
{
repaint();
}

public void paintComponent(Graphics q)
{
Graphics2D n=(Graphics2D)q;
q.drawImage(r,x,y,this);
}
/*
i just want to place the red image fixed at the bottom
and start new blue image thread i,e
try
{
y=0;
while(y!=floor)
{
y++;
q.drawImage(b,x,y,this);
Thread.sleep(10);
}
}
catch(Exception e)
{
}
*/

}
}
class f
{
public static void main(String args[])
{
new load();
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2008, 08:43 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 864
Norm is on a distinguished road
Suggestions:
Use CODE tags around code that you post to make for easier reading.
Use words for variable names instead of single letters
Naming conventions for Java classes is Uppercase first letter.

Re your logic - Following probably wrong because it is hard to read your code without the indentation showing nesting of {}.
Move the loop out of the paintComponent() method. You should only stay in this method long enough to do some drawing and then exit. Use repaint() to cause the system to call the paintComponent() method. Put the repaint() call following where you compute what/where to draw, probably in a loop inside a thread .

There are several examples of how to use Threads. Use a Search to find some and then put some code in your progam and post it using CODE tags.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2008, 01:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Didn't we just do this.
Code:
import java.awt.*; import javax.swing.*; public class SlidingDownRevisited extends JPanel implements Runnable { Image[] images; int yLoc; public SlidingDownRevisited() { String prefix = "images/geek/geek"; String[] ids = { "-c---", "--g--" }; images = new Image[ids.length]; for(int i = 0; i < images.length; i++) { String path = prefix + ids[i] + ".gif"; images[i] = new ImageIcon(path).getImage(); } yLoc = -images[1].getHeight(this); } protected void paintComponent(Graphics g) { super.paintComponent(g); int x = (getWidth() - images[0].getWidth(this))/2; int y = getHeight() - images[0].getHeight(this); g.drawImage(images[0], x, y, this); g.drawImage(images[1], x, yLoc, this); } public Dimension getPreferredSize() { return new Dimension(300,400); } public void run() { int height = getPreferredSize().height - images[0].getHeight(this); do { yLoc++; repaint(); try { Thread.sleep(100); } catch(InterruptedException e) { break; } } while(yLoc + images[1].getHeight(this) < height); System.out.println("all done"); } private void start() { Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { SlidingDownRevisited test = new SlidingDownRevisited(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.pack(); f.setLocation(200,200); f.setVisible(true); test.start(); } }
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-23-2008 11:30 PM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 08:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 10:31 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +3. The time now is 07:20 PM.


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