我遇到了麻烦。我正在尝试使用用户输入的数组列表创建条形图。我可以创建一个酒吧,但只能创建一个酒吧。我需要所有的数组输入来创建一个条形图。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import org.omg.PortableServer.CurrentOperations;
public class BarChart {
private double width;
private double height;
private ArrayList<Double> values;
Random generator = new Random ();
public BarChart(double iniwidth, double iniheight) {
width = iniwidth;
height = iniheight;
values = new ArrayList<Double>();
generator = new Random();
}
public void add (double input) {
values.add(input);
}
public void draw (Graphics g2) {
double max = Collections.max(values);
int xleft = 0;
for (int i = 0; i < values.size(); i++)
{
double barHeight= (max - height);
width = 50;
float r = generator.nextFloat();
float g = generator.nextFloat();
float b = generator.nextFloat();
Color randomColor = new Color(r, g, b);
Rectangle bar = new Rectangle ((int) (xleft + width), (int) height);
g2.setColor(randomColor);
((Graphics2D) g2).fill(bar);
g2.setColor(Color.BLACK);
((Graphics2D) g2).draw(bar);
}
System.out.println(Collections.max(values));
}
}