2012-03-02 12 views
1

スレッドからの出力を使用して折れ線グラフを作成していますが、スレッドは52秒間にわたって実行される入出庫請求のシミュレーションであり、これは下図のように銀行を示すために分割されます52秒以上のバランス!グラフの軸の問題

問題は、Y軸が適切に計算されないようです。 ショー出力以下のコード軸の右上にある赤いマーカーが、それは、このべきではないではない

enter image description here

public void paintComponent(Graphics g) { 

    int y = 10000; // balance 
    int x = 52 ; // weeks 
    int prevX, prevY; 
    int maxX = 52; 
    int maxY = 10000; 

    int Xleft = 200; 
    int Xright = 900; 
    int Ytop = 50; 
    int Ybottom = 330;// defining axis 

    Graphics2D g2 = (Graphics2D) g; 
    super.paintComponent(g2); 
    g2.setColor(Color.BLUE); 

    BasicStroke pen = new BasicStroke(4F); 
    g2.setStroke(pen); 

    g2.drawLine(Xleft,Ytop,Xleft,Ybottom); 
    g2.drawLine(Xleft,280,Xright,280); 

    Font f = new Font("Serif", Font.BOLD, 14); 
    g2.setFont(f); 
    g2.drawString("Account Balance (£)", 35, 200); 
    g2.drawString("Elapsed Time (Weeks)", 475, 340); 



//retrieve values from your model for the declared variables  

//calculate the coords line on the canvas 

double balance = (((double)y/maxY) * Y_AXIS_LENGTH) - Y_AXIS_OFFSET; //floating point arithmetic 
double weeks = (((double)x/maxX) * X_AXIS_LENGTH) + X_AXIS_OFFSET; 

int xPos = (int) Math.round (weeks); 
int yPos = (int)Math.round(balance); // changing back to int to be used in drawing oval 

g2.setColor(Color.RED); 
g.drawOval(xPos, yPos, 2, 2); 
System.out.println(xPos + " " + yPos); 

} 

答えて

2

ん:

double balance = (((double)y/maxY) * Y_AXIS_LENGTH) - Y_AXIS_OFFSET; 

はこのこと?

+0

いいえこれは-145の出力となり、マイナス整数はパネルから消えます。 –

+0

私はそれを持っているようだ、答えは.....ダブルバランス= 365 - (((ダブル)y/maxY)* Y_AXIS_LENGTH)+ Y_AXIS_OFFSET; ..... 365は全体の長さパネル –

+1

'Y_AXIS_OFFSET'のように聞こえるのは間違っています。パネルの上端とグラフの起点との違いが必要です。あなたがその権利を設定すると@Hovercraft_Full_Of_Eelsの訂正が必要です。 – MattLBeck