Created
February 20, 2014 07:39
-
-
Save fcflyinsky/9108673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.JFrame; | |
public class Ex13_105 extends JFrame implements MouseListener { | |
// 宣告int,存放滑鼠座標 | |
int x, y; | |
public Ex13_105() { | |
// 設定視窗標題 | |
setTitle("視窗繪圖"); | |
// 設定視窗大小 | |
setSize(300, 300); | |
// 設定監聽,指向本身 | |
addMouseListener(this); | |
// 設定視窗大小不可更改 | |
setResizable(false); | |
// 顯示視窗物件 | |
this.setVisible(true); | |
} | |
public static void main(String[] args) { | |
new Ex13_105(); | |
} | |
@Override | |
// 當滑鼠按下物件時 | |
public void mousePressed(MouseEvent e) { | |
// 得到滑鼠座標 | |
x = e.getX(); | |
y = e.getY(); | |
// 重新繪製視窗 | |
repaint(); | |
} | |
public void update(Graphics g) { | |
// 設定背景顏色 | |
g.setColor(this.getBackground()); | |
// 畫一個填滿的矩形 | |
g.fillRect(0, 0, getWidth(), getHeight()); | |
// 呼叫paint(); | |
paint(g); | |
} | |
public void paint(Graphics g) { | |
// 清除畫面 | |
// g.clearRect(起始x, 起始y, 結束x, 結束y); | |
g.clearRect(0, 0, getWidth(), getHeight()); | |
// 設定繪圖顏色 | |
g.setColor(Color.red); | |
// 在指定位置繪出字串 | |
g.drawString("滑鼠座標:" + x + "," + y, x, y); | |
} | |
// 當滑鼠點選物件時 | |
public void mouseClicked(MouseEvent e) { | |
} | |
@Override | |
// 當滑鼠進入某物件範圍時 | |
public void mouseEntered(MouseEvent e) { | |
} | |
@Override | |
// 當滑鼠退出某物件範圍時 | |
public void mouseExited(MouseEvent e) { | |
} | |
@Override | |
// 當滑鼠放開物件時 | |
public void mouseReleased(MouseEvent e) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment