-1

I'm trying to use a double that has a very small value to produce one of a more regular size. For example, dividing 1/(double) would produce something like 14.12848572.... I then cast this double into an int to produce a number which I can use to draw an image in a JPanel.

The issue I've been having is that the image does not draw how I expect it to draw. I think this is because there's something I don't understand about how casting doubles works. Anyone who can tell me a bit about how this process actually goes down would be very helpful.

EDIT:
The purpose of this code is translating a monetary value into the size of a bar on a graph. The size of these bars should change as the value changes so that they utilize space in the most efficient manner.

Therefore...

  • Due to the nature of the monetary values, max will never be less than 430.

  • I want the image to be bigger the smaller xscale is. If there are fewer values to graph, xscale is smaller, and then the bars are drawn bigger.

EDIT: The following image shows what my program is currently drawing. What I would like to draw is a series of bars on a bar graph. The xscale and associated variables are what I am primarily concerned with right now. 1

EDIT: The SSCCE is completed (I think)! If you run this code, you will see the drawing I don't want. If you change barwidth to equal 7 or some normal int, you will see something that is more along the lines of what I want drawn. Please let me know if there is anything more I should do to make things easier!

EDIT: Copy/pasted wrong code, has been corrected (derp)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


  public class castingexample extends JPanel
{   
private static int ypoint;
private static int barheight;
private static Color color;

private static int bars = 10;
private static int xpoint = 0;
private static int barwidth = 7;
private static double xscale = 7;
private static int yscaleplus = 10000;
private static int yscaleneg = 0;

public static void main(String[] args)
{
    JFrame frame = new JFrame();
    frame.add(new castingexample());
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
}

public castingexample()
{   
    new Timer(100, new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            resize();
            repaint();
        }
    }).start();
}

public void resize()
{

        xscale = bars/(800 - bars*5);
        xscale = 1/xscale;
}


public void paintComponent (Graphics g)
{
    super.paintComponent(g);

    for (int i = 0; i < bars; i++)
    {
        barheight = 200;
        barwidth = (int) (xscale);
        ypoint = 450 - barheight;
        xpoint = 105+(barwidth + 5)*i;

        if(ypoint < 450)
            color = Color.green;
        else
            color = Color.red;

        g.setColor(color);
        g.fillRect(xpoint, ypoint, barwidth, barheight);
    }
}

}

Here's some relevant code:

    public void resize()
{
    int max = 0;
    int min = 0;

        for (int i = 0; i < bars; i++)
        {
            if (getTime(i) > max)
                max = (int)getTime(i);
        }
        yscaleplus = max/430;

        /*
        for (int i = (int)(getLife() - getRetire() + 1); i < bars; i++)
        {
            if (getTime(i) < min)
                min = (int)getTime(i);
        }
        yscaleneg = Math.abs(min/200);
        */

        xscale = bars/(800 - bars*5);
        xscale = 1/xscale;
}


public void paintComponent (Graphics g)
{
    super.paintComponent(g);

    g.drawLine(100, 20, 100, 630);
    g.drawLine(100, 450, 900, 450);

    for (int i = 0; i < bars; i++)
    {
        barheight = (int) (getTime(i)/yscaleplus);
        barwidth = (int) (xscale);
        ypoint = 450 - barheight;
        xpoint = 105+(barwidth + 5)*i;

        if(ypoint < 450)
            color = Color.green;
        else
            color = Color.red;

        g.setColor(color);
        g.fillRect(xpoint, ypoint, barwidth, barheight);
    }
}
Christian Baker
  • 375
  • 2
  • 7
  • 22
  • 4
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Dec 14 '13 at 22:43
  • 3
    Maybe it's me, but I'm just confused by your code snippets. If you could create a small compilable and runnable program that demonstrates just your problem, (as @AndrewThompson has already recommended above), I think many of us would be better equipped to help you. – Hovercraft Full Of Eels Dec 14 '13 at 22:44
  • 3
    @HovercraftFullOfEels *"Maybe it's me"* No, you're not alone there. The population of 'Confusionville' is at least 2. – Andrew Thompson Dec 14 '13 at 22:46
  • 1
    if `max` is less than 430, `max/430` will always be 0 because it's an integer division. That may be your issue, in which case you can simply add: `max/430d` to do a double division. – assylias Dec 14 '13 at 22:48
  • Sorry, I'll add some more stuff in a sec – Christian Baker Dec 14 '13 at 22:49
  • I am also confused, but you do realize that if `xscale` is lower than 1, then the smaller `xscale` is, the bigger it's gonna get when you do `xscale = 1/xscale`, right? I really don't know what your code does, but this could just mean that the smaller the image should be, the bigger you're actually gonna draw it... – zbr Dec 14 '13 at 22:49
  • I'm working on adding more things besides the edits I just made to help you guys help me out. – Christian Baker Dec 14 '13 at 22:56
  • 2
    -1 for not posting an [sscce](http://sscce.org). The construct is well worth the effort, and so you won't regret creating and posting it. – Hovercraft Full Of Eels Dec 14 '13 at 23:01
  • I have no clue how to post an sscce, I'm trying to figure it out, but until then I just posted an image. EDIT: I'm trying to find all the relevant code, but it's taking a little bit. – Christian Baker Dec 14 '13 at 23:02
  • 1
    Please read the several links you've been given to the [sscce](http://sscce.org) page. They explain what you need. – Hovercraft Full Of Eels Dec 14 '13 at 23:03
  • 1
    We don't need 'more' stuff. We need *less* stuff. One line of code with the cast in it would do. All this Swing stuff is irrelevant. You need to take @AndrewThompson's advice. – user207421 Dec 14 '13 at 23:17
  • I'm just gonna kill this page and see if I can figure some more stuff out with the answer that has been provided, which is pretty helpful actually. If I still can't figure it out, I'll post another question with the appropriate formatting and information. Sorry to screw this up so badly, I'm still pretty new to the site. – Christian Baker Dec 14 '13 at 23:22
  • No sweat -- you're not screwing anything up, and in fact it looks like you've got your answer. But when you get a chance, do look over the sscce site as you will find it helpful in the future. In fact if done well, the process will help you yourself isolate and solve errors. – Hovercraft Full Of Eels Dec 15 '13 at 01:54

1 Answers1

2

doubles do not store information as you seem to think, they store information as a value followed by an exponential value of 2^x, this removes the capability you are trying to use

take a look at Math.round(x) instead.

Kent
  • 713
  • 2
  • 8
  • 19
  • What capability does it remove exactly? The ability to turn a very small value into a moderately sized one, or the ability to round that moderately sized value? – Christian Baker Dec 14 '13 at 23:17
  • as you tend towards smaller -or- larger numbers, double values become less accurate, leading to what you see here. – Kent Dec 14 '13 at 23:19
  • Should I use float or long instead? Or do those suffer from the same issues? – Christian Baker Dec 14 '13 at 23:23
  • long, int, short, & byte store information in integer format float & double store information in floating-point format again, take a look at Math.round() it should work well in this case – Kent Dec 14 '13 at 23:34