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 05-03-2008, 11:13 PM
Member
 
Join Date: May 2008
Posts: 2
Sephirangel is on a distinguished road
Threading a method
Hey,
I have a method which calculates fractal points and displays them as an Image.
What i would like to know, is how to make the method so that the threads concurrently take it in turns to work out the points themselves in the set efficiently.

Heres the code for the mandelbrot method;

Code:
private void mandelbrot() { // calculate all points int x, y; float h, b, alt = 0.0f; actionOn = false; setCursor(c1); //showStatus("Computing Mandelbrot-Set ..."); for (x = 0; x < x1; x++) { for (y = 0; y < y1; y++) { h = pickColIndex(xStart + xZoom * (double)x, yStart + yZoom * (double)y); // color value if (h != alt) { b = 1.0f - h * h; // brightnes g1.setColor(Color.getHSBColor(h, 0.8f, b)); alt = h; } g1.drawLine(x, y, x + 1, y); } }
Thanks in advance!
Josh

PS: im new to the forums so hello!!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-04-2008, 12:14 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
Synchronize all the public methods of the parent class. We can help more if you post the whole code.
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-04-2008, 12:41 AM
Member
 
Join Date: May 2008
Posts: 2
Sephirangel is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class mdbasic extends JPanel implements MouseListener, MouseMotionListener, Runnable { private final int MAX = 256; // #MaxIterations => infinity! private final double SX = -2.025; // start value real private final double SY = -1.125; // start value imaginary private final double EX = 0.6; // end value real private final double EY = 1.125; // end value imaginary //Mandelbrot-set domain aspect ratio = 2.625/2.25 = 1.167 private static int x1, y1, xs, ys, xe, ye; private static double xStart, yStart, xEnd, yEnd, xZoom, yZoom; private static boolean actionOn, redrawCheck, ready2Run; private static float XOverY; // (dynamic) Aspect ratio of Applet. private Image iBoard; private Graphics g1; private Cursor c1, c2; boolean running = false; //list variables private static int listIndex = 0; //for keeping track of next empty list index (adding/removing items from list) private static int currentIndex = 0; //for keeping track of which list index current picture belongs to private LinkedList positionStore = new LinkedList(); //linked list for storing position history private Iterator it = positionStore.iterator(); public void start() { // all instances must be prepared, constructor //ready2Run = false; ready2Run = true; addMouseListener(this); addMouseMotionListener(this); c1 = new Cursor(Cursor.WAIT_CURSOR); c2 = new Cursor(Cursor.CROSSHAIR_CURSOR); x1 = getSize().width; y1 = getSize().height; XOverY = (float)x1 / (float)y1; iBoard = createImage(x1, y1); g1 = iBoard.getGraphics(); actionOn = false; redrawCheck = false; makeAspectRatio(); xZoom = (xEnd - xStart) / (double)x1; yZoom = (yEnd - yStart) / (double)y1; mandelbrot(); //storing first set of coords for original picture CoordinateHistory coord = new CoordinateHistory(xStart, yStart, xEnd, yEnd, xZoom, yZoom); positionStore.add(listIndex, coord); listIndex++; } public Image returnImage(){ //returns Image to GUI to obtain BufferedImage return iBoard; } public void destroy() { // delete all instances if (ready2Run) { removeMouseListener(this); removeMouseMotionListener(this); iBoard = null; g1 = null; c1 = null; c2 = null; System.gc(); // garbage collection } } // public void stop() {} public void run() { paint(g1); } public void paint(Graphics g) { if(!running){ start(); running = true; update(g); }else{ update(g); } } public void update(Graphics g) { g.drawImage(iBoard, 0, 0, this); if (redrawCheck) { g.setColor(Color.white); if (xs < xe) { if (ys < ye) g.drawRect(xs, ys, (xe - xs), (ye - ys)); else g.drawRect(xs, ye, (xe - xs), (ys - ye)); } else { if (ys < ye) g.drawRect(xe, ys, (xs - xe), (ye - ys)); else g.drawRect(xe, ye, (xs - xe), (ys - ye)); } } } //function to remove past coords from list if new set has been made using cursor public void removePastCoords() { int i = ((positionStore.size()) - listIndex); for(int j = 0; j<i ; j++) { positionStore.removeLast(); } } private void mandelbrot() { // calculate all points int x, y; float h, b, alt = 0.0f; actionOn = false; setCursor(c1); //showStatus("Computing Mandelbrot-Set ..."); for (x = 0; x < x1; x++) { for (y = 0; y < y1; y++) { h = pickColIndex(xStart + xZoom * (double)x, yStart + yZoom * (double)y); // color value if (h != alt) { b = 1.0f - h * h; // brightnes g1.setColor(Color.getHSBColor(h, 0.8f, b)); alt = h; } g1.drawLine(x, y, x + 1, y); } } //showStatus("Mandelbrot-Set completed: left-Drag mouse to zoom in area."); setCursor(c2); actionOn = true; } private void makeAspectRatio() { // reset start values xStart = SX; yStart = SY; xEnd = EX; yEnd = EY; if ((float)((xEnd - xStart) / (yEnd - yStart)) != XOverY ) xStart = xEnd - (yEnd - yStart) * (double)XOverY; } private float pickColIndex(double xwert, double ywert) { // color value from 0.0 to 1.0 by iterations double r = 0.0, i = 0.0, m = 0.0; int j = 0; while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + ywert; r = m + xwert; } return (float)j / (float) MAX; } public void mousePressed(MouseEvent e) { e.consume(); if (actionOn) { xs = e.getX(); ys = e.getY(); } } public void mouseReleased(MouseEvent e) { int z, w; e.consume(); if (actionOn) { xe = e.getX(); ye = e.getY(); if (xs > xe) { z = xs; xs = xe; xe = z; } if (ys > ye) { z = ys; ys = ye; ye = z; } w = (xe - xs); z = (ye - ys); if ((w < 2) && (z < 2)) makeAspectRatio(); else { if (((float)w > (float)z * XOverY)) ye = (int)((float)ys + (float)w / XOverY); else xe = (int)((float)xs + (float)z * XOverY); xEnd = xStart + xZoom * (double)xe; yEnd = yStart + yZoom * (double)ye; xStart += xZoom * (double)xs; yStart += yZoom * (double)ys; } xZoom = (xEnd - xStart) / (double)x1; yZoom = (yEnd - yStart) / (double)y1; CoordinateHistory coord = new CoordinateHistory(xStart, yStart, xEnd, yEnd, xZoom, yZoom); if(it.hasNext()){ removePastCoords(); } positionStore.add(listIndex, coord); listIndex++; currentIndex++; mandelbrot(); redrawCheck = false; repaint(); } } public void mouseDragged(MouseEvent e) { e.consume(); if (actionOn) { xe = e.getX(); ye = e.getY(); redrawCheck = true; repaint(); } } static final long serialVersionUID = 1L; // merely to avoid the (rather annoying!) warning. }
Thats pretty much the whole class, minus a few irrelevent methods.
I was thinking of having the computations of the mandelbrot() method moved into a Thread class and having them output the points to a drawMandelBrot() method that uses g.drawLine but again, im not sure where to start and how to go about returning the values!

thanks in advance
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-05-2008, 08:38 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
First you code doesn't compile, the CoordinateHistory class is missing. As a design suggestion keep your calculating code away from your displaying code. You can for example have threads calculating points or whatever a fractal algorithm needs and then push the results in a queue for a displaying thread.
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
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
Java threading Eranga Advanced Java 2 03-13-2008 06:30 AM
Threading prob.. banie Java Applets 0 02-05-2008 07:30 AM
Explanation bout threading and concurrency? cruxblack New To Java 1 08-10-2007 11:33 AM
question about Multi threading in Java fred Advanced Java 1 07-24-2007 02:55 AM
method not abstract, does not override actionperformed method. Theman New To Java 1 05-08-2007 07:13 AM


All times are GMT +3. The time now is 12:25 PM.


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