/** * actionPerformed() handles clicks on the button. It takes the data from the input JTextFields, * and sends them to the GradeCalculater class to calculate a running average and computes the * letter grade, which are displayed in TextFields. * * @param e -- the ActionEvent the generated this system call */ public void actionPerformed(ActionEvent e) { double count, grade = 0, ave; DecimalFormat df = new DecimalFormat("0.00"); String inputString = inputField.getText(); // HINT: use try/catch blocks to catch bad input to parseDouble() // Type mismatch // check for range too boolean error = true; try { grade = Double.parseDouble(inputString); error = false; } catch (Exception e1) { System.out.println("input a number"); } // HINT: reject a bad grade in some way (the modified addGrade will return false // there is a problem with the grade // HINT: output grade count along with average and letter grade if (error == false) { inputField.setText(""); calculator.addGrade(grade); ave = calculator.calcAvg(); count = calculator.getCount(); String average = "" + df.format(ave); String letterGrade = calculator.calcLetterGrade(); resultField.setText(average + " " + letterGrade + " " + count); } } // actionPeformed()
/** * Adds an editable text field containing the hours spent on a project. * * @param gbl The layout to add the text field to. * @param gbc The layout constraints to use. * @param row The row to link against. * @param hours The number of hours spent on the project. * @see {@link #addRow(GridBagLayout, GridBagConstraints, String, double)} */ private void addMiddleField(GridBagLayout gbl, GridBagConstraints gbc, Row row, double hours) { row.hoursTF.setText(decimalFormat.format(hours)); gbc.gridx = 1; gbc.weightx = 1; gbl.setConstraints(row.hoursTF, gbc); gbc.weightx = 0; reviewPanel.add(row.hoursTF); }
// int frame = 0; public void paint(Graphics g) { // System.out.println("frame: " + (frame++)); lStatus.setText( "t = " + df.format(md.dt * md.step) + ", " + "N = " + md.N + ", " + "E/N = " + df.format(md.E / md.N) + ", " + "U/N = " + df.format(md.U / md.N) + ", " + "K/N = " + df.format(md.K / md.N) + ", " + "p = " + df.format(md.p) + ";"); tAvK.setText(df.format(md.avK.getAve() / md.N) + " "); tAvU.setText(df.format(md.avU.getAve() / md.N) + " "); tTemp.setText(df.format((2 * md.K) / (3 * (md.N - 1))) + " "); tAvp.setText(df.format(md.avp.getAve()) + " "); canvas.refresh(md.getXWrap(), md.N, true, false); cpnl.repaint(); spnl.repaint(); try { PrintWriter wavefunc = new PrintWriter(new FileOutputStream(new File("energyData.txt"), true)); wavefunc.print(md.E / md.N + " " + md.K / md.N + " " + md.U / md.N); wavefunc.println(); wavefunc.close(); } catch (IOException ex) { } try { PrintWriter tempwriter = new PrintWriter(new FileOutputStream(new File("tempData.txt"), true)); tempwriter.print(df.format((2 * md.K) / (3 * (md.N - 1)))); tempwriter.println(); tempwriter.close(); } catch (IOException ex) { } }
// calculate and display amounts private void calculateJButtonActionPerformed(ActionEvent event) { resultJTextArea.setText("Rate (%)\tAmount after 10 years"); DecimalFormat dollars = new DecimalFormat("$0.00"); int principal = Integer.parseInt(principalJTextField.getText()); // for loop to calculate interest for (int rate = 5; rate <= 10; rate++) { double amount = (double) principal * Math.pow(1 + ((double) rate / 100), 10); resultJTextArea.append("\n" + rate + "\t" + dollars.format(amount)); } // end for } // end method calculateJButtonActionPerformed
/** * This function re-computes the total number of hours for the selected period. It is ran every * time the user edits a text field specifying the number of hours for a particular top-level * project. Percentage labels are also updated. */ private void recomputeTotal() { double total = 0; for (Row row : rows.values()) { try { row.hours = Double.parseDouble(row.hoursTF.getText()); total += row.hours; row.hoursTF.setForeground(normalColour); } catch (NumberFormatException e) { row.hoursTF.setForeground(errorColour); totalLabel.setText("ERROR"); totalLabel.setForeground(errorColour); return; } } totalLabel.setText(decimalFormat.format(total)); totalLabel.setForeground(normalColour); for (Row row : rows.values()) { String percentS = decimalFormat.format(total == 0 ? 0 : 100 * row.hours / total); row.percentL.setText("(" + percentS + "%)"); } pack(); }
private String generateOverviewText() throws InsufficientDataException { StringBuilder sb = new StringBuilder(); final String team = config.getTeam(); double total = checkTotal(); final String nl = System.getProperty("line.separator"); for (Entry<String, Row> entry : rows.entrySet()) { double hours = Double.parseDouble(entry.getValue().hoursTF.getText()); double fraction = hours / total; if (fraction < 0.004) continue; String line = team + ", " + decimalFormat.format(fraction) + ", " + entry.getKey(); sb.append(line + nl); } return sb.toString(); }
// calculate and display amounts private void calculateJButtonActionPerformed(ActionEvent event) { // declare variables to store user input double principal = Double.parseDouble(principalJTextField.getText()); double rate = Double.parseDouble(interestRateJTextField.getText()); Integer integerObject = (Integer) yearsJSpinner.getValue(); Integer year = integerObject.intValue(); yearlyBalanceJTextArea.setText("Year\tAmount on Deposit"); DecimalFormat dollars = new DecimalFormat("$0.00"); // calculate the total value for each year for (int count = 1; count <= year; count++) { double amount = principal * Math.pow((1 + rate / 100), count); yearlyBalanceJTextArea.append("\n" + count + "\t" + dollars.format(amount)); } // end for } // end method calculateJButtonActionPerformed
/** convenience method for setting a double field */ public static void setFieldValue(JTextField field, double value) { DecimalFormat formatter = new DecimalFormat("0.0##E0"); field.setText(formatter.format(value)); }
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; RenderingHints rh = g2d.getRenderingHints(); rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHints(rh); // Background. D = this.getSize(); g.setColor(backgroundColor); g.fillRect(0, 0, D.width, D.height); Graphics2D g2 = (Graphics2D) g; g2.setStroke(lineStroke); // Axes, bounding box. g.setColor(Color.gray); g.drawLine(inset, D.height - inset, D.width - inset, D.height - inset); g.drawLine(D.width - inset, inset, D.width - inset, D.height - inset); g.drawLine(inset, inset, inset, D.height - inset); g.drawLine(inset, inset, D.width - inset, inset); double xDelta = (maxX - minX) / numIntervals; // X-ticks and labels. for (int i = 1; i <= numIntervals; i++) { double xTickd = i * xDelta; int xTick = (int) (xTickd / (maxX - minX) * (D.width - 2 * inset)); g.drawLine(inset + xTick, D.height - inset - 5, inset + xTick, D.height - inset + 5); double x = minX + i * xDelta; g.drawString(df.format(x), xTick + inset - 5, D.height - inset + 20); } // Y-ticks double yDelta = (maxY - minY) / numIntervals; for (int i = 0; i < numIntervals; i++) { int yTick = (i + 1) * (int) ((D.height - 2 * inset) / (double) numIntervals); g.drawLine(inset - 5, D.height - yTick - inset, inset + 5, D.height - yTick - inset); double y = minY + (i + 1) * yDelta; g.drawString(df.format(y), 1, D.height - yTick - inset); } // Zoom+move Font savedFont = g.getFont(); g.setFont(plusFont); g.drawString("+", D.width - 25, 20); g.setFont(minusFont); g.drawString("-", D.width - 25, 50); drawArrow(g2d, D.width - 70, 20, D.width - 70, 0, 1.0f, lineStroke); // Up drawArrow(g2d, D.width - 70, 30, D.width - 70, 50, 1.0f, lineStroke); // Down drawArrow(g2d, D.width - 65, 25, D.width - 45, 25, 1.0f, lineStroke); // Right drawArrow(g2d, D.width - 75, 25, D.width - 95, 25, 1.0f, lineStroke); // Left g.setFont(savedFont); // See if standard axes are in the middle. g.setColor(Color.gray); if ((minX < 0) && (maxX > 0) && (drawMiddleAxes)) { // Draw y-axis int x = (int) ((0 - minX) / (maxX - minX) * (D.width - 2 * inset)); g.drawLine(inset + x, D.height - inset, inset + x, inset); } if ((minY < 0) && (maxY > 0) && (drawMiddleAxes)) { // Draw x-axis int y = (int) ((0 - minY) / (maxY - minY) * (D.height - 2.0 * inset)); g.drawLine(inset, D.height - y - inset, D.width - inset, D.height - y - inset); } // Draw the objects. drawObjects(g, points, lines, ovals, rectangles, images, labels, eqnLines); if (animationMode) { drawObjects(g, animPoints, animLines, animOvals, animRectangles, null, labels, eqnLines); // No images in animation mode. } drawScribbles(g); }
public static void writeTopValue(double v) { writeTopString("" + df.format(v)); }
/** * Mit dieser Methode wird der Wert des Feldes mittels eines Integers gesetzt. * * @param value DOCUMENT ME! */ public void setValue(final int value) { setText(integerFormatter.format(value)); }