gpt4 book ai didi

java - 图形绘制问题

转载 作者:行者123 更新时间:2023-12-02 07:56:25 25 4
gpt4 key购买 nike

我正在使用线程的输出创建折线图,这些线程是在 52 秒的过程中运行的传入和传出账单的模拟,这将显示在折线图上,如下所示,以显示银行余额52秒!

目前程序运行良好,当我单击开始时,点会更新,但每个新点都会放置在图表上,前一个点就会消失。我怎样才能保留图表上的所有点。

我假设我必须创建 2 个新的 int 变量... prevX 和 prevY。

enter image description here

import java.awt.*;
import javax.swing.*;

public class DrawPanel extends JPanel {

private static final int X_AXIS_LENGTH = 700;
private static final int Y_AXIS_LENGTH = 230; // could be changed
private static final int X_AXIS_OFFSET = 200;
private static final int Y_AXIS_OFFSET = 85;
private static final int PanelHeight = 365;
private static final int PanelWidth = 1000;

public DrawPanel() {
this.setBackground(Color.white);
this.setPreferredSize(new Dimension(PanelWidth, PanelHeight));
}

public void paintComponent(Graphics g) {

int y = ControlPanel.bk1.getBalance(); // balance
int x = ControlPanel.bk1.getWeek(); // weeks //
int prevX, prevY;
int maxX = ContentPanel.controlPanel.getDuration();
int maxY = 100000;

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

while (ControlPanel.bk1.getBalance() > maxY) {
int i = maxY / 4;
maxY = maxY + i;
}

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); // set axis
g2.drawLine(Xleft, 280, Xright, 280);

int i = X_AXIS_OFFSET + (X_AXIS_LENGTH / 2);
int ii = X_AXIS_OFFSET + (X_AXIS_LENGTH / 4);
int iii = ((X_AXIS_LENGTH / 4)) * 3 + X_AXIS_OFFSET;
BasicStroke spaces = new BasicStroke(1F);
g2.setStroke(spaces);
g2.drawLine(i, 280, i, 300);
g2.drawLine(ii, 280, ii, 300);
g2.drawLine(iii, 280, iii, 300);

g2.setStroke(pen);

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

g2.setColor(Color.BLACK);
String maxXDisplay = Integer.toString(maxX);
String maxYDisplay = Integer.toString(maxY);
g2.drawString(maxYDisplay, 160, 45);
g2.drawString(maxXDisplay, 900, 300);

// retrieve values from your model for the declared variables

// calculate the coords line on the canvas

double balance = PanelHeight
- ((((double) y / maxY) * Y_AXIS_LENGTH) + Y_AXIS_OFFSET);
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);

}

public void reDraw() {
repaint();
}

}

最佳答案

您似乎只想在您的paintComponent方法中绘制一个点:

g.drawOval(xPos, yPos, 2, 2);

通常,您将通过 for 循环循环绘制此方法中的所有点。例如:

  for (int j = 0; j < maxPointCount; j++) {
x = someMethodToGetX(j);
y = someMethodToGetY(j);
double balance = PanelHeight - ((((double) y / maxY) *
Y_AXIS_LENGTH) + Y_AXIS_OFFSET);
double weeks = (((double) x / maxX) * X_AXIS_LENGTH) +
X_AXIS_OFFSET;

int xPos = (int) Math.round(weeks);
int yPos = (int) Math.round(balance);

g2.setColor(Color.RED);
g.drawOval(xPos, yPos, 2, 2);
}

编辑 1
关于您最近的评论:

Tryed that for loop and it makes no difference to the program

我上面的代码当然不是可以剪切并粘贴到您的程序中并且可以预期工作的代码,而只是被视为概念的示例。如果正确实现,for 循环将起作用,因为它对我有用很多次,但你的不起作用,所以我们必须修复你的实现,为了做到这一点,我们需要更多信息:

  • 您如何生成数据点?
  • 您是否使用 Swing Timer 来模拟实时数据收集?
  • 您需要在收集数据点时存储它们,以便您的paintComponent可以迭代它们。您如何存储数据点?是在ArrayList中吗?我们能看到这段代码吗?
  • 看到这一切后,我们能看到您尝试实现 for 循环来绘制所有数据点的代码吗?

您需要对问题进行编辑才能显示此新信息。如果您这样做,请在此答案中发表评论来通知我。

