2.7 画笔
package com.mysoft.lesson03;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame() {
setBounds(200, 200, 600, 500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//super.paint(g);
//画笔,需要有颜色,画笔可以画画
g.setColor(Color.red);
//g.drawOval(100, 100, 100, 100);
g.fillOval(100,100,100,100);//实心的圆
g.setColor(Color.green);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将它还原到最初的颜色
}
}
实行效果:
2.8 鼠标监听
目的:实现鼠标画画
TestMouseListener.java
package com.mysoft.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame {
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList<Point> points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,400);
setVisible(true);
//存鼠标点击的点
points = new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
System.out.println("paint(Graphics g) called!");
Iterator<Point> iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x,point.y,10,10);
}
}
//适配器模式
private class MyMouseListener extends MouseAdapter {
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
System.out.println("MyMouseListener =>" + e.getX()+", "+e.getY());
MyFrame frame = (MyFrame)e.getSource();
//这个我们点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点:
frame.points.add(new Point(e.getX(), e.getY()));
//每次点击鼠标都重新画一遍!
frame.repaint();//刷新 30帧 60帧
}
}
}
实行效果图:
思维导图:
2.9 窗口监听
窗口监听一般用的比较少,主要是键盘监听!
TestWindow.java
package com.mysoft.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBounds(100, 100, 400, 400);
setBackground(Color.blue);
setVisible(true);
addWindowListener(new MyWindowListener());
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮,隐藏窗口
System.exit(0);//正常退出
}
}
}
实行效果:
匿名内部类,建议这么写
package com.mysoft.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBounds(100, 100, 400, 400);
setBackground(Color.blue);
setVisible(true);
//addWindowListener(new MyWindowListener());
//匿名内部类
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮,隐藏窗口
System.exit(0);//正常退出
}
}
}
2.10 键盘监听
TestKeyListener.java
package com.mysoft.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
setBounds(1, 2, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的是哪个键,当前的码
int keyCode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性 VK_XXX
System.out.println(keyCodes);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
//根据按下的键,产生不同的结果
}
});
}
}