package varia;
/*
* MonitorApplet.java
*/
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
public class MonitorApplet extends JApplet {
private Monitor monitor;
public void init() {
Tester tester = new Tester();
new Thread(tester).start();
monitor = new Monitor(tester);
getContentPane().add(monitor, BorderLayout.CENTER);
Thread monitorThread = new Thread(monitor);
monitorThread.setPriority(Thread.MIN_PRIORITY);
monitorThread.start();
}
}
class Monitor extends JPanel implements Runnable {
private long sleepAmount = 1000;//wie oft aktualisiert wird (in Millisekunden)
private int histCount = 400;// maximale Lauflänge der Graphik
private Object object;
private BufferedImage bimg;
private Graphics2D big;
private Font font = new Font("Times New Roman", Font.PLAIN, 11);
private float valuesArray[];
private int w, h, ptNum, ascent, descent, columnInc;
private Rectangle graphOutlineRect = new Rectangle();
private Rectangle2D freeRect = new Rectangle2D.Float();
private Rectangle2D valueRect = new Rectangle2D.Float();
private Line2D graphLine = new Line2D.Float();
private Color graphColor = new Color(46, 139, 87);
private Color mfColor = new Color(0, 100, 0);
private String str;
public Monitor(final Object object) {
this.object = object;
setBackground(Color.black);
valuesArray = new float[histCount];
ptNum = 0;
}
public void paintComponent(final Graphics g) {
super.paintComponent(g);
if (big == null){
Dimension d = getSize();
if (d.width != w || d.height != h) {
w = d.width;
h = d.height;
bimg = (BufferedImage) createImage(w, h);
big = bimg.createGraphics();
big.setFont(font);
FontMetrics fm = big.getFontMetrics(font);
ascent = (int) fm.getAscent();
descent = (int) fm.getDescent();
}
}
big.setBackground(getBackground());
big.clearRect(0,0,w,h);
plotValues(w,h, ((Tester)object).getWert());
g.drawImage(bimg, 0, 0, this);
}
public void plotValues(final int width, final int height, final float wert) {
float value = wert;
float total = 500000f;// hier hab ich auch nur einen Testwert eingesetzt
big.setColor(Color.green);
// Print Maximum
big.drawString(String.valueOf((int)total) + "Max ", 4.0f, ascent+0.5f);
big.setColor(Color.yellow);
big.drawString("Monitor", width/2, ascent+0.5f);
// Print the value
str = String.valueOf((int)value);
big.setColor(Color.green);
big.drawString(str, 4, height-descent);
// Calculate remaining size
float ssH = ascent + descent;
float remainingHeight = (float) (height - (ssH*2) - 0.5f);
float blockHeight = remainingHeight/10;
float blockWidth = 20.0f;
float remainingWidth = (float) (width - blockWidth - 10);
// .. Free ..
big.setColor(mfColor);
int usage = (int) (((total - value) / total) * 10);
int i = 0;
for ( ; i < usage ; i++) {
freeRect.setRect(5,(float) ssH+i*blockHeight,
blockWidth,(float) blockHeight-1);
big.fill(freeRect);
}
// .. Value ..
big.setColor(Color.green);
for ( ; i < 10; i++) {
valueRect.setRect(5,(float) ssH+i*blockHeight,
blockWidth,(float) blockHeight-1);
big.fill(valueRect);
}
// .. Draw History Graph ..
if (remainingWidth <= 30) remainingWidth = (float)30;
if (remainingHeight <= ssH) remainingHeight = (float)ssH;
big.setColor(graphColor);
int graphX = 30;
int graphY = (int) ssH;
int graphW = (int) remainingWidth;
int graphH = (int) remainingHeight;
graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
big.draw(graphOutlineRect);
int graphRow = graphH/10;
// .. Draw row ..
for (int j = graphY; j <= graphH+graphY; j += graphRow) {
graphLine.setLine(graphX,j,graphX+graphW,j);
big.draw(graphLine);
}
// .. Draw animated column movement ..
int graphColumn = graphW/15;
if (columnInc == 0) {
columnInc = graphColumn;
}
for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) {
graphLine.setLine(j,graphY,j,graphY+graphH);
big.draw(graphLine);
}
--columnInc;
// save history.
valuesArray[ptNum] = value;
big.setColor(Color.yellow);
int w1; // width of history.
if (ptNum > graphW) w1 = graphW;
else w1 = ptNum;
for (int j=graphX+graphW-w1, k=ptNum-w1; k < ptNum;
k++, j++) {
if (k != 0) {
if (valuesArray[k] != valuesArray[k-1]) {
int h1 = (int)(graphY + graphH * ((total -valuesArray[k-1])/total));
int h2 = (int)(graphY + graphH * ((total -valuesArray[k])/total));
big.drawLine(j-1, h1, j, h2);
} else {
int h1 = (int)(graphY + graphH * ((total -valuesArray[k])/total));
big.fillRect(j, h1, 1, 1);
}
}
}
if (ptNum+2 == valuesArray.length) {
// throw out oldest point
for (int j = 1;j < ptNum; j++) {
valuesArray[j-1] = valuesArray[j];
}
--ptNum;
} else {
ptNum++;
}
}
public void run() {
while (true) {
repaint();
try { Thread.sleep(sleepAmount); } catch (InterruptedException e) { break; }
}
}
}
class Tester implements Runnable {// Test thread to generate values
private float testWert;
public Tester() {
}
public void run() {
int sign = 1;
while(true){//Hier wird der testWert verändert (Testfunktion):
try {Thread.sleep(100);} catch (Exception x){}
if(testWert > 200000) sign = -1;
if(testWert < 10000) sign = 1;
testWert += sign*1000;
}
}
public float getWert(){
return testWert;
}
}