코드 사본은 다음과 같습니다.
패키지 graphicscanvas;
java.awt.borderlayout import;
java.awt.canvas import;
import java.awt.color;
java.awt.graphics 가져 오기;
import java.awt.image;
import java.awt.event.actionevent;
import java.awt.event.actionListener;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;
import javax.swing.jtextfield;
import javax.swing.timer;
import javax.swing.uimanager;
/**
* 시뮬레이션 된 혈압 미터, 고압, 저압
*
* @Author 팬 Junbin
* @Time 2013-12-10
*/
공공 계급 혈액 연장 jframe {
개인 정적 최종 최종 긴 SerialversionUID = 1L;
개인 이미지 ibuffer;
개인 mycanvas bloodcanvas = new mycanvas ();
Private Jtextfield HighPressText, LowPresstext;
// 캔버스 길이와 너비
개인 최종 int canvas_width = 400;
개인 최종 최종 int canvas_height = 800;
// 유리 쉘의 길이와 너비와 시작 좌표
개인 최종 int blood_width = 30;
개인 최종 int blood_height = 650;
개인 최종 int blood_x = canvas_width / 2 -blood_width / 2;
개인 최종 int blood_y = 50;
// 프레임 크기 및 시작 좌표
개인 최종 int frame_width = 120;
개인 최종 int frame_height = 720;
개인 최종 int frame_x = canvas_width / 2 -frame_width / 2;
개인 최종 int frame_y = blood_y -20;
// 0 스케일 라인의 수평 및 수직 좌표 및 길이
개인 최종 int Zoreline_y = blood_y + blood_height -10;
개인 최종 int zoreline_x = canvas_width / 2 + blood_width / 2;
개인 최종 int line_length = 8;
// 입력 고전압 및 저전압
개인 INT Highpressinput, Lowpressinput;
// 고압 수은 및 저압 수은 기둥의 동적 높이
int highpressheight = 0;
int lowpressheight = 0;
int startlow = blood_y;
// 높고 낮은 수은 타이머
타이머 Highpresstimer, Lowpresstimer;
공개 블러드 () {
Super ( "사용자 정의 혈압 모니터 모델-프리맨");
this.setDefaultCloseOperation (exit_on_close);
this.setBounds (300, 50, canvas_width, canvas_height + 20);
// 프레임 워크 북쪽에 제어를 추가합니다
JPANEL TOPPANEL = 새로운 jpanel ();
this.add (toppanel, borderlayout.north);
HighPressText = New Jtextfield (5);
LowPressText = New Jtextfield (5);
JBUTTON PRESSBUTTON = NEW JBUTTON ( "Show");
PressButton.addactionListener (New ActionListener () {
@보수
public void actionperformed (ActionEvent arg0) {
HighPressInput = integer.parseint (HighPressText.getText ());
LowPressInput = integer.parseint (lowpresstext.getText ());
ActionListener HighpresstaskperFormer = New ActionListener () {
public void ActionPerformed (ActionEvent evt) {
// 높이를 1 픽셀/0.01s로 늘리고 입력 요구 사항이 충족 될 때까지 타이밍을 중지하십시오.
HighpressHeight += 1;
Bloodcanvas.repaint ();
if (highpressheight == HighPressInput * 2) {
HighPresstimer.stop ();
// 저압 수은 열 타이머는 고압 타이머 내부에 중첩되어 있으며 (고압 우선, 저압)
startLow = Zoreline_y- HighpressHeight;
ActionListener LowpresstaskperFormer = New ActionListener () {
public void ActionPerformed (ActionEvent evt) {
LowpressHeight += 1;
Bloodcanvas.repaint ();
if (lowpressheight == Zoreline_y
- Lowpressinput * 2 -Startlow)
Lowpresstimer.stop ();
}
};
Lowpresstimer = New Timer (10, Lowpresstaskperformer);
Lowpresstimer.start ();
}
}
};
// 0.01 초마다 실행되는 이벤트 리스너를 정의합니다
Highpresstimer = New Timer (10, Highpresstaskperformer);
HighPresstimer.start ();
}
});
toppanel.add (New Jlabel ( "고전압 값", jlabel.center));
toppanel.add (HighPressText);
toppanel.add (New Jlabel ( "저전압 값", jlabel.center));
toppanel.add (lowpresstext);
// toppanel.add (New Jlabel ( "심박수", jlabel.center));
toppanel.add (pressbutton);
// 중앙 영역에 캔버스를 추가합니다
this.add (bloodcanvas, borderlayout.center);
this.setResizable (false);
this.set -Visible (true);
}
/**
* 혈압 미터의 캔버스를 다시 그리기
*/
클래스 MyCanvas는 캔버스를 확장합니다 {
공개 공허 페인트 (그래픽 g) {
// 경계를 그리십시오
g.setcolor (color.black);
G.Draw3dRect (frame_x, frame_y, frame_width, frame_height, true);
// 유리 쉘을 그립니다
g.setcolor (color.orange);
g.fill3drect (blood_x, blood_y, blood_width, blood_height, true);
// 고압 수은 컬럼
g.setcolor (color.red);
g.fill3drect (blood_x, zoreline_y -highpressheight, blood_width,
HighpressHeight, True);
// 저압 고압 수은 컬럼
g.setcolor (color.orange);
g.fill3drect (blood_x, startlow, blood_width, lowpressheight, true);
// 하단에 수은 공을 그립니다
g.setcolor (color.red);
g.filloval (canvas_width / 2-30, Zoreline_y -5, 60, 60);
// 오른쪽의 0 스케일 라인의 시작 진드기 및 좌표 (스케일 라인의 수직 좌표는 점차 line_y에 의해 변경됩니다)
int rightstartDegree = 0;
int line_y = Zoreline_y;
for (; line_y> blood_y; line_y- = 2) {
// 2 픽셀은 최소 지수 1도입니다.
g.setcolor (color.black);
G.DrawLine (Zoreline_x, line_y, Zoreline_x + line_length, line_y);
// 최소 10도 인덱스마다 10도 진드기를 그립니다
if (line_y % 20 == 10) {
g.setcolor (color.blue);
G.DrawLine (Zoreline_X, Line_y,
zoreline_x + line_length * 2, line_y);
G.DrawString (RightStartDegree + "", Zoreline_x
+ line_length * 3, line_y + 4);
RightStartDegree += 10;
}
}
// 왼쪽의 0 스케일 라인의 시작 진드기 및 좌표 (스케일 라인의 수직 좌표는 점차 line_y에 의해 변경됩니다)
int leftstartDegree = 0;
int leftline_y = Zoreline_y;
for (; leftline_y> blood_y; leftline_y- = 6) {
// 6 픽셀 포인트는 최소 지수 1도입니다.
g.setcolor (color.black);
G.Drawline (Blood_x, Leftline_y, Blood_x- Line_length,
Leftline_y);
// 최소 10도 인덱스마다 10도 진드기를 그립니다
if (leftline_y % 20 == 10) {
g.setcolor (color.blue);
G.Drawline (Blood_x, Leftline_y, Blood_x- Line_length * 2,
Leftline_y);
G.DrawString (LeftStartDegree + "", Blood_x -Line_length
* 4, Leftline_y + 4);
LeftStartDegree += 10;
}
}
}
/**
* 이중 버퍼링 기술 : 복잡한 계산 속도는 화면 디스플레이보다 느리고 버퍼링은 화면 깜박 거리는 문제를 해결하는 데 사용됩니다.
*/
@보수
공개 무효 업데이트 (그래픽 g) {
if (ibuffer == null) {
iBuffer = createImage (this.getSize (). 너비,
this.getSize (). 높이);
}
그래픽 gbuffer = ibuffer.getGraphics ();
gbuffer.setColor (getbackground ());
gbuffer.fillRect (0, 0, this.getSize (). 너비, this.getSize (). 높이);
페인트 (Gbuffer);
gbuffer.dispose ();
G.DrawImage (Ibuffer, 0, 0, this);
}
}
public static void main (String [] args) {
// 시스템 모양에 인터페이스의 모양을 설정합니다.
노력하다 {
UIMANAGER.SETLOOKANDFEEL (uimanager.getSystemLookandFeEleclassName ());
} catch (예외 e) {
e.printstacktrace ();
}
새로운 혈액 ();
}
}