📄 paintpanel.java
字号:
else if (preX - e.getX() <= 0)
gh.rotateNum -= 1 * Math.PI / 180;
redraw();
}
}
} else if (nowType != BaseObject.FILL && nowType != BaseObject.ROTATE) {
BaseObject gh = (BaseObject) (graphics.getLast());
gh.changeSize(e.getX(), e.getY());
instance.setState("正在创建物体 " + String.valueOf(graphics.size() - 1)
+ " : " + gh.toString());
}
redraw();
preX = e.getX();
}
public void c_mouseMoved(MouseEvent e) {
if (nowType == -1 || nowType == BaseObject.FILL
|| nowType == BaseObject.ROTATE) {
BaseObject gh = null;
int x = e.getX();
int y = e.getY();
nowGraphics = -1;
nowChangeGraphics = -1;
nowRotate = -1;
// nowRotate=-1;
for (int i = graphics.size() - 1; i >= 0; i--) {
gh = (BaseObject) (graphics.get(i));
if (gh.canRotate(x, y) && nowType == BaseObject.ROTATE) {
// setCursor("rotate.gif",new Point(32,32),"rotate");
setCursor("rotate.gif", new Point(8, 23), "rotate");
// c.setCursor(new Cursor(Cursor.HAND_CURSOR));
nowRotate = i;
break;
}
if (gh.canResizeObject(x, y) && nowType == -1) {
int x1 = gh.getPointX(0);
int y1 = gh.getPointY(0);
if ((x >= x1 && y >= y1) || (x <= x1 && y <= y1))
c.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
else if ((x > x1 && y < y1) || (x < x1 && y > y1))
c.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
nowChangeGraphics = i;
break;
}
if (gh.isCursorIn(x, y)) {
if (nowType == BaseObject.FILL) {
setCursor("fill.gif", new Point(5, 5), "fill");
nowGraphics = i;
break;
} else if (nowType == BaseObject.ROTATE) {
nowRotate = i;
break;
}
c.setCursor(new Cursor(Cursor.MOVE_CURSOR));
nowGraphics = i;
// redraw();
break;
}
}
if (nowGraphics == -1 && nowChangeGraphics == -1 && nowRotate == -1) { // &&
// nowRotate
// ==
// -
// 1
c.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} else {
/*
* if (nowRotate>=0) {
* instance.setState("可旋转物体 "+String.valueOf(
* nowRotate)+" : "+graphics.get(nowRotate)); } else
*/
if (nowGraphics >= 0) {
instance.setState("当前物体 " + String.valueOf(nowGraphics)
+ " : " + gh.toString());
} else if (nowChangeGraphics >= 0) {
instance.setState("可以改变大小 物体 "
+ String.valueOf(nowChangeGraphics) + " : "
+ gh.toString());
}
return;
}
// redraw();
}
if (nowObject < 0) {
instance.setState("当前鼠标 (" + String.valueOf(e.getX()) + ","
+ String.valueOf(e.getY()) + ")");
} else if (nowGraphics != nowObject) {
instance.setState("当前选中物体 " + String.valueOf(nowObject) + " : "
+ graphics.get(nowObject));
}
}
// *鼠标处理事件结束
/*
* Panel 鼠标处理事件
*/
private int resizeType(int x, int y) {
if ((x > width + 5) && (y > height / 2) && (x < width + 15)
&& (y < height / 2 + 5))
return 1; // 水平拉
else if ((x > width / 2) && (y > height + 5) && (x < width / 2 + 5)
&& (y < height + 15))
return 2;
else if ((x > width + 5) && (y > height + 5) && (x < width + 15)
&& (y < height + 15))
return 3; // 对角线拉
else
return 0;
}
/**
* 当鼠标在主 <code>JPanel</code> 上按下的时候触发,判断是否可改变画布大小
*/
public void p_mousePressed(MouseEvent e) {
changeSize = resizeType(e.getX(), e.getY());
if (changeSize > 0) {
xOffset = e.getX() - width - 5;
yOffset = e.getY() - height - 5;
}
}
/**
* 当鼠标在主 <code>JPanel</code> 上拖动的时候触发,符合条件则改变画布大小
*/
public void p_mouseDragged(MouseEvent e) {
if (changeSize > 0) {
if (changeSize == 1 || changeSize == 3)
width = e.getX() - 5 - xOffset;
if (changeSize == 2 || changeSize == 3)
height = e.getY() - 5 - yOffset;
c.setSize(width, height);
instance.setState("画布大小: " + String.valueOf(width) + " * "
+ String.valueOf(height));
// repaint();
paint(getGraphics());
}
}
/**
* 当鼠标在主 <code>JPanel</code> 上移动的时候触发,符合改变大小条件则改变鼠标指针样式
*/
public void p_mouseMoved(MouseEvent e) {
int bak = changeSize;
changeSize = resizeType(e.getX(), e.getY());
if (bak != changeSize) {
if (changeSize == 1) {
this.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
} else if (changeSize == 2) {
this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
} else if (changeSize == 3) {
this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
} else {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
}
/**
* 当鼠标在主 <code>JPanel</code> 上弹起的时候触发,若正在改变大小则恢复
*/
public void p_mouseReleased(MouseEvent e) {
if (changeSize > 0) {
// setSize(width+10,height+10);
c.setSize(width, height);
changeSize = 0;
paint(getGraphics());
c_mouseExited(e);
p_mouseMoved(e);
}
}
// * Panel 鼠标事件结束
/**
* 画布有焦点时,键盘按下触发事件<br>
* 按下 <code>Delete</code> 键则删除选择物体
*/
public void c_keyTyped(KeyEvent e) {
// System.out.println (e);
if (e.getKeyChar() == e.VK_DELETE) { // 删除物体
instance.toolPanel.deleteObject();
}
}
// *键盘处理事件结束
/**
* 设置撤xiao钮的可用状tai
*/
public void setUndoable(boolean b) {
instance.mnuE_Undo.setEnabled(b);
instance.toolbarPanel.jbtn[3].setEnabled(b);
}
/**
* 设置删除按钮的可用状tai
*/
public void setDelable(boolean b) {
instance.mnuO_DelObject.setEnabled(b);
instance.toolbarPanel.jbtn[5].setEnabled(b);
instance.toolbarPanel.jbtn[4].setEnabled(b);
}
private BaseObject createObject(int x, int y) {
BaseObject gh;
if (nowType == BaseObject.LINE) {
gh = new ObjLine();
} else if (nowType == BaseObject.RECTANGLE) {
gh = new ObjRectangle();
} else if (nowType == BaseObject.ROUNDRECT) {
gh = new ObjRoundRect();
} else if (nowType == BaseObject.OVAL) {
gh = new ObjOval();
} else if (nowType == BaseObject.PENCIL) {
gh = new ObjPolyline();
} else { // if (nowType==G2d.POLYGON) {
gh = new ObjPolygon();
}
graphics.add(gh);
gh.type = nowType;
gh.color = nowColor;
gh.color2 = bacColor;
gh.gradientPaint = gradientPaint;
gh.filled = isFill;
// 画正二维图形
gh.isSquare = isSquare;
gh.addPoint(x, y);
gh.addPoint(x, y);
gh.stroke = stroke;
return gh;
}
/**
* 设置画布鼠标指针样式
*
* @param name
* 指针文件名 pt 指针图像的热点 curName 自定义指针的名称
*/
public void setCursor(String name, Point pt, String curName) {
try {
Image imgCur = Toolkit.getDefaultToolkit()
.getImage(
Project.class.getResource("/project/paint/cursors/"
+ name));
c.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(imgCur,
pt, curName));
} catch (Exception e) {
System.out.println("==============================");
System.out.println("setCursor(String,Point,String)方法出现异常:");
System.out.println("name=" + name + ", pt=" + pt.toString()
+ ", curName=" + curName);
System.out.println("错误信息: " + e.toString());
System.out.println("==============================");
c.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
/**
* 设置当前鼠标动作
*
* @param type
* 动作,参考 {@link BaseObject#PENCIL},{@link BaseObject#LINE},
* {@link BaseObject#RECTANGLE},{@link BaseObject#ROUNDRECT},
* {@link BaseObject#OVAL},{@link BaseObject#POLYGON}, (@link
* StatePanel#setNowType(int,boolean)}
*/
public void setType(int type) {
nowType = type;
if (type == -1) {
c.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
instance.setState("移动物件");
} else {
setCursor("cross.gif", new Point(15, 16), "cross");
if (type == BaseObject.PENCIL) {
setCursor("pencil.gif", new Point(8, 23), "pencil");
instance.setState("铅笔");
} else if (type == GETCOLOR) {
setCursor("getcolor.gif", new Point(8, 22), "getcolor");
instance.setState("选取颜色");
} else if (type == BaseObject.LINE)
instance.setState("直线");
else if (type == BaseObject.RECTANGLE) {
if (isFill) {
instance.setState("填充矩形");
} else {
instance.setState("矩形");
}
} else if (type == BaseObject.ROUNDRECT) {
if (isFill) {
instance.setState("填充圆角矩形");
} else {
instance.setState("圆角矩形");
}
} else if (type == BaseObject.OVAL) {
if (isFill) {
instance.setState("填充椭圆");
} else {
instance.setState("椭圆");
}
} else if (type == BaseObject.FILL) {
instance.setState("填充");
} else if (type == BaseObject.ROTATE) {
instance.setState("旋转");
}
}
instance.statePanel.setNowType(type, isFill);
}
/**
* 用来初始化变量
*/
public void initParam() {
nowColor = Color.BLACK;
forColor = Color.BLACK;
bacColor = Color.WHITE;
midColor = Color.WHITE;
// nowType=-1; //MOVE
nowGraphics = -1;
nowObject = -1;
nowChangeGraphics = -1;
nowRotate = -1;
isFilling = false;
isDrawing = false;
isChanging = false;
isSquare = false;
isRotating = false;
gradientPaint = 0;
xOffset = 0;
yOffset = 0;
stroke = 1.0f;
changeSize = 0;
graphics = new LinkedList<BaseObject>();
undoGraphics = new LinkedList<BaseObject>();
if (c != null)
c.getGraphics().clearRect(0, 0, width, height);
backColor = Color.WHITE;
instance.setState("准备就绪");
instance.f.setTitle("未命名 - " + Project.TITLE);
// redraw();
repaint();
}
/**
* 重画,调用 c.paint(Graphics)
*/
public void redraw() {
if (c == null)
return;
c.paint(c.getGraphics());
// c.repaint();
}
/**
* 保存文件
*
* @param showDialog
* 是否显示选择文件对话框
*/
public void saveFile2(JPanel jcomponent) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -