1. Member
Join Date
Feb 2015
Posts
67
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## 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. Member
Join Date
Feb 2015
Posts
67
Rep Power
0

## Re: Double in drawLine()

Originally Posted by pbrockway2
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: Double in drawLine()

Originally Posted by Robben
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. Member
Join Date
Feb 2015
Posts
67
Rep Power
0

## Re: Double in drawLine()

Originally Posted by pbrockway2
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. ## 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

7. ## Re: Double in drawLine()

Moved from New to Java to Java 2D.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•