📄 temp.java
字号:
import java.applet.Applet;
import java.applet.AppletStub;
import java.awt.Graphics;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class Temp extends Applet {
Update timer;
public String temperature_str = "0.0";
public Font font1,font2,font3;
final int maxNumValues = 15;
double values[] = new double[maxNumValues];
int time[] = new int[maxNumValues];
int i = 0; // counting variable
double tempHigh = 76.0;
double tempLow = 74.0;
boolean first_time = true;
//----------------------------------------------------------------------------------
// Overridden applet functions (starting point of execution)
//----------------------------------------------------------------------------------
public void init() {
font1 = new Font("sans-serif", Font.BOLD, 34);
font2 = new Font("sans-serif", Font.BOLD, 20);
font3 = new Font("sans-serif", Font.BOLD, 10);
setBackground(Color.blue);
for(i = 0; i < maxNumValues; i++){
values[i] = 1000;
time[i] = (i + 1) - maxNumValues;
}
}
public void start(){
timer = new Update();
timer.start();
}
public void paint(Graphics g) {
drawText(g);
drawAxis(g);
}
public void stop(){
timer.stop();
}
//----------------------------------------------------------------------------------
// Nested Class Definitions
//----------------------------------------------------------------------------------
class Update extends Thread {
public Update(){
}
public void run() {
while(true){
setTemperature();
repaint();
try{
sleep(500);
}catch(InterruptedException e){
System.out.println("EXEPTION -- cannot sleep");
}
}
}
}
//----------------------------------------------------------------------------------
// Member functions
//----------------------------------------------------------------------------------
public void setTemperature(){
URL u;
URLConnection uc;
try {
u = new URL("http://" + getDocumentBase().getHost() + "/get_data?arg=nothing");
try {
uc = u.openConnection();
System.out.println("Connecting...");
uc.connect();
System.out.println("Connected...");
BufferedReader in = new BufferedReader(
new InputStreamReader(uc.getInputStream()) );
System.out.println("Reading a line ...");
temperature_str = in.readLine();
System.out.println("the line says: " + temperature_str);
in.close();
System.out.println("The input stream buffer is closed \n-- EXECUTION OK");
// increment the time
for(i = 0; i < maxNumValues; i++){
time[i]++;
}
// shift the values to the left
for(i = 0; i < (maxNumValues - 1); i++){
values[i] = values[i+1];
}
// store the new value in the last element of the array
String temp_str1 = temperature_str.substring(0, temperature_str.indexOf('.'));
String temp_str2 = temperature_str.substring(temperature_str.indexOf('.')+ 1);
values[maxNumValues - 1] = Integer.valueOf(temp_str1).floatValue()
+ (Integer.valueOf(temp_str2).floatValue()) / (float)10.0;
// auto scale
double val = values[maxNumValues - 1];
if(first_time){ //check for first time through
tempHigh =(double)((int) val + 1);
tempLow =(double)((int) val - 1);
first_time = false;
}else{
if( val >= tempHigh){
tempHigh = (double)((int)(val + 1));
}
if( val <= tempLow){
tempLow = (double)((int)(val - 1));
}
}
} catch (IOException e) {
System.err.println(e);
temperature_str = "***";
} // end try catch
} catch (MalformedURLException e) {
System.err.println(e);
temperature_str = "***";
}// end try catch
}// end set temperature
//----------------------------------------------------------------------------------
// Drawing functions
//----------------------------------------------------------------------------------
public void drawText(Graphics g){
g.setFont(font1);
g.setColor(Color.red);
g.drawString(temperature_str, 250, 34);
}
public void drawAxis(Graphics g){
g.setColor(Color.yellow);
g.setFont(font3);
// Chart box parameters
final int x0 = 50, y0 = 50; // absolute orgin of chart
final int xlen = 500, ylen = 250; // length of drawing plane
int ybot = y0 + ylen, xend = x0 + xlen; // maximum x and y values
int i = 0; // counting variable
//Draw the x and y axis
g.drawLine(x0, y0, x0, ybot); // y-axis
g.drawLine(x0, ybot, xend, ybot); // x-axis
// Y-axis Label Parameters
int numTicksY = 10;
int x = x0 - 35; // start drawing axis labels 35 px to the left of the axis
int y = ybot; // start drawing labels from the bottom (minimum value first)
// Draw the Y-axis Labels -- upon entry, brush location x and y (left and top)
// are initialized as offsets from (0,0) the top left corner of the window.
double yLabelVal = tempLow;
String label = "0.00";
int lbl_len;
for(i = 0; i <= numTicksY; i++){
// determine the length of the label
if(yLabelVal > 0 && yLabelVal < 10 ){
lbl_len = 3;
}else if ( yLabelVal == 0.0){
lbl_len = 1;
}else if (yLabelVal >= 100.0){
lbl_len = 5;
}else {
lbl_len = 4;
}
label = String.valueOf(yLabelVal).substring(0, lbl_len); //convert from float->string and truncate
g.drawString(label, x, y); //draw the label at(x,y)
yLabelVal += (double)(tempHigh - tempLow)/numTicksY; //increment temperature by one tick's worth
y -= ylen/numTicksY; //move the brush up one tick
}
// Draw Bars and Labels on the X-axis
int barWidth = xlen / maxNumValues;
double PixPerDegree = (double) ylen / (double)(tempHigh - tempLow); //pixels per degree
int xLabelVal;
x = x0;
y = ybot + 20; //draw labels 20px below the axis
for(i = 0; i < maxNumValues; i++){
if(time[i] > 0){
//write the label
xLabelVal = time[i];
label = String.valueOf(xLabelVal);
g.drawString(label, x, y);
// draw the bar
double barHeightRatio = (values[i] - tempLow) / (tempHigh - tempLow);
int barHeight = (int)( (double)ylen * barHeightRatio);
g.fillRect(x, ybot - barHeight, barWidth, barHeight); //x,y,width,height
}
x = x + barWidth;
}// end for
}//end draw axis
} //end Temp class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -