📄 chapter16n2.java
字号:
/** * * demonstration of AWT - use of text fields for thermometer * * Written by: Roger Garside * * First Written: 11/July/96 * Last Rewritten: 30/May/97 * */import java.awt.* ;import java.awt.event.* ;public class Chapter16n2 extends Frame implements WindowListener, ActionListener { Temperature temp1 ; private TextField celsiusField, fahrField ; private Canvas2 canvas ; private Button toF, toC, quit ; /** * * constructor * */ public Chapter16n2() { temp1 = new Temperature() ; // set up basic window setTitle("Chapter16n2") ; setBackground(Color.green) ; setSize(400, 600) ; addWindowListener(this) ; // set up area for the thermometer canvas = new Canvas2(this) ; add("Center", canvas) ; // set up the panel of buttons and text fields // set up the first line of items Panel p1 = new Panel() ; p1.setLayout(new FlowLayout()) ; p1.add(new Label("Celsius")) ; celsiusField = new TextField("", 8) ; p1.add(celsiusField) ; toF = new Button("Convert to F") ; p1.add(toF) ; toF.addActionListener(this) ; // set up the second line of items Panel p2 = new Panel() ; p2.setLayout(new FlowLayout()) ; p2.add(new Label("Fahrenheit")) ; fahrField = new TextField("", 8) ; p2.add(fahrField) ; toC = new Button("Convert to C") ; p2.add(toC) ; toC.addActionListener(this) ; // set up the third line of items Panel p3 = new Panel() ; p3.setLayout(new FlowLayout()) ; quit = new Button("Quit") ; p3.add(quit) ; quit.addActionListener(this) ; Panel p = new Panel() ; p.setLayout(new BorderLayout()) ; p.add("North", p1) ; p.add("Center", p2) ; p.add("South", p3) ; add("South", p) ; } // end of constructor method /** * * main * */ public static void main(String[] args) { Chapter16n2 f = new Chapter16n2() ; f.setVisible(true) ; } // end of main method /** * * actionPerformed * */ public void actionPerformed(ActionEvent event) { // deal with "Quit" button if (event.getSource() == quit) { dispose(); System.exit(0); } // deal with "Convert to Celsius" button else if (event.getSource() == toC) { String f1 = fahrField.getText() ; double f2 = 0.0 ; boolean isValid ; try { f2 = Double.valueOf(f1).doubleValue() ; isValid = true ; } catch (NumberFormatException ex) { isValid = false ; } if (isValid) { temp1.setFahr(f2) ; double c2 = temp1.getCelsius() ; c2 = ((double) Math.round(c2 * 100)) / 100 ; String c1 = Double.toString(c2) ; celsiusField.setText(c1) ; canvas.repaint() ; } else { fahrField.setText("") ; } } // deal with "Convert to Fahrenheit" button else if (event.getSource() == toF) { String c1 = celsiusField.getText() ; double c2 = 0.0 ; boolean isValid ; try { c2 = Double.valueOf(c1).doubleValue() ; isValid = true ; } catch (NumberFormatException ex) { isValid = false ; } if (isValid) { temp1.setCelsius(c2) ; double f2 = temp1.getFahr() ; f2 = ((double) Math.round(f2 * 100)) / 100 ; String f1 = Double.toString(f2) ; fahrField.setText(f1) ; canvas.repaint() ; } else { celsiusField.setText("") ; } } } // end of method actionPerformed /** * * windowClosing * */ public void windowClosing(WindowEvent event) { dispose(); System.exit(0); } // end of method windowClosing public void windowOpened(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowClosed(WindowEvent event) {} public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} } // end of class Chapter16n2class Canvas2 extends Canvas { Chapter16n2 parent ; /** * * constructor * */ public Canvas2(Chapter16n2 f) { parent = f ; } // end of constructor method /** * * paint * */ public void paint(Graphics g) { // set up some dimensions for the drawing Dimension d = getSize() ; int cx = d.width / 2, cy = d.height / 2 + 150, bulbInnerRadius = 40, bulbOuterRadius = 50, bulbInnerAngle = 20, bulbOuterAngle = 20, innerTubeRadius = 10, outerTubeRadius = 20, tubeLength = 300 ; // set up some dimensions for the text labels Font f1 = new Font("Helvetica", Font.BOLD, 14) ; FontMetrics fm1 = g.getFontMetrics(f1) ; int w0 = fm1.stringWidth("212F"), h0 = (fm1.getAscent() / 2) ; // draw surrounding line g.setColor(Color.black) ; g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ; // draw bulb g.setColor(Color.black) ; g.drawArc(cx - bulbOuterRadius, cy - bulbOuterRadius, bulbOuterRadius * 2, bulbOuterRadius * 2, 90 + bulbOuterAngle, 360 - (2 * bulbOuterAngle)) ; g.drawArc(cx - bulbInnerRadius, cy - bulbInnerRadius, bulbInnerRadius * 2, bulbInnerRadius * 2, 90 + bulbInnerAngle, 360 - (2 * bulbInnerAngle)) ; g.setColor(Color.red) ; g.fillOval(cx - bulbInnerRadius + 1, cy - bulbInnerRadius + 1, 2 * (bulbInnerRadius - 1), 2 * (bulbInnerRadius - 1)) ; // draw tube g.setColor(Color.black) ; g.drawLine(cx - outerTubeRadius, cy - bulbOuterRadius, cx - outerTubeRadius, cy - bulbOuterRadius - tubeLength) ; g.drawLine(cx - innerTubeRadius, cy - bulbInnerRadius, cx - innerTubeRadius, cy - bulbOuterRadius - tubeLength) ; g.drawLine(cx + innerTubeRadius, cy - bulbInnerRadius, cx + innerTubeRadius, cy - bulbOuterRadius - tubeLength) ; g.drawLine(cx + outerTubeRadius, cy - bulbOuterRadius, cx + outerTubeRadius, cy - bulbOuterRadius - tubeLength) ; // draw cap g.setColor(Color.black) ; g.drawArc(cx - outerTubeRadius, cy - bulbOuterRadius - tubeLength - outerTubeRadius, 2 * outerTubeRadius, 2 * outerTubeRadius, 0, 180) ; g.drawArc(cx - innerTubeRadius, cy - bulbOuterRadius - tubeLength - innerTubeRadius, 2 * innerTubeRadius, 2 * innerTubeRadius, 0, 180) ; // draw marks g.setColor(Color.black) ; g.drawLine(cx - 40, cy - bulbOuterRadius - 20, cx - 30, cy - bulbOuterRadius - 20) ; g.drawLine(cx + 40, cy - bulbOuterRadius - 20, cx + 30, cy - bulbOuterRadius - 20) ; g.drawLine(cx - 40, cy - bulbOuterRadius - tubeLength + 20, cx - 30, cy - bulbOuterRadius - tubeLength + 20) ; g.drawLine(cx + 40, cy - bulbOuterRadius - tubeLength + 20, cx + 30, cy - bulbOuterRadius - tubeLength + 20) ; g.drawString("0C", cx - 50 - w0, cy - bulbOuterRadius - 20 + h0) ; g.drawString("32C", cx + 50, cy - bulbOuterRadius - 20 + h0) ; g.drawString("100C", cx - 50 - w0, cy - bulbOuterRadius - tubeLength + 20 + h0) ; g.drawString("212C", cx + 50, cy - bulbOuterRadius - tubeLength + 20 + h0) ; // draw mercury double tempC = parent.temp1.getCelsius() ; int mercuryLength ; if (tempC < - 11) mercuryLength = 0 ; else if (tempC < 120) mercuryLength = (int) (30 + (2.6 * tempC)) ; else mercuryLength = tubeLength ; g.setColor(Color.red) ; g.fillRect(cx - innerTubeRadius + 1, cy - bulbInnerRadius - mercuryLength, 2 * innerTubeRadius, mercuryLength) ; } // end of method paint } // end of class Canvas2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -