Hello everyone my name is Fyree, I have recently been finishing up a school assignment, and am in need of some assistance. First let me give you the second half of my code to which this question pertains:
public class Rate_Graph_Graph extends Rate_Graph1{
double Aa, Bb, Cc, Dd, Ee, Ff;
public void anotheractionPerformed(ActionEvent ae)
{
if (ae.getSource() == calc){
Aa = Double.parseDouble(bv1.getText());
Bb = Double.parseDouble(ev_1.getText());
Cc = Double.parseDouble(ev_2.getText());
Dd = Double.parseDouble(ev_3.getText());
Ee = Double.parseDouble(ev_4.getText());
Ff = Double.parseDouble(ev_5.getText());
}
}
JLabel graphLabel;
double[] data = {Aa, Bb, Cc, Dd, Ee, Ff};
final int finalInt = 50;
public void paintGraph(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int w = getWidth();
int h = getHeight();
graphLabel = new JLabel ("<HTML><FONT SIZE =+25>Line Graph showing the Return Rate</FONT>");
add(graphLabel);
g2.draw(new Line2D.Double(finalInt, finalInt, finalInt, h-finalInt));
g2.draw(new Line2D.Double(finalInt, h-finalInt, w-finalInt, h-finalInt));
Font font1 = g2.getFont();
FontRenderContext fontRender = g2.getFontRenderContext();
LineMetrics lineMetrics = font1.getLineMetrics("0", fontRender);
float ft = lineMetrics.getAscent()+lineMetrics.getDescent();
String str = "";
g.drawString("Aa", 5, 315);
g.drawString("Bb", 5, 260);
g.drawString("Cc",5,205);
g.drawString("Dd",5,150);
g.drawString("Ee",5,100);
g.drawString("Ff",5,55);
g.drawString("Beginning Value",50,335);
g.drawString("Yeatr 1 Value",175,335);
g.drawString("Year 2 Value",300,335);
g.drawString("Year 3 Value",425,335);
g.drawString("Year 4 Value",550,335);
g.drawString("Year 5 Value",675,335);
float ft2 = finalInt + ((h - 2*finalInt) - str.length()*ft)/2 + lineMetrics.getAscent();
for(int i = 0; i < str.length(); i++){
String strLetter = String.valueOf(str.charAt(i));
float ft3 = (float)font1.getStringBounds(strLetter, fontRender).getWidth();
float ft4 = (finalInt - ft3)/2;
g2.drawString(strLetter, ft3, ft4);
ft2 += ft;
}
str = "YEARS";
ft2 = h - finalInt + (finalInt - ft)/2 + lineMetrics.getAscent();
float ft3 = (float)font1.getStringBounds(str, fontRender).getWidth();
float ft4 = (w - ft3)/2;
g2.drawString(str, ft4, ft2);
double xInc = (double)(w - 2*finalInt)/(data.length-1);
double scale = (double)(h - 2*finalInt)/getMax();
g2.setPaint(Color.green.darker());
for(int i = 0; i < data.length-1; i++){
double x1 = finalInt + i*xInc;
double y1 = h - finalInt - scale*data[i];
double x2 = finalInt + (i+1)*xInc;
double y2 = h - finalInt - scale*data[i+1];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
}
private double getMax()
{
int max = -Integer.MAX_VALUE;
final int[] intData = new int[data.length];
for(int i = 0; i < data.length; i++){
if(data[i] > max)
max = data[i];
}
return max;
}
public static void main(String[]args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new GraphingData());
f.setSize(800,400);
f.setLocation(200,200);
f.setResizable(false);
f.setVisible(true);
}
}
Now here is what my question is, how do you I convert the double[] data array that I have above into an int array so that I am able to use it in the private double getMax() method that is at the end of the code (I get the error: "incompatible types: possible lossy of conversion from double to int" when I try to do it without the conversion)? Any help is appreciated as this assignment is due later today!
your mistake is here
private double getMax()
{
int max = -Integer.MAX_VALUE;
final int[] intData = new int[data.length];
for(int i = 0; i < data.length; i++){
if(data[i] > max)
max = data[i];
}
return max;
}
Your array is double, and you are comparing it to int, you should do like this:
private double getMax()
{
double max = Double.MIN_VALUE;
final int[] intData = new int[data.length];
for(int i = 0; i < data.length; i++){
if(data[i] > max)
max = data[i];
}
return max;
}
Btw, what is intData, it is never used?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.