图轴问题 [英] Graph Axis issue
本文介绍了图轴问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用来自线程的输出创建线图,线程是在52秒的时间内运行的入站和出站帐单的模拟,这将在如下所示的线图上显示,以显示银行余额52秒!
I am creating line graph using outputs from a thread, the threads are simulations of incoming and outgoing bill that run over a course of 52 seconds and this will be dipicted on a line graph as shown below to show the bank balance over the 52 seconds!
问题是我似乎无法正确计算Y轴。
下面的代码显示在轴右上角输出红色标记,但它不
The problem is I cannot seem to get the Y Axis calculating properly. The code below show output the red marker at the top right hand side of the axis but it does not
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);
}
推荐答案
t:
Shouldn't this:
double balance = (((double)y / maxY) * Y_AXIS_LENGTH) - Y_AXIS_OFFSET;
是这样吗?
double balance = Y_AXIS_OFFSET - (((double)y / maxY) * Y_AXIS_LENGTH);
这篇关于图轴问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文