// TempLanRH36 (アプリケーション型)
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
public class Plotting extends JPanel{
Point prevpTM,prevpRH,newpTM,newpRH;
Sensor sensor;
int xMax, yMax, yConst;
BufferedImage bi;
Graphics G;
public static String xport_ip = "192.168.24.50"; // XPortのIPアドレスをここで宣言しておく
String buffTM;
String buffRH;
public Plotting(Point startp){
prevpTM = prevpRH = newpTM = newpRH = startp;
xMax = 750;
yConst = 10;
yMax = 550;
sensor = new Sensor();
}
public void doPlot(){
String buff01;
String sTM = "TM";
String sRH = "RH";
int x;
int yTM = 0;
int yRH = 0;
double numTM;
double numRH;
while (true){
if(newpTM.x > xMax ){
prevpTM.x = 45;
prevpRH.x = 45;
initG();
}
buff01 = sensor.readData();
if( buff01.equals(sTM) ) {
buffTM = sensor.readData(); // 温度データーを受け取る
numTM = Double.parseDouble(buffTM);
yTM = yMax - (int)((double)yConst * numTM); //10倍して整数化する
}
if( buff01.equals(sRH) ) {
buffRH = sensor.readData(); // 湿度データーを受け取る
numRH = Double.parseDouble(buffRH);
yRH = yMax - (int)((double)yConst * numRH / 2); //10倍して整数化し、1/2のスケーリングをしておく
}
x = prevpTM.x + 1;
newpTM = new Point(x, yTM);
newpRH = new Point(x, yRH);
repaint(); //上で描画用パラメータをセットし、描画をリクエストする
try{
Thread.sleep(60);
}
catch (InterruptedException ie){
ie.printStackTrace();
}
prevpTM = newpTM;
prevpRH = newpRH;
}
}
public void initG(){
G = bi.createGraphics();
G.setColor(Color.white); //背景色
G.fillRect(0, 0, getWidth(), getHeight());
G.setColor(Color.black);
G.setFont(new Font("Serif",Font.BOLD,18));
G.drawLine( 45, 550, 750, 550); // 時間軸
G.drawLine( 50, 555, 50, 50); // 温度軸
G.drawLine( 45, 500, 750, 500);
G.drawLine( 45, 450, 750, 450);
G.drawLine( 45, 400, 750, 400);
G.drawLine( 45, 350, 750, 350);
G.drawLine( 45, 300, 750, 300);
G.drawLine( 45, 250, 750, 250);
G.drawLine( 45, 200, 750, 200);
G.drawLine( 45, 150, 750, 150);
G.drawLine( 45, 100, 750, 100);
G.drawLine( 45, 50, 750, 50);
G.setColor(Color.blue);
G.drawString(" 0",20,555); // 0℃
G.drawString(" 5",20,505); // 5℃
G.drawString("10",20,455); // 10℃
G.drawString("15",20,405); // 15℃
G.drawString("20",20,355); // 20℃
G.drawString("25",20,305); // 25℃
G.drawString("30",20,255); // 30℃
G.drawString("35",20,205); // 35℃
G.drawString("40",20,155); // 40℃
G.drawString("45",20,105); // 45℃
G.drawString("50",20,55); // 50℃
G.setColor(Color.green);
G.drawString(" 0",750,555); // 0%
G.drawString("10",750,505); // 10%
G.drawString("20",750,455); // 20%
G.drawString("30",750,405); // 30%
G.drawString("40",750,355); // 40%
G.drawString("50",750,305); // 50%
G.drawString("60",750,255); // 60%
G.drawString("70",750,205); // 70%
G.drawString("80",750,155); // 80%
G.drawString("90",750,105); // 90%
G.drawString("100",750,55); // 100%
}
public void paintComponent(Graphics g){
if (bi == null){ // BufferdImageを用意する
bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
initG();
}
super.paintComponent(g);
Font CurrentFont = G.getFont();
Font font = new Font("Serif", Font.PLAIN , 24);
G.setFont(font);
G.setColor(Color.white);
G.fillRect(0, 0, 800, 40);
G.setColor(Color.black);
G.drawString(buffTM +"℃ " + buffRH + "%", 50, 35);
G.setColor(Color.blue);
G.fillOval(newpTM.x, newpTM.y, 2, 2);
G.setColor(Color.green);
G.fillOval(newpRH.x, newpRH.y, 2, 2);
g.drawImage(bi,0,0,null); //それを画面に描画する
}
public static void main(String[] args){
Plotting plt;
JFrame frame = new JFrame("TempLanRH 36");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(plt = new Plotting(new Point(45, 300)));
frame.setSize(800, 600);
frame.setVisible(true);
plt.doPlot();
}
}
/* センサー */
class Sensor{
String xport_ip = Plotting.xport_ip;
int port = 10001;
Socket xport_socket;
BufferedReader socketin;
String buff;
public Sensor(){
try
{
xport_socket = new Socket(xport_ip, port);
socketin = new BufferedReader(new InputStreamReader(xport_socket.getInputStream()));
}
catch(IOException e){}
}
public String readData(){
try
{
buff = socketin.readLine();
}
catch(IOException e){
buff = null;
}
return buff;
}
} |