Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Double in drawLine()

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Double in drawLine()

    I am trying to make an applet that describes a predator and prey model and I came across a troublesome situation. I have extended Applet, now my problem is trying to draw a line that incorporates my numerical solution, but the problem is that drawLine() takes in only integer values but my numerical solution are doubles. The numerical solution interval is from 4.7 to 5.0. How can I get my draw line to take in doubles?

    What I have imported, extended, and implemented is:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    	public class PreyApplet extends Applet
    	implements MouseListener, ActionListener, Runnable{
    ...
    }
    My numerical solution is:

    Java Code:
    public class Model {
        
            double[] prey = new double[1000];
            double[] predator = new double[1000];
            double[] dt = new double[101];
            double[] b = new double[101];
            prey[0] = 10;
            predator[0] = 5;  
            dt[0] = 0;
            b[0] = 0.01;  
      
            for (int j = 1; j < 101; j ++) {
                dt[j] = 2 + dt[j-1];
                b[j] = b[0]*((double)j/2); 
            }   
      
            for(int i = 1; i < 100; i++) {
                prey[i] = prey[i-1] + (0.2*prey[i-1]*(1 - prey[i-1] / 500)
                   -(b[i]*prey[i-1]*predator[i-1])/(1+b[i]*prey[i-1])) * (1/dt[i]);
                predator[i] = predator[i-1] + (0.1*predator[i-1]*(1-predator[i-1]
                    /(0.2*prey[i-1])))*(1/dt[i]);
                //System.out.println("" + prey[i] + " " + predator[i]); 
            }     
        }     
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Double in drawLine()

    drawLine() works with pixels (and so only ints make sense). One thing you could do is use a bigger scale. So for instance the range 4.7->5.0 could involve pixels 470->500.

  3. #3
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Double in drawLine()

    Quote Originally Posted by pbrockway2 View Post
    drawLine() works with pixels (and so only ints make sense). One thing you could do is use a bigger scale. So for instance the range 4.7->5.0 could involve pixels 470->500.
    Hm, I see, but what if I need the values up to four decimal places, i.e. 10.0045. Because I have values 10.005587, 10.00735, ...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Double in drawLine()

    Quote Originally Posted by Robben View Post
    Hm, I see, but what if I need the values up to four decimal places, i.e. 10.0045. Because I have values 10.005587, 10.00735, ...
    If the total range is 10.0045->10.00735 then could put the origin at 10 and use a 10,000x scale so the range goes from pixel 450 to pixel 735.

    Where things get really tricky is where the range itself has four significant figures (where "significant" means you want to be able to distinguish on the monitor values that differ in the least significant place). In that case you have to buy a really big monitor...

    More seriously, in that case you ought to think hard about the user interface and give the user the ability to scroll and zoom at will because you only have c1000 pixels on which to display the different values.

  5. #5
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Double in drawLine()

    Quote Originally Posted by pbrockway2 View Post
    If the total range is 10.0045->10.00735 then could put the origin at 10 and use a 10,000x scale so the range goes from pixel 450 to pixel 735.

    Where things get really tricky is where the range itself has four significant figures (where "significant" means you want to be able to distinguish on the monitor values that differ in the least significant place). In that case you have to buy a really big monitor...

    More seriously, in that case you ought to think hard about the user interface and give the user the ability to scroll and zoom at will because you only have c1000 pixels on which to display the different values.
    That is quite the predicament. I have much studying to do in order to figure this one out. I guess I have to change my numerical solution somehow to fit into the screen then. I have much respect for programmers now, this is pretty hard stuff.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Double in drawLine()

    Look up these classes/interfaces: Shape, Line2D.Double, Graphics2D, RenderingHints.

    Since Java 1.1 or 1.2, all Graphics references are actually instances of Graphics2D, and can be cast as such. Simulation of sub-pixel resolution is achieved with RenderingHints.KEY_RENDERING / VALUE_ANTIALIAS_ON

    db
    Robben likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Double in drawLine()

    Moved from New to Java to Java 2D.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. drawLine - inconsistent endpoint
    By buffalo in forum Advanced Java
    Replies: 5
    Last Post: 04-27-2011, 01:43 AM
  2. problem with drawline using arrow key
    By shazlin in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 01:12 AM
  3. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  4. drawing ellipse by drawline method?
    By hopey in forum Java 2D
    Replies: 8
    Last Post: 04-18-2009, 11:52 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 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
  •