嗨,我想将图表打印到小程序窗口中,但在重新调整窗口大小之前,图表是不可见的。
我的问题是:我的代码有什么问题,我该如何修复?另外,我在下面一行收到一个空指针异常
loop:for(int i=0; i <count.length; i++){
这是一个我的变量问题,但我不能在这个阶段修复它。我完全是个初学者。
public class Simple extends Applet implements ActionListener, KeyListener {
Button analyze, reset, load;
TextArea input, output;
Panel panel, drawArea;
JFileChooser fc;
FileReader readFile;
BufferedReader bufferedFile;
String line = "";
int count[];
int length;
int i ;
int x = 550;
int y = 320;
public void init() {
setSize(800,600);
setLayout (new BorderLayout());
panel = new Panel(new GridLayout(0,3));
panel.add(analyze = new Button("Analyze"));
panel.add(load = new Button("Load"));
panel.add(reset = new Button("Reset"));
add(panel,BorderLayout.SOUTH);
panel = new Panel(new GridLayout(2,1));
panel.setPreferredSize(new Dimension(200,0));
panel.add(input = new TextArea());
panel.add(output = new TextArea());
add(panel,BorderLayout.WEST);
/*
panel.add(drawArea = new Panel());
add(drawArea,BorderLayout.CENTER);
drawArea.getGraphics();
*/
analyze.addActionListener(this);
analyze.setEnabled(false);
load.addActionListener(this);
reset.addActionListener(this);
input.addKeyListener(this);
output.setEditable(false);
fc = new JFileChooser();
setBackground(Color.cyan);
}
public void start() {
System.out.println("starting...");
}
public void stop() {
System.out.println("stopping...");
}
public void destroy() {
System.out.println("preparing to unload...");
}
@Override
public void keyPressed(KeyEvent e) {
String inputGetText = input.getText();
if (inputGetText != null) {
analyze.setEnabled(true);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==analyze) {
input.setEditable(false);
analyze.setEnabled(false);
wordProcess();
}
if (event.getSource()==load){
fc.showOpenDialog(load);
File file = fc.getSelectedFile();
try {
readFile = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedFile = new BufferedReader(readFile);
try {
String nLine = bufferedFile.readLine();
while (nLine!=null){
line+=nLine + "\n";
nLine = bufferedFile.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
input.setText(line);
analyze.setEnabled(true);
}
if (event.getSource()==reset){
input.setText(null);
input.setEditable(true);
output.setText(null);
//x = 50;
line = "";
analyze.setEnabled(false);
}
}
public void wordProcess(){
for(String retva1: input.getText().replaceAll("[^a-zA-Z]+"," ").split(" "))
if(length < retva1.length())
length = retva1.length();
count = new int [length+1];
for(String retva2: input.getText().replaceAll("[^a-zA-Z]+"," ").split(" ")){
count[retva2.length()]++;
}
String show = "";
float average = 0;
float sum = 0;
float sum2 = 0;
loop: for(int i = 0; i <count.length; i++){
if (count[i]==0||i==0){
continue loop;
}else{
show += (count[i]+" words of length "+i) + "\n";
sum2 +=i*count[i];
average +=count[i];
}
output.setText("There are: "+"\n"+show);
}
sum = sum2/average;
String s2 = new Float(sum2).toString();
String a = new Float(average).toString();
output.append("\n"+"The mean word length: "+sum);
output.append("\n"+s2);
output.append("\n"+a);
}
public void paint(Graphics g) {
g.drawLine(250, 50, 250, 320);
g.drawLine(250, 320, 520, 320);
g.drawLine(250, 340, 250, 610 );
g.drawLine(250, 610, 520, 610);
loop: for(int i=0; i <count.length; i++){
if (count[i]==0||i==0){
continue loop;
}else{
int wordFreqency = count[i];
int wordLenght = i;
g.fillRect( x , y-(wordFreqency*5), 10, wordFreqency*5);
String scaleIndexX = new Integer(i).toString();
g.drawString(scaleIndexX, x, 335);
String scaleIndexY = new Integer(count[i]).toString();
g.drawString(scaleIndexY, x, y-(wordFreqency*5+5));
System.out.println(x);
int move = 0;
if (move<wordLenght){
x+=20;
}
}
}
}
}
1条答案
按热度按时间jvlzgdj91#
count数组变量最初为null,仅在actionperformed方法调用时在按钮按下时创建
wordProcess()
。所以您的绘图例程正在尝试使用空变量。解决方案:使用if控件检查count是否为null,然后再尝试使用它进行绘制。作为旁白:你最好使用swing而不是awt。
另外,打电话
repaint()
在获得绘制图表所需的所有信息后,使用actionperformed方法。