📄 speedgraph.java
字号:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
/** This class receives the speed of the user from CalculateSpeed and shows the speed graph
* accordingly.
*/
class SpeedGraph extends JPanel{
Surface surf;
SpeedGraph(){
setBackground(getBackground());
setLayout(new BorderLayout());
setBorder(new CustomTitledBorder("Speed Graph"));
setPreferredSize(new Dimension(225,225));
add(surf=new Surface());
}
public void setSpeedSource(CalculateSpeed speed){
surf.setSpeedSource(speed);
}
public void lessonStarted(){
surf.lessonStarted();
}
}
class Surface extends JPanel implements Runnable{
public Thread thread;
public long sleepAmount = 500;
private int w, h;
private BufferedImage bimg;
private Graphics2D big;
private Font font = new Font("Times New Roman", Font.PLAIN, 11);
private int columnInc;
private int pts[];
private int ptNum;
private int ascent, descent;
private Rectangle graphOutlineRect = new Rectangle();
private Rectangle2D mfRect = new Rectangle2D.Float();
private Rectangle2D muRect = new Rectangle2D.Float();
private Line2D graphLine = new Line2D.Float();
private Color graphColor = new Color(100,150,255);
private Color mfColor = new Color(0, 0, 100);
private CalculateSpeed speed;
private String name;
public Surface() {
setBackground(Color.black);
MainScreen screen=MainScreen.getInstance();
UserData user=screen.getCurrentUser();
name=user.getName();
}
public void setSpeedSource(CalculateSpeed speed){
this.speed=speed;
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getPreferredSize() {
return new Dimension(135,80);
}
public void paint(Graphics g) {
if (big == null||speed==null) {
return;
}
big.setBackground(getBackground());
big.clearRect(0,0,w,h);
// .. Draw allocated and used strings ..
big.setColor(Color.yellow);
big.drawString(name, 4.0f, (float) ascent+0.5f);
big.drawString(speed.getRawSpeed()+" WPM", 4, h-descent);
// Calculate remaining size
float ssH = ascent + descent;
float remainingHeight = (float) (h - (ssH*2) - 0.5f);
float blockHeight = remainingHeight/10;
float blockWidth = 20.0f;
float remainingWidth = (float) (w - blockWidth - 10);
int graphX = 30;
int graphY = (int) ssH;
int graphW = w - graphX - 5;
int graphH = (int) remainingHeight;
// .. Memory Used ..
// .. Draw History Graph ..
big.setColor(graphColor);
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/10;
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;
if (pts == null) {
pts = new int[graphW];
ptNum = 0;
} else if (pts.length != graphW) {
int tmp[] = null;
if (ptNum < graphW) {
tmp = new int[ptNum];
System.arraycopy(pts, 0, tmp, 0, tmp.length);
} else {
tmp = new int[graphW];
System.arraycopy(pts, pts.length-tmp.length, tmp, 0, tmp.length);
ptNum = tmp.length - 2;
}
System.arraycopy(tmp, 0, pts, 0, tmp.length);
} else {
big.setColor(Color.cyan);
int value=(int)(speed.getRawSpeed()+.001);
float factor=graphH/60;
pts[ptNum]=(int)(graphY+graphH-factor*value); //-------------->>>>>>>
if(pts[ptNum]>(graphY+graphH))
pts[ptNum]=graphY+graphH;
else if(pts[ptNum]<graphY)
pts[ptNum]=graphY;
for (int j=graphX+graphW-ptNum, k=0;k < ptNum; k++, j++) {
if (k != 0) {
if (pts[k] != pts[k-1]) {
big.drawLine(j-1, pts[k-1], j, pts[k]);
} else {
big.fillRect(j, pts[k], 1, 1);
}
}
}
big.setColor(Color.yellow);
int i=1;
int limit=(int)((value*factor*10)/graphH);
if(limit>10)
limit=10;
blockHeight=graphH/10;
blockWidth=20;
for ( ; i <= limit ; i++) {
mfRect.setRect(5,(float) ((graphY+graphH+1)-i*blockHeight),
blockWidth,(float) blockHeight-1);
big.fill(mfRect);
}
big.setColor(new Color(50,50,0));
for ( ; i <=10; i++) {
muRect.setRect(5,(float) ((graphY+graphH+1)-i*blockHeight),
blockWidth,(float) blockHeight-1);
big.fill(muRect);
}
if (ptNum+2 == pts.length) {
// throw out oldest point
for (int j = 1;j < ptNum; j++) {
pts[j-1] = pts[j];
}
--ptNum;
} else {
ptNum++;
}
}
g.drawImage(bimg, 0, 0, this);
}
public void lessonStarted() {
if(speed==null)
return;
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName("SpeedGraph");
thread.start();
}
public synchronized void stop() {
thread = null;
notify();
}
public void run() {
Thread me = Thread.currentThread();
while (thread == me && !isShowing() || getSize().width == 0) {
try {
thread.sleep(500);
} catch (InterruptedException e) { return; }
}
while (thread == me && isShowing()) {
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();
}
repaint();
try {
thread.sleep(sleepAmount);
} catch (InterruptedException e) { break; }
}
thread = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -