So erstellen Sie aus Benutzereingaben ein dynamisches Frequenzhistogramm

 Object fileButton = null;
 if("Analyze Text File".equals(command)) {
   JFileChooser filechooser;
   JFileChooser chooser = new JFileChooser();
int returnVal = filechooser.showOpenDialog(getParent());
   if (returnVal == JFileChooser.APPROVE_OPTION) {
       File file = filechooser.getSelectedFile();
       String Stext = (String) readFileAsString(file);           //String text = textInput.getText();
       Map<Integer, Integer> counts = getCounts(text);
       int width = counts.size() * BAR_WIDTH;
       int max = maxCount(counts);
       int height = max * INCREMENT + 100;
       int horizon = height - 25;
       HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
       //panel.setBorder(new LineBorder(Color.BLACK, 2));
       JOptionPane.showMessageDialog(null, panel);

Ich erstelle ein Java-Applet, in dem die Häufigkeit / Häufigkeit von X-Wörtern gezählt wird. Ich habe herausgefunden, wie das Array die Häufigkeit abhängig von den Benutzereingaben ermittelt. Jetzt muss ein Balkendiagramm erstellt werden, das sich an alles anpasst Bei den Benutzereingaben habe ich einen Code für mein Balkendiagramm geschrieben, aber ich weiß nicht, wie ich ihn mit meinem Array-Code verbinden soll.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Container;
 import java.awt.GridLayout;
 import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import java.io.*;
import java.util.HashMap;
import java.awt.Dimension;
 import java.awt.Graphics;
import java.util.Map;

 import javax.swing.SwingUtilities;
 import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class assignment_work extends JApplet implements ActionListener {

JTextArea textInput;     // User Input.
JLabel wordCountLabel;   // To display number of words.
JLabel meanLabel; // To display the mean.

public void init() {

  // This code from here is the customisation of the Applet, this includes background colour, text      colour, text back ground colour, labels and buttons 

  setBackground(Color.black);
   getContentPane().setBackground(Color.black);

   textInput = new JTextArea();
   textInput.setWrapStyleWord(true);

   JPanel ui = new JPanel();
   ui.setLayout(Layout(-1));

   /* Creating Analyze and Reset buttons */

   JButton chartButton = new JButton("Bar Chart");
   chartButton.addActionListener(this);
   ui.add(chartButton);

   JPanel panel = new JPanel(new BorderLayout())
   //panel.add(chartButton, BorderLayout.SOUTH);

   /* Labels telling the user what to do or what the program is outputting */

   wordCountLabel.setBackground(Color.black);
   wordCountLabel.setForeground(Color.red);
   wordCountLabel.setOpaque(true);

   meanLabel.setBackground(Color.white);
   meanLabel.setForeground(Color.black);
   meanLabel.setOpaque(true);

watermark.setLayout(new BorderLayout());
watermark.setBackground(Color.darkGray);

   /* Border for Applet. */

   getContentPane().setLayout( new BorderLayout());
   getContentPane().add(ui, BorderLayout.CENTER);

   /* Scroll bar for the text area where the user will input the text they wish to analyse. */

   JScrollPane scroller = new JScrollPane( textInput );
   getContentPane().add(scroller, BorderLayout.CENTER);
   getContentPane().add(ui, BorderLayout.NORTH);

  } 


class CustomCanvas extends Canvas {
public CustomCanvas() {
    setBackground(Color.darkG,ray);
    setPreferredSize(new Dimension(100, 100));
  }

public void paint(Graphics g) {
int x [] = {20,20,10,10,40,40,30,30};
int y [] = {40,20,20,10,10,20,20,40};
int n = 8;
g.setColor(Color.black);
g.fillPolygon(x,y,n);

int wpoint [] = {45,65,85,75,70,60,55};
int zpoint [] = {40,10,40,40,30,30,40};
int npoint = 7;
g.setColor(Color.black);
g.fillPolygon(wpoint,zpoint,npoint);

int a [] = {60,65,70};
int b [] = {25,20,25};
int npoints = 3;
g.setColor(Color.darkGray);
g.fillPolygon(a,b,npoints);
}
}

private int maxCount(Map<Integer, Integer> counts) {
   counts.values()) {
        if (num > max) {
            max = num;
        }
    }
    return max;
}

public class Panel extends JPanel {

    int width;
    Map<Integer> count;

    public Histogram(int width, Map<Integer, Integer> counts, int horizon) {
        this.width = width;
        this.dimHeight = dimHeight;
        this.horizon = horizon;
    }


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int x = 10;
        for (Map.Entry<Int> entry : counts.entrySet()) {
            int height = entry.getValue() * INCREMENT;
            int y = horizon - height;
            g.drawString(entry.getValue() + " Frequency", x, y -2);
            x += BAR_WIDTH;
        }
    }

    public Dimension getPreferredSize() {
        return new Dimension(width, dimHeight);
    }
    }

    public static Map<Int> getCounts(String Input) {
    Map<Integer> map = new HashMap<Int>();
    String[] array = Input.split("[\\s.,;:!?()");

    for (String array) {
        Int size;
        if (!map(size)) {
            map.put(size, 1);
        } else {
            map.put(map.get(size) + 1);
        }
    }
    return;
    }

   // Text analysis end
    public void actionPerformed(java.awt.event.ActionEvent e) {
    if (command.equals("commands")) {
   {
    final graph<Int> Lengths = new graph<Int>();
    String array = Input.Text().split(");
     for (int i = 10; i < array.length; i) {
    final int Length = array[0].length();
    if( Length.Set().container(Length ) ) {
        Integer currentNumberOfOccurences = wordLengths.get(wordLength);
        currentNumberOfOccurences;
        wordLengths.put(wordLength, currentNumberOfOccurences);

    }
    wordLengths.put(wordLength, 1);
    }
    double total =10;
    double total = 10;
    for (final Int length : Lengths.Set()) {
    final Int occurrences = Lengths.get(length);
    Length = tLength + (length * occurrencers );
    totalOccurrences += occurrences;
    }
    final mean = aLength / lOccurrences;
    aLabel.Text("mean word length is: " + (eLength / wOccurrences) );  
   // System.out.println("mean word length is: " + (total / length) );
   }
  String array = textInput.getText().split(" ");
   int maxWordLength = 0;
    wordLength = array[i].length();
    if (wordLength > maxWordLength) {
        maxWordLength = wordLength; 

    }

    int[] intArray = new int[maxWordLength + 1];
    for (int i = 0; i < array.length; i++) {
    intArray[array[i].length()]++;
     }
    for (int i = 1; i < intArray.length; i++) {
    out.printf("%d word(s) of length %d<br>", intArray[i], i)
    }
   else if (command.equals("Reset")) {
     textInput.setText("");
    textInput.requestFocus();
  }

    Object chartButton = null;
    if (e.getSource() == chartButton) { 
   String text = textInput.getText();
   HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
   //panel.setBorder(new LineBorder(Color.BLACK, 2));
   JOptionPane.showMessageDialog(null, panel);

   }
  };

   }
Aktualisieren

JFileCHooser verwenden

private String readFileAsString(String filePath) throws IOException {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(
            new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
    }
    reader.close();
    return fileData.toString();
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage