?? painterpanel.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //圖案類型
Point[] point=new Point[2]; //記錄鼠標拖動的起始點和終點
public PainterPanel(){
super(); //調用父類構造函數
this.setBackground(Color.white); //設置背景顏色
point[0]=new Point(-1,-1); //初始化變量
point[1]=new Point(-1,-1);
addMouseListener(this); //增加鼠標事件
}
public void mouseReleased(MouseEvent e){ //鼠標釋放事件
point[1]=new Point(e.getX(),e.getY()); //設置終點位置
repaint(); //重繪屏幕
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){ //鼠標按下時事件
point[0]=new Point(e.getX(),e.getY()); //設置起始點位置
}
public void paint(Graphics g){
super.paint(g);
switch (shape){ //根據shape值繪制圖形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //繪線
break;
case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //繪橢圓
break;
case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //繪矩形
break;
}
}
public void drawShape(int shape){
this.shape=shape;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -