我正在做一个项目,简而言之:
它正确编译,弹出JFrame窗口,但没有打印出数据。这只是一个空窗口。
这是我的代码:
package basicgraphicstester;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class BasicGraphicsTester extends JFrame {
private Image fImageBuffer;
private Insets fInsets;
private Graphics g;
private static final int WIDTH = 800, HEIGHT = 600;
private static final Color OVER_AVERAGE = Color.blue,
UNDER_AVERAGE = Color.red;
int[] data;
public BasicGraphicsTester() throws FileNotFoundException
{ ReadInputData();
addWindowListener(new WindowCloser());
setVisible(true);
fInsets = getInsets();
setSize(WIDTH + fInsets.left + fInsets.right, HEIGHT + fInsets.top +fInsets.bottom);
setTitle("Bar Graph");
setResizable(false);
if (((fImageBuffer = createImage(WIDTH, HEIGHT)) != null) &&
((g = fImageBuffer.getGraphics()) != null)) Run();
else System.exit(1);
}
class WindowCloser extends WindowAdapter
{ public void WindowClosing(WindowEvent e )
{ System.exit(0); }
}
private void Run()
{ DrawAverageLine();
DrawBars();
}
private void DrawBars()
{
double arrayAverage = arrayAverage(data);
int average = averageLine(data);
int max = getMaxValue(data);
int min = getMinValue(data);
int barWidth = 57;
double barHeight;
for (int i = 0; i < data.length; i++) {
barHeight = (600 * (max - data[i]))/(max - min);
if (data[i] > average) {
g.setColor(OVER_AVERAGE);
g.fillRect((int)barWidth * i, (int)barHeight, (int)barWidth,
(average - (int)barHeight));
}
else if (barHeight == average) {
g.setColor(Color.green);
g.fillRect((int)barWidth * i, average, (int)barWidth, 0);
}
else {
g.setColor(UNDER_AVERAGE);
g.fillRect((int)barWidth * i, average, (int)barWidth,
((int)barHeight - average));
repaint();
}
} //for loop
} //DrawBar
private void DrawAverageLine()
{
int average = averageLine(data);
g.drawLine(0, average, 800, average);
}
public static int getMaxValue(int[] data) {
int maxValue = data[0];
for (int i=1;i < data.length;i++) {
if (data[i] > maxValue)
maxValue = data[i];
}
return maxValue;
}
public static int getMinValue(int[] data) {
int minValue = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] < minValue)
minValue = data[i];
}
return minValue;
}
public static double arrayAverage(int[] data) {
double result = 0.0;
for (int i = 0; i < data.length; i++) {
result = result + data[i];
}
result = result/data.length;
return result;
}
public static int averageLine (int[] data) {
int max = getMaxValue(data);
int min = getMinValue(data);
return (HEIGHT * max - (int)arrayAverage(data)) / (max - min);
}
public void paint( Graphics g)
{ if (fImageBuffer != null )
g.drawImage(fImageBuffer, fInsets.left, fInsets.top, null);
}
public void ReadInputData() throws FileNotFoundException {
try {Scanner readFile = new Scanner(new File("BarChart.data"));
data = new int [13];
for (int i = 0; i < data.length; i++)
data[i] = readFile.nextInt();
} //try
catch (FileNotFoundException e) {
System.out.println(e);
}
} // ReadInputData
public static void main(String[] args) throws FileNotFoundException {
new BasicGraphicsTester();
}
}
请帮忙。提前谢谢。
答案 0 :(得分:2)
JFrame
是一个空框架。您需要在框架中放置JPanel
,并覆盖后者的paintComponent()
方法,以便在屏幕上呈现数据。
答案 1 :(得分:2)
这不是你用Swing做图形或绘图的方式,我有兴趣看看你读过的教程建议你去做你正在做的Graphics对象。相反,您应该使用JVM提供的作为参数的Graphics实例绘制扩展JComponent的类的paintComponent(...)
方法,例如JPanel,然后将此组件添加到JFrame中。您需要阅读painting in Swing tutorial,它是标准Swing教程的一部分。
答案 2 :(得分:0)
如果你想直接绘制到JFrame中,你仍然需要覆盖ContentPane的绘画,而不是JFrame的绘画方法AFAIK。 是的,更好的方法是实现自己的组件(例如通过扩展JPanel)并将其放在JFrame中(记住它必须放在ContentPane中)。
检查有关ContentPane的JFrame javadoc。
答案 3 :(得分:0)
我最近使用BarGraph组件在JavaFX中编写了类似的东西。我会发布源代码,如果它有用,但也许你需要使用swing,你的项目的要点是制作你自己的条形图。 查看我的javafx bar graph