📄 mycanvas.java
字号:
* 在java中repaint()是通过两个步骤来实现刷新功能的,首先它调用public void update()来刷新屏幕,
* 其次再调用paint(Graphcis g)来重画屏幕,这就容易造成闪烁,特别是一些需要重画背景的程序,
* 如果下一桢图象可以完全覆盖上一桢图象的话,便可以重写update函数如下来消除闪烁:
* public void update(Graphics g){ paint(g) }同样调用repaint()重画屏幕。
* 或者直接重写不调用repaint,而用Graphics g=getGraphics(); paint(g);来实现重画屏幕。
*/
/**
* 重写update方法以消除闪烁。
*/
public void update(Graphics g){
paint(g);
}
/**
* 画画板
*/
public void paint(Graphics g) {
/**** 画空画板 **/
// 获得整个画板的区域
size = getSize();
m_wScreen = size.width;
m_hScreen = size.height;
if( !(command==Command.PENCIL) &&!(command==Command.ERASER)){
// 设置画板的颜色为白色
g.setColor(bgColor);
// 用白色填充画板区域
g.fillRect(0, 0, m_wScreen, m_hScreen);
/*** 画画板上的图形 ***/
int i = 0;
Shape shape = null;
for(i = 0; i < Shapes.size(); i++)
{
shape = (Shape) Shapes.get(i);
shape.draw(g, 0,bgColor);
}
for(i = 0; i < n_GraphSelect; i++)
{
DrawGraph(g,GraphSelect[i]);
}
}
// 如果当前鼠标还没有松开
if (bMousePressing) {
// 设置画笔颜色
g.setColor(fgColor);
switch (command) {
case Command.LINE:
// 画线,在鼠标起始位置和当前位置之间画一条直线
g.drawLine(beginX, beginY, currentX, currentY);
break;
case Command.PENCIL:
// 画笔
pencil.draw(g, 0,bgColor);
break;
case Command.RECTANGLE:
// 画矩形
if(fill == false)
{
if (currentX < beginX) {
// 如果鼠标当前位置在起始位置的左上方,则以鼠标当前位置为矩形的左上角位置。
if (currentY < beginY) {
g.drawRect(currentX, currentY, beginX - currentX, beginY
- currentY);
} else {
// 如果鼠标当前位置在起始位置的左下方,
// 则以当前位置的横坐标和起始位置的纵坐标为矩形的左上角位置
g.drawRect(currentX, beginY, beginX - currentX, currentY
- beginY);
}
} else {
// 如果鼠标当前位置在起始位置的右上方,
// 则以鼠标起始位置的横坐标和当前的纵坐标为矩形的左上角位置
if (currentY < beginY) {
g.drawRect(beginX, currentY, currentX - beginX, beginY
- currentY);
} else {
// 如果鼠标当前位置在起始位置的右下方,则以起始位置为矩形的左上角位置
g.drawRect(beginX, beginY, currentX - beginX, currentY - beginY);
}
}
}
else{
if (currentX < beginX) {
// 如果鼠标当前位置在起始位置的左上方,则以鼠标当前位置为矩形的左上角位置。
if (currentY < beginY) {
g.fillRect(currentX, currentY, beginX - currentX, beginY
- currentY);
} else {
// 如果鼠标当前位置在起始位置的左下方,
// 则以当前位置的横坐标和起始位置的纵坐标为矩形的左上角位置
g.fillRect(currentX, beginY, beginX - currentX, currentY
- beginY);
}
} else {
// 如果鼠标当前位置在起始位置的右上方,
// 则以鼠标起始位置的横坐标和当前的纵坐标为矩形的左上角位置
if (currentY < beginY) {
g.fillRect(beginX, currentY, currentX - beginX, beginY
- currentY);
} else {
// 如果鼠标当前位置在起始位置的右下方,则以起始位置为矩形的左上角位置
g.fillRect(beginX, beginY, currentX - beginX, currentY - beginY);
}
}
}
break;
case Command.CIRCLE:
// 画圆,获取半径。半径等于a*a + b*b的平方根
int radius = (int) java.lang.Math.sqrt((beginX - currentX)
* (beginX - currentX) + (beginY - currentY)
* (beginY - currentY));
if(fill == false)
{
// 画圆弧
g.drawArc(beginX - radius / 2, beginY - radius / 2, radius, radius, 0, 360);
}
else{
g.fillArc(beginX - radius / 2, beginY - radius / 2, radius, radius, 0, 360);
}
break;
case Command.ERASER:
int erasewidth = ERASER_STROKES[eraserIndex];
g.setColor(bgColor);
g.fillRect(currentX,currentY,erasewidth,erasewidth);
Eraser erase = new Eraser(fgColor,brushColor,eraserIndex,currentX,currentY);
Shapes.add(erase);
}//End switch(command)
}
}
// MouseListener接口定义的方法,处理鼠标单击事件。
public void mouseClicked(MouseEvent e) {
switch(command){
case Command.SELECT:
currentX = e.getX();
currentY = e.getY();
float j1 = 8;
int pb = -1;
pb = PointSelect(currentX,currentY,j1);
if(pb != -1)
{
AddSelectList(pb);
}
break;
case Command.TEXTINSERT:
Point aPoint = new Point(e.getX(), e.getY());
Font f = new Font("", 0, 10);
TextInput ti = new TextInput(pd, true, null);
if (ti.showDialog(pd, null, f) == 0) {
CusString aCusShape = new CusString(ti.str, (int) aPoint.getX(),
(int) aPoint.getY());
aCusShape.setFont(ti.aFont);
aCusShape.setColor(this.fgColor);
Shapes.add(aCusShape);
Graphics g=getGraphics();
paint(g);
}
break;
}
}
// MouseListener接口定义的方法,处理鼠标按下事件。
public void mousePressed(MouseEvent e) {
// 设置鼠标起始位置
beginX = e.getX();
beginY = e.getY();
statusTextArea.setText("The position of cursor: x=" + currentX + ", y=" + currentY + "\n The current draw tool: " + getCommandString(command));
if(command == Command.PENCIL){
pencil = new Pencil(this.fgColor, this.bgColor,this.strokeIndex, e.getX(), e.getY());
}
// 设置bMousePressing为真,表示鼠标按下了。
bMousePressing = true;
}
//MouseListener接口定义的方法,处理鼠标松开事件。
public void mouseReleased(MouseEvent e) {
// 获取鼠标当前位置
currentX = e.getX();
currentY = e.getY();
statusTextArea.setText("The position of cursor: x=" + currentX + ", y=" + currentY + "\n The current draw tool: " + getCommandString(command));
// 设置鼠标已经松开了。
bMousePressing = false;
// 松开鼠标时,将刚刚画的图形保存到vShapes中
switch (command) {
case Command.LINE:
// 新建图形
Line line = new Line(fgColor,brushColor,strokeIndex,beginX,beginY,currentX,currentY);
Shapes.add(line);
break;
case Command.PENCIL:
pencil.setPoints(e.getX(), e.getY());
Shapes.add(pencil);
pencil = null;
break;
case Command.RECTANGLE:
Rectangle rectangle = null;
if (currentX < beginX) {
if (currentY < beginY) {
rectangle = new Rectangle(fgColor,brushColor,strokeIndex,beginX,beginY,beginX - currentX,beginY - currentY, fill);
} else {
rectangle = new Rectangle(fgColor,brushColor,strokeIndex,currentX,beginY,beginX - currentX,currentY - beginY, fill);
}
} else {
if (currentY < beginY) {
rectangle = new Rectangle(fgColor,brushColor,strokeIndex,beginX,currentY,currentX - beginX,beginY - currentY, fill);
} else {
rectangle = new Rectangle(fgColor,brushColor,strokeIndex,beginX,beginY,currentX - beginX,currentY - beginY, fill);
}
}
Shapes.add(rectangle);
break;
case Command.CIRCLE:
int radius = (int) java.lang.Math.sqrt((beginX - currentX)
* (beginX - currentX) + (beginY - currentY) * (beginY - currentY));
Circle circle = new Circle(fgColor,brushColor,strokeIndex,beginX - radius / 2,beginY - radius / 2, radius, fill);
Shapes.add(circle);
break;
} //End switch(command)
//重画画板
Graphics g=getGraphics();
paint(g);
}
//MouseListener接口定义的方法,处理鼠标移入事件。
public void mouseEntered(MouseEvent e) {
// do nothing
}
//MouseListener接口定义的方法,处理鼠标移出事件。
public void mouseExited(MouseEvent e) {
// do nothing
}
//MouseListener接口定义的方法,处理鼠标按住不放拖动事件。
public void mouseDragged(MouseEvent e) {
// 按住鼠标拖动时,不断的获取鼠标当前位置,并重画画板
PastX = currentX;
PastY = currentY;
currentX = e.getX();
currentY = e.getY();
statusTextArea.setText("The position of cursor: x=" + currentX + ", y=" + currentY + "\n The current draw tool: " + getCommandString(command));
if(command == Command.PENCIL){
pencil.setPoints(e.getX(), e.getY());
}
Graphics g=getGraphics();
paint(g);
}
public void SaveJpg() {
size = getSize();
m_wScreen = size.width;
m_hScreen = size.height;
offScreenImg = new BufferedImage(m_wScreen, m_hScreen, BufferedImage.TYPE_INT_RGB);
offScreenG = offScreenImg.createGraphics();
offScreenG.setColor(this.bgColor);
offScreenG.fillRect(0, 0, this.getWidth(), this.getHeight());
int i = 0;
Shape shape = null;
for (;i < Shapes.size();i++)
{
shape = (Shape)Shapes.get(i);
shape.draw(offScreenG,0, bgColor);
}
}
//MouseListener接口定义的方法,处理鼠标移动事件。
public void mouseMoved(MouseEvent e) {
// do nothing
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -