java入門 第五天之作業答案 及 接口 簡介

作業答案,沒有標準答案,實現了就好。

package day05;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 小球反彈案例
 */
public class MyBall {
	public static void main(String [] args){
		//1.
		JFrame frame = new JFrame("MyBall");
		frame.setSize(400,500);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//2.
		MyPanel panel = new MyPanel();
		panel.setBackground(Color.GREEN);
		frame.add(panel);
		frame.setVisible(true);
		//3.
		panel.drop();
		
	}
}

class MyPanel extends JPanel{
	//小球的座標
	int x = 30 ;
	int y = 30 ; 
	//小球的方向
	int dir = 1 ;  // 1↘ 2↙ 3↖ 4↗ 
	public void paint(Graphics g){
		super.paint(g);
		//畫小球
		g.setColor(Color.BLUE);
		g.fillOval(x, y, 30, 30);
	}	
	
	//將小球動的邏輯封裝成一個方法
	public void drop(){
		while(true){
			//小球的運動
			if(dir == 1){
				x ++;
				y ++;
			}else if(dir == 2){
				x -- ;
				y ++ ;
			}else if(dir == 3){
				x -- ;
				y -- ;
			}else if(dir ==4){
				x ++ ;
				y -- ;
			}
			//判斷小球撞牆反彈
			//上
			if(y <=0){
				if(dir ==4){
					dir = 1;
				}
				if(dir == 3){
					dir = 2;
				}
			}
			//下
			if(y >=440){
				if(dir ==2){
					dir = 3;
				}
				if(dir == 1){
					dir = 4;
				}
			}
			//左
			if(x <= 0){
				if(dir == 3){
					dir = 4 ;
				}
				if(dir ==2){
					dir =1 ;
				}
			}
			//右
			if(x >=360){
				if(dir == 4){
					dir = 3;
				}
				if(dir ==1){
					dir = 2 ;
				}
			}
//			for(int i =0 ;i <10000000;i++){
//				
//			}
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//重繪
			repaint();
		}
	}
}


接口簡介

接口
...既是....(extends)又是....(implements)...還是....(接口)


比如

鼠標事件 MouseListener
 MyMouseBall extends JPanel implements MouseListener
 java是單繼承,多實現
 (一個親爹,多個乾爹)

package day05;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyMouseBall {
	public static void main(String [] args){
		JFrame frame = new JFrame("MyMouseBall");
		frame.setSize(400,500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		//
		MyMouseBallPanel mmbp = new MyMouseBallPanel();
		frame.add(mmbp);
		//給面板對象添加監聽
		mmbp.addMouseListener(mmbp);
		frame.setVisible(true);
	}
}
class MyMouseBallPanel extends JPanel implements MouseListener{
	
	
	public void paint(Graphics g) {
		super.paint(g);
		
	}
	//鼠標點擊
	//MouseEvent 事件對象
	public void mouseClicked(MouseEvent e) {
		//獲取鼠標點擊的位置
		int x = e.getX();
		int y = e.getY();
		System.out.println("x:"+x+",y:"+y);
	
	}
	//鼠標進入
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	//鼠標退出
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	//鼠標按下
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	//鼠標釋放
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}












大招:alt + /  自動提示

超必殺:直接點錯誤提示,自動 生成缺少代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章