2012-03-02 43 views
1

我使用一个线程的输出创建线图,这些线程模拟了运行了52秒的输入和输出账单,并且这将在线图上显示,如下所示,以显示银行平衡超过52秒!Graph Axis问题

问题是我似乎无法正确计算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; 

是本?

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

没有这个给出和输出的-145,任何减号将离开面板。 – 2012-03-02 10:29:53

+0

我似乎已经明白了,答案是..... double balance = 365 - ((((double)y/maxY)* Y_AXIS_LENGTH)+ Y_AXIS_OFFSET); ..... 365是整个长度面板 – 2012-03-02 11:20:50

+1

这听起来像'Y_AXIS_OFFSET'被定义不正确。它需要是面板顶部和图形原点之间的区别。如果你设置了这个权利,那么@Hovercraft_Full_Of_Eels的更正就是你需要的。 – MattLBeck 2012-03-02 12:04:53