All pastes #477270 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-08 15:27 UTC
rendered paste body
/*
 * HistogramJframe.java
 *
 * Created on March 15, 2007, 5:30 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mygraphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


public class HistogramJframe extends JFrame {
    // Mechanism for user input
    private JLabel label1;    // Prompt to user
    private JTextField textfield1;   // Data entry mechanism
    
    // Member variables used as part of output display
    private MyPanel pan = null;
    private static int histogramWidth = 300;
    private static int histogramHeight = 300;
    private int MAX_COLORS = 6;
    private Color col[] = new Color[MAX_COLORS];
    
    // Member variables used as part of data storage and manipulation
    private final int MAX_NUMBERS = 50;  // The maximum number of entries allowed
    private int[] dataValue = new int[MAX_NUMBERS];   
    private boolean cannotAddFlag = false; // Indicates whether more data can be added
    private int entryCount = 0;
    
    public static void main( String[] args ) {
        HistogramJframe frame =  new HistogramJframe();  // Construct window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize( new Dimension( histogramWidth + 140, histogramHeight + 180 ) );  
        frame.setVisible( true );
    }
    
    /** Creates a new instance of HistogramJframe */
    public HistogramJframe() {    
        super("Histogram Demonstration");
        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());
        
        // Create panel to hold input components
        JPanel jpan = new JPanel();
        jpan.setLayout(new GridLayout(1,2));
        jpan.setBorder(new TitledBorder("Data Entry"));
        c.add(jpan,BorderLayout.NORTH);
        jpan.setVisible(true);
        
        // Create prompt and input mechanism to get data values from user
        label1 = new JLabel("Enter number, hit Enter key:");
        label1.setBorder(new EtchedBorder());
        jpan.add( label1);  // put prompt on panel
        textfield1 = new JTextField( 10 );
        textfield1.setEditable(true);        
        jpan.add( textfield1 );  // put input JTextField on panel
        
        // Listener to respond to a user hitting Enter in any JTextField
        MyActionListener myActListener = new MyActionListener();
        textfield1.addActionListener( myActListener );
        
        // Panel on which to display histogram
        pan = new MyPanel();
        pan.setBorder(new EtchedBorder());
        c.add(pan,BorderLayout.CENTER);
        pan.setVisible(true);
    }
    
    private void addNumberStringToArray(String numberStr) {
        int numb = 0;
        if ((numberStr != null) && (numberStr.length()) > 0) {
            if (entryCount < MAX_NUMBERS) {
                numb = Integer.parseInt(numberStr);
                dataValue[entryCount] = numb;
                entryCount++;
            }                
        }            
    }
    
    private void displayData() {
        pan.repaint();
    }
    
    protected String arrayToString(int Z[], int count) {
        String result = "";
        for (int i = 0; i < count; i++) {
            result += Z[i] + " ";
        }
        return result;
    }
    
    // Listener class to react to the user hitting the Enter key in JTextField
     class MyActionListener implements ActionListener {
        // Process user's action in JTextField input
        public void actionPerformed( ActionEvent e) {
            String outputStr = "";
            if ( entryCount < MAX_NUMBERS ) && ( numb <= 300 )  {
                String numberStr = textfield1.getText();
                if (numberStr.length() > 0) {
                    addNumberStringToArray(numberStr);   
                    textfield1.setText("");
                    outputStr = "The array values: " + arrayToString(dataValue, entryCount);
                    JOptionPane.showMessageDialog(null, outputStr, "Results",
                                         JOptionPane.INFORMATION_MESSAGE);
                }
            } else {
                cannotAddFlag = true;
            }            
            displayData();
        }
    }
    
    // A JPanel on which to draw the histogram
    class MyPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);            
            
            // NOTE: Student must replace the following illustrative code.
            //       This existing code does not belong in your final program.
            //       See the handout containing the Exercise instructions.
            g.drawString("Histogram Demonstration", 30, 80);            
            g.setColor(Color.RED);
            g.fillRect(30, 110, 250, 30);
            g.setColor(Color.BLUE);
            g.fillRect(30, 140, 350, 30);
            g.setColor(Color.GREEN);
            g.fillRect(30, 170, 150, 30);
            
        }
    }    
    
     createColorList(color) {
        color[0]=new Color(255, 0, 150);
        color[1]=new Color()
        color[3]=new Color()
        color[4]=new Color()
        color[5]=new Color()
        color[6]=new Color()
        
        return color;
    }
}