본문 바로가기
프로그래밍/② JAVA(자바) 프로젝트

② JAVA(자바) 프로젝트-16 쓰레드를 이용한 라벨 게이지 채우기 == Fill the label gauge using threads

by ronul 2017. 5. 10.
300x250

 

키를 계속 누르면 라벨을 증가시키게 되는데 점점 차는 모습이 게이지가 차게 되는 것처럼 보이는 프로그램입니다.

나중에 시간을 이용한 채우기를 사용하면 좋을 거 같습니다.

소스입니다.

 

 

 

 

 

fillGage.java 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class labelAll extends JLabel{
 int barSize=0;
 int maxBarSize;
 labelAll(int maxBarSize){
  this.maxBarSize=maxBarSize;
 }
 
 public void paintComponent(Graphics g){
  super.paintComponent(g);
  g.setColor(Color.red);
  int width=(int)(((double)(this.getWidth()))
    /maxBarSize*barSize);
  if(width==0)return;
  g.fillRect(0, 0, width, this.getHeight());
 }
 synchronized void fill(){
  if(barSize==maxBarSize){
   try{
    wait();
    
   }catch (InterruptedException e){return;}
  }
  barSize++;
  repaint();
  notify();
 }
 synchronized void consume(){
  if(barSize==0){
   try{
    wait();
    
   }catch (InterruptedException e){return;}
  }
  barSize--;
  repaint();
  notify();
  
  }
 }
 class ConsumerThread extends Thread{
  labelAll bar;
 
 ConsumerThread(labelAll bar){
  this.bar=bar;
  
 }
 public void run(){
  while(true){
   try{
    sleep(200);
    bar.consume();
   }catch(InterruptedException e){return;}
   }
 }
 
}


public class fillGage extends JFrame {
 labelAll bar=new labelAll(100);
 fillGage(String title){
  super(title);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container c=getContentPane();
  c.setLayout(null);
  bar.setBackground(Color.blue);
  bar.setOpaque(true);
  bar.setLocation(20,50);
  bar.setSize(300,20);
  c.add(bar);
  c.addKeyListener(new KeyAdapter(){
   public void keyPressed(KeyEvent e){
    bar.fill();
    
   }
  });
  setSize(350,200);
  setVisible(true);
  
  c.requestFocus();
  ConsumerThread th=new ConsumerThread(bar);
  th.start();
 }
 public static void main(String[] args){
  new fillGage("키를 빨리 눌러 바 채우기");
 }

}

300x250

댓글