Results 1 to 4 of 4
  1. #1
    Sephirangel is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default 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;

    Java 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!!!!

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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

  3. #3
    Sephirangel is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    Java 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

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    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

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Java threading
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2008, 05:30 AM
  3. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM
  4. Explanation bout threading and concurrency?
    By cruxblack in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 10:33 AM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 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
  •