1. 程式人生 > >第一次的期末作業。Monica7

第一次的期末作業。Monica7

Gui的介面設計

public class Gui extends JFrame{
static Gui gui;
JButton jb1;
JButton jb2;
JPanel jp1;
//JPanel jp2;
JLabel jl1;
ArrayList al;
private String strCopyright="@Monica7";
JLabel jl2;

  public void Ku(){
  this.setVisible(true);
  this.setTitle("彈球");
  this.setSize(865, 500);
  
  JPanel jp1=new JPanel();
  JPanel jp2=new JPanel();
  this.add(jp1);

  jp1.setLayout(new GridLayout(3,1));

  JLabel jl1=new JLabel();
  JLabel jl2=new JLabel();

  jl1.setText("你見過球嗎?自動彈球");
  jl1.setFont(new Font("華文行楷",Font.ITALIC+Font.BOLD,70));
  jp1.add(jl1);
  
  

  
  
  JButton jb1=new JButton();
  JButton jb2=new JButton();
   jp1.add(jb1);
   jp1.add(jb2);
   jb1.setText("開始");
   jb1.setFont(new Font("華文行楷",Font.ITALIC+Font.BOLD,100));
jb1.addActionListener(new Action());
   jb2.setText("結束");
   jb2.setFont(new Font("華文行楷",Font.ITALIC+Font.BOLD,100));
  jb2.addActionListener(new Action());
   
   al=Util.getSizeAndposition();
   this.setBounds(al.get(0),al.get(1),al.get(2),al.get(3));
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

  public JButton getJb1() {
		return jb1;
	}



	public void setJb1(JButton jb1) {
		this.jb1 = jb1;
	}
	public static Gui getGui(){
		if(gui==null)gui=new Gui();
		return gui;
		
	}
	

	public static void closethis() {
		// TODO Auto-generated method stub
		System.exit(0);
	}

}

Util 實現功能類
package ballgame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;

public class Util {
public static ArrayListgetSizeAndposition(){
ArrayList al=new ArrayList();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int ScreenWidth=(int)dim.getWidth();
int ScreenHeigt=(int)dim.getHeight();
int Width=ScreenWidth / 2;
int Height=ScreenHeigt / 2;
int posX = ( ScreenWidth - Width) / 2;
int posY = ( ScreenHeigt - Height) / 2;
al.add(posX);
al.add(posY);
al.add(Width);
al.add(Height);
return al;
}
}

Action功能
package ballgame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
;

public class Action implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
	JButton jb1=Gui.getGui().getJb1();
	if(e.getActionCommand()=="開始"){
		
		ThreadDemo ttDemo=new ThreadDemo();
		new Thread(ttDemo).start();
	     

	
	}if(e.getActionCommand()=="結束"){
	System.exit(0);

	
}
}


}

彈球小功能
package ballgame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;
public class BallGame extends JFrame {

Image ball=Toolkit.getDefaultToolkit().getImage("images/ball.png");
Image desk=Toolkit.getDefaultToolkit().getImage("images/desk.jpg");

double x=100;
double y=100;
 boolean right=true;
 double degree = 3.14/3;
public void paint(Graphics g){
	//System.out.println("畫了一次");	
	 g.drawImage(desk, 0, 0, null);
     g.drawImage(ball, (int)x,(int)y, null); 	
     
     x  = x+ 10*Math.cos(degree);
     y  = y +10*Math.sin(degree); 
     if(y>500-40-30||y<40+40){
         degree = -degree;
     }
     
     if(x<40||x>856-40-30){
         degree = 3.14 - degree;
     }
 
}

void launchFrame(){
	ArrayList<Integer> al=new ArrayList<Integer>();
	al=Util.getSizeAndposition();
       this.setBounds(al.get(0),al.get(1),al.get(2),al.get(3));
      // setSize(865,500);
	setVisible(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	while(true){
		repaint();
	try{
		Thread.sleep(40);
	}catch(Exception e){
		e.printStackTrace();
	}
	}
}

}
執行緒
package ballgame;

public class ThreadDemo implements Runnable {

private int i;
public void run(){
	for(; i<100 ;i++){
		
		BallGame bb=new BallGame();
		bb.launchFrame();
	}
}
public static void main(String[] args) {
	for(int i = 0 ;i<100; i++){
	
		if(i==20){
			ThreadDemo td = new ThreadDemo();
			new Thread(td).start();
			
		}
	}

}

}
主方法
package ballgame;

public class Main {

public static void main(String[] args) {
Gui gg=new Gui();
gg.Ku();

}
}