如何根据用户输入制作动态频率直方图

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 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); 

我正在创建一个java applet,它计算X字的频率/出现次数,我已经研究了如何根据用户输入的数据来计算频率,我现在需要创建一个适应任何东西的条形图用户输入,我已经为我的条形图写了一个代码,但我不知道如何将它连接到我的数组代码。

 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.darkGray); 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 counts) { counts.values()) { if (num > max) { max = num; } } return max; } public class Panel extends JPanel { int width; Map count; public Histogram(int width, Map 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 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 getCounts(String Input) { Map map = new HashMap(); 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 Lengths = new graph(); String array = Input.Text().split("); for (int i = 10; i  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
", 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); } }; }

更新

使用JFileCHooser

 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(); } 

以下是您要从输入创建动态大小的操作。

  • 您需要获取地图中的条目数,然后将其乘以您想要的每个条的宽度。
  • 你需要在地图中获得最高值,然后乘以增量(绘制)数量并从中创建高度。
  • 您可以根据前两个点的高度和宽度创建一个新的JPanel。
  • 通过地图迭代绘制条形图。

我在actionPerformed中执行上面的操作,然后将JPanel添加到JOptionPane 。 如果您需要澄清任何事情,请告诉我。

在此处输入图像描述

在此处输入图像描述

 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class Histogram { private static final int BAR_WIDTH = 50; private static final int INCREMENT = 10; public Histogram() { final JTextArea textArea = new JTextArea(5, 40); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); JButton go = new JButton("Histogram-me!"); JPanel panel = new JPanel(new BorderLayout()); panel.add(textArea, BorderLayout.CENTER); panel.add(go, BorderLayout.SOUTH); JFrame frame = new JFrame(); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); go.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = textArea.getText(); Map 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); } }); } private int maxCount(Map counts) { int max = 0; for (Integer num : counts.values()) { if (num > max) { max = num; } } return max; } public class HistogramPanel extends JPanel { int width; int dimHeight; int horizon; Map counts; public HistogramPanel(int width, Map counts, int dimHeight, int horizon) { this.width = width; this.counts = counts; this.dimHeight = dimHeight; this.horizon = horizon; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int x = 10; for (Map.Entry entry : counts.entrySet()) { int height = entry.getValue() * INCREMENT; int y = horizon - height; g.fillRect(x, y, BAR_WIDTH - 10, height); g.drawString(entry.getKey() + " chars", x, horizon + 10); g.drawString(entry.getValue() + " times", x, y -2); x += BAR_WIDTH; } } @Override public Dimension getPreferredSize() { return new Dimension(width, dimHeight); } } public static Map getCounts(String input) { Map map = new HashMap<>(); String[] array = input.split("[\\s.,;:!?(){}]+"); for (String s : array) { Integer size = s.length(); if (!map.containsKey(size)) { map.put(size, 1); } else { map.put(size, map.get(size) + 1); } } return map; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Histogram histogram = new Histogram(); } }); } }