gpt4 book ai didi

java - 图轴问题

转载 作者:行者123 更新时间:2023-11-30 04:52:32 24 4
gpt4 key购买 nike

我正在使用线程的输出创建折线图,这些线程是在 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);

}

最佳答案

这不应该是:

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

是这个吗?

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

关于java - 图轴问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com