import java.awt.*; import javax.swing.*; /** TemperaturesWriter2 creates a graphics window for displaying temperatures */ public class TemperaturesWriter2 extends JFrame { // these fields define the sizes and positions of the two thermometers: private int CELSIUS_POS = 20; // x-position of the Celsius thermometer private int FAHRENHEIT_POS = 170; // x-position of the Fahrenheit thermometer private int TOP = 60; // the y-position of the thermometers' tops private int LENGTH = 50; // the length of the thermometer private int WIDTH = 100; // the width of the thermometer private int MIN_TEMP = 0; // the min. temp. private int MAX_TEMP = 100; // the max. temp. private int TEXT_OFFSET = 15; // where to position the text labels // these fields remember the two temperatures that are displayed: private int celsius_degrees; // the degrees celsius private boolean celsius_degrees_contains_data = false; private int fahrenheit_degrees; // the degrees fahrenheit private boolean fahrenheit_degrees_contains_data = false; /** Constructor TemperaturesWriter2 creates the window and makes it visible */ public TemperaturesWriter2() { setTitle("Celsius into Fahrenheit"); setSize(300,200); setBackground(Color.white); setVisible(true); } /** displayCelsius draws the Celsius thermometer * @param degrees - temperature to display */ public void displayCelsius(int degrees) { celsius_degrees = degrees; celsius_degrees_contains_data = true; repaint(); // repaint the window with the new celsius_degrees } /** displayFahrenheit draws the Fahrenheit thermometer * @param degrees - temperature to display */ public void displayFahrenheit(int degrees) { fahrenheit_degrees = degrees; fahrenheit_degrees_contains_data = true; repaint(); // repaint the window with the new fahrenheit_degrees } /** paint fills the window with two thermometers * @param g - the ``graphics pen'' that draws the items onto the window */ public void paint(Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); g.setColor(Color.black); g.drawString("Celsius", CELSIUS_POS + TEXT_OFFSET, TOP - TEXT_OFFSET); g.drawString("is", (CELSIUS_POS + WIDTH + FAHRENHEIT_POS)/2, TOP + TEXT_OFFSET); g.drawString("Fahrenheit", FAHRENHEIT_POS + TEXT_OFFSET, TOP - TEXT_OFFSET); drawThermometer(CELSIUS_POS, celsius_degrees, celsius_degrees_contains_data, g); drawThermometer(FAHRENHEIT_POS, fahrenheit_degrees, fahrenheit_degrees_contains_data, g); } /** drawThermometer paints a thermometer * @param where - the horizontal position of the thermometer * @param degrees - how high to fill the thermometer * @param data_available - contains degrees a reasonable value? * @param g - the graphics pen */ private void drawThermometer(int where, int degrees, boolean data_available, Graphics g) { if (data_available) { if (MIN_TEMP <= degrees && degrees <= MAX_TEMP) { g.setColor(Color.red); g.drawArc(where, TOP, WIDTH, WIDTH, 0, 180); g.drawLine(where, TOP+LENGTH, where+WIDTH, TOP+LENGTH); paintTemp(where, degrees, g); // show temperature } else { g.setColor(Color.red); g.drawString("Out of range", where, TOP+LENGTH); } g.drawString(degrees + "", where + TEXT_OFFSET, TOP+LENGTH+20); } else { g.drawString("No data", where, TOP+LENGTH); System.out.println("No data" ); } } /** paintTemp fills a thermometer with the appropriate temperature * @param where - the location of the thermometer * @param degrees - how high to fill it * @param g - the graphics pen */ private void paintTemp(int where, int degrees, Graphics g) { int x_center = where + WIDTH/2, // Mittelpunkt des Kreisbogens y_center = TOP + LENGTH; int radius = WIDTH/2 - 5; // Radius des Kreisbogens int x_tip = x_center // Spitze des Zeigers - (int) (radius * Math.cos(degrees * Math.PI/MAX_TEMP)), y_tip = y_center - (int) (radius * Math.sin(degrees * Math.PI/MAX_TEMP)); g.drawLine(x_center, y_center, x_tip, y_tip); } }