编辑2
这是我所描述的一个更完整的示例,它具有一个可绘制所有缩放数据点的功能 for 循环。当然,这些代码都不能复制并粘贴到您的应用程序中,但希望其中包含的概念可以转移。请询问是否有任何令人困惑的地方:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class TestShowGraph {
private static final int MAX_POINTS = 30;
private static final int TIMER_DELAY = 800;

private static void createAndShowGui() {
ShowGraph showGraphPanel = new ShowGraph(MAX_POINTS);
TimerListener timerListener = new TimerListener(MAX_POINTS, showGraphPanel);

JFrame frame = new JFrame("TestShowGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(showGraphPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

new Timer(TIMER_DELAY, timerListener).start();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class TimerListener implements ActionListener {
private static final double EXP_MULTIPLIER = 0.2;

// array of points created in constructor to hold data that
// will eventually be displayed in real time.
// A Swing Timer will copy a point into the pointsList above.
private Point2D[] initPoints;

private int maxPoints;
private int count = 0;
private ShowGraph showGraph;

public TimerListener(int maxPoints, ShowGraph showGraph) {
initPoints = new Point2D[maxPoints];
this.maxPoints = maxPoints;
this.showGraph = showGraph;

// create all data points that will eventually be
// graphed. This is to simulate real-time data collection
for (int i = 0; i < initPoints.length; i++) {
double x = (double) i / initPoints.length;
double y = 1.0 - Math.exp(-1.0 * i * EXP_MULTIPLIER);
initPoints[i] = new Point2D.Double(x, y);
}
}

public void actionPerformed(ActionEvent e) {
if (count < maxPoints) {
// simply push data from initPoints into the list that will
// be used to draw the graph
showGraph.addPoint(initPoints[count]);
count++;
} else {
// unless we've run out of points. Then simply start over
count = 0;
showGraph.clearPointsList();
}

// repaint so that the GUI will show the points
showGraph.repaint();
}
}

@SuppressWarnings("serial")
class ShowGraph extends JPanel {

private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final int BORDER_GAP = 50;
private static final Color AXIS_COLOR = Color.blue;
private static final Color POINTS_COLOR = Color.red;
private static final Color BACKGRND_COLOR = Color.white;
private static final Stroke AXIS_STROKE = new BasicStroke(3f);
private static final Stroke POINTS_STROKE = new BasicStroke(2f);
private static final double X_SCALE = PREF_W - 2 * BORDER_GAP;
private static final double Y_SCALE = PREF_H - 2 * BORDER_GAP;
private static final int POINT_RADIUS = 3;

// list that the paintComponent method loops through to
// draw points
private List<Point2D> pointsList = new ArrayList<Point2D>();


public ShowGraph(int maxPoints) {
setBackground(BACKGRND_COLOR);
}

public void addPoint(Point2D point2d) {
pointsList.add(point2d);
}

public void clearPointsList() {
pointsList.clear();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawAxises(g2);
drawPoints(g2);
}

private void drawAxises(Graphics g2) {
// derive a Graphics2D object from the one provided by the
// JVM so we can change settings on it without effecting
// the Graphics object provided by the JVM
Graphics2D g2Axises = (Graphics2D) g2.create();
g2Axises.setStroke(AXIS_STROKE);
g2Axises.setColor(AXIS_COLOR);
int x1XAxis = BORDER_GAP;
int y1XAxis = PREF_H - BORDER_GAP;
int x2XAxis = PREF_W - BORDER_GAP;
int y2XAxis = PREF_H - BORDER_GAP;
g2Axises.drawLine(x1XAxis, y1XAxis, x2XAxis, y2XAxis);
int x1YAxis = BORDER_GAP;
int y1YAxis = BORDER_GAP;
int x2YAxis = BORDER_GAP;
int y2YAxis = PREF_H - BORDER_GAP;
g2Axises.drawLine(x1YAxis, y1YAxis, x2YAxis, y2YAxis);
g2Axises.dispose(); // because we derived this we must dispose it
}

private void drawPoints(Graphics2D g2) {
Graphics2D g2Points = (Graphics2D) g2.create();
g2Points.setStroke(POINTS_STROKE);
g2Points.setColor(POINTS_COLOR);

for (Point2D p : pointsList) {
// p points hold data between 0 and 1
// we must scale our points to fit the display
// before displaying them
int pX = (int)(X_SCALE * p.getX()) + BORDER_GAP;
int pY = PREF_H - (int)(Y_SCALE * p.getY()) - BORDER_GAP;

// displayed the scaled points
int radius = POINT_RADIUS;
g2Points.drawOval(pX - radius, pY - radius, 2 * radius, 2 * radius);
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

}

运气。

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

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