我从下面给出的链接中获得了制作条形图的源代码 -
我制作了一个模型类来存储项目及其相应的值。然后,我制作了一个面板类,它应该只接受模型并将其显示为条形图。然后我创建了一个框架类,将面板添加到它的内容窗格中。然后,我创建了一个应该运行所有这些的驱动程序类,就像链接中给出的代码一样。
问题是我只看到我的框架而没有条形图。我该如何解决?
我故意不包含我的代码,因为它太大了。但是,如果你觉得我应该把它放在这里,那么请告诉我。
package myArea;
public class SimpleBarChartModel{
private String[] items;
private double[] valuesOfItems;
private String title;
public SimpleBarChartModel(String[] itemList, double[] valueList, String title){
String[] items = itemList;
double[] valuesOfItems = valueList;
}
public String[] getItems(){
return items;
}
public void setItems(String[] items){
this.items = items;
}
public double[] getValuesOfItems(){
return valuesOfItems;
}
public void setValuesOfItems(double[] valuesOfItems){
this.valuesOfItems = valuesOfItems;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
}
package myArea;
import java.awt.*;
import javax.swing.*;
public class SimpleBarChartPanel extends JPanel{
SimpleBarChartModel bChartMdl;
public SimpleBarChartPanel(SimpleBarChartModel bcMdl){
bChartMdl = bcMdl;
}
public void paintComponent(Graphics graphics) {
//Must always be the first line in paintComponent method
super.paintComponent(graphics);
double[] values = bChartMdl.getValuesOfItems();
String[] items = bChartMdl.getItems();
String title = bChartMdl.getTitle();
if (values == null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i]){
minValue = values[i];
}
if (maxValue < values[i]){
maxValue = values[i];
}
}
Dimension dim = getSize();
int clientWidth = dim.width;
int clientHeight = dim.height;
int barWidth = clientWidth / values.length;
Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int q = titleFontMetrics.getAscent();
int p = (clientWidth - titleWidth) / 2;
graphics.setFont(titleFont);
graphics.drawString(title, p, q);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue){
return;
}
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
q = clientHeight - labelFontMetrics.getDescent();
graphics.setFont(labelFont);
for (int j = 0; j < values.length; j++) {
int valueP = j * barWidth + 1;
int valueQ = top;
int height = (int) (values[j] * scale);
if (values[j] >= 0){
valueQ += (int) ((maxValue - values[j]) * scale);
}else{
valueQ += (int) (maxValue * scale);
height = -height;
}
graphics.setColor(Color.blue);
graphics.fillRect(valueP, valueQ, barWidth - 2, height);
graphics.setColor(Color.black);
graphics.drawRect(valueP, valueQ, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(items[j]);
p = j * barWidth + (barWidth - labelWidth) / 2;
graphics.drawString(items[j], p, q);
}
}
}
package myArea;
import javax.swing.*;
public class SimpleBarChartView extends JFrame{
SimpleBarChartPanel bChartPanel;
public SimpleBarChartView(SimpleBarChartPanel bcPanel, int width, int height){
bChartPanel = bcPanel;
this.setSize(width, height);
this.getContentPane().add(bChartPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package myArea;
public class SimpleBarChartDriver {
public static void main(String[]args){
double[] value= new double[5];
String[] languages = new String[5];
value[0] = 1;
languages[0] = "Visual Basic";
value[1] = 2;
languages[1] = "PHP";
value[2] = 3;
languages[2] = "C++";
value[3] = 4;
languages[3] = "C";
value[4] = 5;
languages[4] = "Java";
SimpleBarChartModel sBModel =
new SimpleBarChartModel(languages,value,"comp languages");
SimpleBarChartPanel sBPanel = new SimpleBarChartPanel(sBModel);
SimpleBarChartView sbView = new SimpleBarChartView(sBPanel, 500, 500);
sbView.setVisible(true);
}
}
更新 - 这些是我在运行驱动程序类时遇到的错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.font.FontDesignMetrics.stringWidth(Unknown Source)