gpt4 book ai didi

java - 使用 arrayList 作为 Graphics2d 中的 y 坐标

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

我正在尝试通过使用 line2d/graphics2d 创建“条形图”形式来创建一种可视化数组排序的方法。这个想法是在 x 轴上设置数组索引,然后在 y 轴上设置值并在使用不同的排序方法时更新它。因此,我尝试使用 arrayList 来存储 y 值,然后在 Paint 方法中使用 for 循环来绘制所有线条,但似乎每当我尝试使用 repaint 时,它都会重置我的 arrayList,所以我该怎么做?

这是我正在使用的代码:

import javax.swing.*;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Random;

public class Frame_Main extends JFrame {

private JPanel screen = new Screen();
private Screen _screen = new Screen();

private int rndMaxRange = 100;
private int maxArraySize = 50;

// TODO Make sure static is really needed
public static ArrayList<Integer> array = new ArrayList<>();

public Frame_Main() {
this.setSize(700, 500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(screen);
}

public static void main(String[] args) {
Frame_Main m = new Frame_Main();

m.randomizeArray(array);
m.refreshVisuals(array);
}

public void refreshVisuals(ArrayList<Integer> arrayList) {

_screen.setArrayList(arrayList);
_screen.repaint();

System.out.println("Refreshed drawing. Screen Array Size: " + _screen.lineArrayList.size());
}

public void randomizeArray(ArrayList<Integer> arrayList) {
arrayList.clear();
Random rnd = new Random();

for (int i = 0; i < maxArraySize; i++) {
arrayList.add(rnd.nextInt(rndMaxRange) + 1);
}

System.out.println("Created an array with the size: " + arrayList.size());
}
}

class Screen extends JPanel {

public ArrayList<Integer> lineArrayList = new ArrayList<>();

// private int canvasWidth = Frame_Main.WIDTH;
private int lineWidth;

public void setArrayList(ArrayList<Integer> arrayList) {
lineArrayList.addAll(arrayList);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

lineWidth = 3;
g2.setStroke(new BasicStroke(lineWidth));

System.out.println("lineArrayList Size: " + lineArrayList.size());

int x = 0;

for (int i : lineArrayList) {
g2.draw(new Line2D.Float(x, x, 0, i));
x = x + lineWidth;
}
}
}

最佳答案

您永远不会将 _screen 变量添加到 GUI,它永远不会显示,因此对其调用 repaint() 不会执行任何操作。

所以首先,改变

this.add(screen);

至:

add(_screen);

其他不相关的问题:

  • 您应该重写 JPanel 的paintComponent 方法,而不是它的paint 方法
  • 您应该在覆盖中首先调用 super 的绘制方法。

例如,类似...

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class MyScreen extends JPanel {
private static final int PREF_W = 700;
private static final int PREF_H = 500;
private static final int LINE_WIDTH = 5;
private static final Stroke STROKE = new BasicStroke((float) LINE_WIDTH);
private static final int MAX_DATA_SIZE = 50;
private static final int MAX_RANGE = 400;
private List<Integer> displayData = new ArrayList<>();
private Random random = new Random();

public MyScreen() {
for (int i = 0; i < MAX_DATA_SIZE; i++) {
displayData.add(random.nextInt(MAX_RANGE) + 1);
}
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

@Override
protected void paintComponent(Graphics g) {
// to do house-keeping painting
super.paintComponent(g);

// to smooth out graphics
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if (displayData == null) {
return;
}

g2.setStroke(STROKE);
int x = 0;
for (Integer i : displayData) {
g2.drawLine(x, x, 0, i);
x += LINE_WIDTH;
}
}


private static void createAndShowGui() {
MyScreen mainPanel = new MyScreen();

JFrame frame = new JFrame("My Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于java - 使用 arrayList 作为 Graphics2d 中的 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45863528/

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