1
我使用一個線程的輸出創建線圖,這些線程模擬了運行了52秒的輸入和輸出賬單,並且這將在線圖上顯示,如下所示,以顯示銀行平衡超過52秒!Graph Axis問題
問題是我似乎無法正確計算Y軸。 下面顯示輸出的代碼在軸的頂部右手側的紅色標記,但它不會
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);
}
沒有這個給出和輸出的-145,任何減號將離開面板。 – 2012-03-02 10:29:53
我似乎已經明白了,答案是..... double balance = 365 - ((((double)y/maxY)* Y_AXIS_LENGTH)+ Y_AXIS_OFFSET); ..... 365是整個長度面板 – 2012-03-02 11:20:50
這聽起來像'Y_AXIS_OFFSET'被定義不正確。它需要是面板頂部和圖形原點之間的區別。如果你設置了這個權利,那麼@Hovercraft_Full_Of_Eels的更正就是你需要的。 – MattLBeck 2012-03-02 12:04:53