📄 myapplet.java
字号:
itemList[i].pressCount = 0;
itemList[i].R = 0;
itemList[i].G = 0;
itemList[i].B = 0;
itemList[i].isSelected = false;
repaint();
break;
}
}// */
}
else if(currentChoice == STATUS_ARROW)
{
CPoint point = new CPoint(e.getPoint());
int i;
for (i = 0; i < index; i++)
{
if (point.pointInArea(itemList[i]))
{
if (itemList[i].type == 5)
{
itemList[index].x2 = itemList[i].x1;
itemList[index].y2 = (itemList[i].y1 + itemList[i].y2)/2;
}
repaint();
index++;
createNewItem();
break;
}
}// */
if (i == index)
{
itemList[index].x2 = e.getX();
itemList[index].y2 = e.getY();
repaint();
index++;
createNewItem();
}
}
else
{
itemList[index].x2 = e.getX();
itemList[index].y2 = e.getY();
repaint();
index++;
createNewItem();
}
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText(" Mouse Entered @:[" + e.getX() + ", "
+ e.getY() + "]");
}
public void mouseExited(MouseEvent e)
{
statusBar.setText(" Mouse Entered @:[" + e.getX() + ", "
+ e.getY() + "]");
}
}
// 鼠标事件mouseB类继承了MouseMotionAdapter,用来完成鼠标拖动和鼠标移动时的相应操作
class mouseB extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
statusBar.setText(" Mouse Dragged @:[" + e.getX() + ", "
+ e.getY() + "]");
if (currentChoice == STATUS_MOUSE)
{
CPoint point = new CPoint(e.getPoint());
for (int i = 0; i < index; i++)
{
if (point.pointInArea(itemList[i]) && itemList[i].pressCount > 1)
{
int moveX = e.getX() - x_old;
int moveY = e.getY() - y_old;
itemList[i].x1 += moveX;
itemList[i].y1 += moveY;
itemList[i].x2 += moveX;
itemList[i].y2 += moveY;
itemList[i].isDraging = true;
itemList[i].R = 255;
itemList[i].G = 0;
itemList[i].B = 0;
break;
}
}// */
}
else
{
itemList[index].x2 = e.getX();
itemList[index].y2 = e.getY();
}
x_old = e.getX();
y_old = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
statusBar.setText(" Mouse Dragged @:[" + e.getX() + ", "
+ e.getY() + "]");
}
}
// 新建一个画图基本单元对象的程序段
void createNewItem()
{
if (currentChoice == STATUS_COLOR)// 进行相应的游标设置
myDraw.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
else
myDraw.setCursor(Cursor
.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
switch (currentChoice)
{
case STATUS_MOUSE:
return;
case STATUS_LINE:
itemList[index] = new Line();
break;
case STATUS_RECT:
itemList[index] = new Rect();
break;
case STATUS_FRECT:
itemList[index]=new fillRect();
break;
case STATUS_OVAL:
// itemList[index]=new Oval();
break;
case STATUS_FOVAL:
// itemList[index]=new fillOval();
break;
case STATUS_CIRCLE:
// itemList[index]=new Circle();
break;
case STATUS_FCIRCLE:
// itemList[index]=new fillCircle();
break;
case STATUS_ROUNDRECT:
// itemList[index]=new RoundRect();
break;
case STATUS_FROUNDRECT:
// itemList[index]=new fillRoundRect();
break;
case STATUS_ARROW:
itemList[index] = new Arrow();
break;
case STATUS_COLOR:
// itemList[index]=new Word();
break;
}
itemList[index].type = currentChoice;
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
itemList[index].stroke = stroke;
itemList[index].isSelected = false;
itemList[index].isDraging = false;
itemList[index].pressCount = 0;
}
}
class FlowItems implements Serializable
{
int x1, y1, x2, y2; // 定义坐标属性
int R, G, B; // 定义色彩属性
float stroke; // 定义线条粗细属性
int type; // 定义字体属性
String s1;
String s2; // 定义字体风格属性
boolean isSelected; // 定义图元状态 0 未选中 1 选中
boolean isDraging; // 是否处于拖放状态
int pressCount; // 鼠标按下的次数
void draw(Graphics2D g2d)
{
};// 定义绘图函数
}
/*******************************************************************************
* 下面是各种基本图形单元的子类,都继承自父类FlowItems
******************************************************************************/
class Line extends FlowItems // 直线类
{
void draw(Graphics2D g2d)
{
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1, y1, x2, y2);
}
}
class Rect extends FlowItems // 矩形类
{
void draw(Graphics2D g2d)
{
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2d.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
}
class fillRect extends FlowItems // 实矩形
{
void draw(Graphics2D g2d)
{
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2d.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
}
class Arrow extends FlowItems // 箭头类
{
void draw(Graphics2D g2d)
{
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
// 利用向量来进行计算
int len = 10; // 箭头的边长
int x0 = 0, y0 = 0;
double dx = x2 - x0, dy = y2 - y0;
//共线 模长为len
//0 = (x2 -x1)*dy - dx*(y2 -y1); dx*dx + dy*dy = len*len
double mAB = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1);
dx = (x2 -x1)*len / java.lang.Math.sqrt(mAB) ;
dy = (y2 -y1)*len / java.lang.Math.sqrt(mAB) ;
x0 = (int) (x2 - dx);
y0 = (int) (y2 - dy);
int x3 = 0, y3 = 0, x4, y4;
dx = x3 - x0;
dy = y3 - y0;
double mCB = (x2 - x0)*(x2 - x0) + (y2 - y0)*(y2 - y0);
dx = -(y2 -y0)*len / java.lang.Math.sqrt(mCB) ;
dy = (x2 -x0)*len / java.lang.Math.sqrt(mCB) ;
x3 = (int) (dx + x0);
y3 = (int) (dy + y0);
x4 = 2*x0 - x3;
y4 = 2*y0 - y3;
g2d.drawLine(x1, y1, x0, y0);
g2d.setPaint(new Color(255, 0, 0));
Polygon p = new Polygon();
p.addPoint(x2, y2);
p.addPoint(x3, y3);
p.addPoint(x4, y4);
g2d.drawPolygon(p);
g2d.fillPolygon(p);
}
}
class CPoint extends Point
{
public CPoint(Point point)
{
this.x = point.x;
this.y = point.y;
}
public boolean pointInArea(FlowItems item)
{
if (item != null)
{
if ((x > item.x1 && x < item.x2) || (x > item.x2 && x < item.x1))
{
if ((y > item.y1 && y < item.y2) || (y > item.y2 && y < item.y1))
{
return true;
}
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -