Gamepanel

3237 단어 자바
패키지 바 시 보 혁;import javax.swing.*;
import javax.swing.Timer; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.; public class Gamepanel extends JPanel implements KeyListener,ActionListener {
int N;//     
int m;//      
int a[] = new int[2];//      
int p=1;
Random random = new Random();//    
Timer timer = new Timer(100,this);
boolean isStart =false;//      
boolean isFail = false;//      
JTextField jtx=new JTextField(25);
//   
	public Gamepanel(){
		init();
		//         
		this.setFocusable(true);
		this.addKeyListener(this);
		timer.start();//      
	}
	//   

public void init(){
	
	N = random.nextInt(100);
	m = random.nextInt(10);
}
protected void paintComponent(Graphics g){
	super.paintComponent(g);//  
	this.setBackground(Color.red);//       
	//Data.header.paintIcon(this, g, 25, 11);//   
	g.fillRect(0,0,900,720);//      
	if(isStart==false){
		g.setColor(Color.red);
		g.setFont(new Font("    ",Font.BOLD,40));
		g.drawString("    ",350,350);
	}		
	if(isFail){
		//    
		g.setColor(Color.RED);
		g.setFont(new Font("    ",Font.BOLD,40));
		g.drawString("   ", 300, 300);
	}
	g.drawString("    :", 10, 500);
	g.drawString("    n   ,             ", 10, 550);
	g.drawString("    n   ,             ", 10, 600);
	//  
	g.setColor(Color.blue);
	g.setFont(new Font("    ",Font.BOLD,18));
	g.drawString("N   "+N, 200, 100);
	g.drawString("m     "+m, 200, 200);
	g.drawString("  "+a[1], 200,300);
	g.drawString("   "+a[0], 200, 400);
}
@Override
public void keyPressed(KeyEvent e) {
	// TODO Auto-generated method stub
	int keyCode  = e.getKeyCode();
	if(keyCode==KeyEvent.VK_SPACE){
		if(isFail){//  ,      
			isFail=false;
			init();//    ,     
		}else{//    
			isStart = !isStart;
		}
		//isStart = !isStart;
		repaint();//    
	}
	
	if(N%(m+1)==0){
		//     
		
	}else {
		//  AL  
		a[0]=N%(m+1);
		N  = N-a[0];
	}
	if(keyCode==KeyEvent.VK_1){
		p=1;
	}else if(keyCode==KeyEvent.VK_2){
		p=2;
	}else if(keyCode==KeyEvent.VK_3){
		p=3;
	}else if(keyCode==KeyEvent.VK_4){
		p=4;
	}else if(keyCode==KeyEvent.VK_5){
		p=5;
	}else if(keyCode==KeyEvent.VK_6){
		p=6;
	}else if(keyCode==KeyEvent.VK_7){
		p=7;
	}else if(keyCode==KeyEvent.VK_8){
		p=8;
	}else if(keyCode==KeyEvent.VK_9){
		p=9;
	}
}
@Override
public void actionPerformed(ActionEvent e) {
	//          ,        
	if(isStart && isFail==false){
			if(p==1){
				a[0]=1;
				a[1]=(m+1)-a[0];
			}else if(p==2){
				a[0]=2;
				a[1]=(m+1)-a[0];
			}else if(p==3){
				a[0]=3;
				a[1]=(m+1)-a[0];
			}else if(p==4){
				a[0]=4;
				a[1]=(m+1)-a[0];
			}else if(p==5){
				a[0]=5;
				a[1]=(m+1)-a[0];
			}else if(p==6){
				a[0]=6;
				a[1]=(m+1)-a[0];
			}else if(p==7){
				a[0]=7;
				a[1]=(m+1)-a[0];
			}else if(p==8){
				a[0]=8;
				a[1]=(m+1)-a[0];
			}else if(p==9){
				a[0]=9;
				a[1]=(m+1)-a[0];
			}
			N = N - (m+1);
			if(N==0){//    
				isFail=true;
		    }
		repaint();
	}
	timer.start();//      
	
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

좋은 웹페이지 즐겨찾기