我试图通过创建一个"条形图"的形式来创建一种可视化数组输出的方法。通过使用line2d / graphics2d。我们的想法是让数组索引在x轴上,然后在y上使用值并在使用不同的排序方法时更新它。所以我试图使用arrayList来存储y值,然后在paint方法中使用for循环来绘制所有的行,但似乎每当我尝试使用重绘时它会重置我的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;
}
}
}
答案 0 :(得分:1)
您永远不会将您的_screen变量添加到GUI,它永远不会显示,因此调用它上面的repaint()
什么都不做。
所以对于starteres,改变
this.add(screen);
为:
add(_screen);
其他无关的问题:
,例如... ...
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());
}
}