📄 screencanvas.java
字号:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.sound.sampled.Line;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
/**
*
*/
/**
* @author squirrel
*
*/
public class ScreenCanvas extends Canvas
implements MouseListener,MouseMotionListener {
private double beginX;
private double beginY;
private double endX;
private double endY;
private double atX;
private double atY;
private List shapeList;
private int drawShapeType;
private int drawToolType;
private int resize;
private Color drawColor;
private Color paintColor;
private Graphics g;
private ShapeCreator shapeCreator;
private JPanel screenPanel;
public ScreenCanvas()
{
super();
drawShapeType = -1;
beginX = -1;
beginY = -1;
endX = -1;
endY = -1;
atX = -1;
atY = -1;
resize = 0;
drawToolType = 0;
shapeList = new ArrayList();
drawColor = Color.BLACK;
shapeCreator = new ShapeCreator();
this.setBackground(Color.WHITE);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public double getBeginX() {
return beginX;
}
public void setBeginX(double beginX) {
this.beginX = beginX;
}
public double getBeginY() {
return beginY;
}
public void setBeginY(double beginY) {
this.beginY = beginY;
}
public int getDrawShapeType() {
return drawShapeType;
}
public void setDrawShapeType(int drawShapeType) {
this.drawShapeType = drawShapeType;
}
public double getEndX() {
return endX;
}
public void setEndX(double endX) {
this.endX = endX;
}
public double getEndY() {
return endY;
}
public void setEndY(double endY) {
this.endY = endY;
}
public double getAtX() {
return atX;
}
public void setAtX(double atX) {
this.atX = atX;
}
public double getAtY() {
return atY;
}
public void setAtY(double atY) {
this.atY = atY;
}
public int getResize() {
return resize;
}
public void setResize(int resize) {
this.resize = resize;
}
public Color getPaintColor() {
return paintColor;
}
public void setPaintColor(Color paintColor) {
this.paintColor = paintColor;
}
public int getDrawToolType() {
return drawToolType;
}
public void setDrawToolType(int drawToolType) {
this.drawToolType = drawToolType;
}
public Graphics getG() {
return g;
}
public void setG(Graphics g) {
this.g = g;
}
public Color getDrawColor() {
return drawColor;
}
public void setDrawColor(Color drawColor) {
this.drawColor = drawColor;
}
public List getShapeList() {
return shapeList;
}
public void setShapeList(List shapeList) {
this.shapeList = shapeList;
}
public void setScreenPanel(JPanel screenPanel) {
this.screenPanel = screenPanel;
}
public void setDrawToolColor(Color color,int drawToolType)
{
if(drawToolType == 0)
this.drawColor = color;
else if(drawToolType == 1)
this.paintColor = color;
}
public void paintShape(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
if(drawShapeType == -1)
return;
Shape shape = this.getShape(drawShapeType,beginX,beginY,endX,endY);
ShapeEntity entity = new ShapeEntity();
entity.setShapeType(drawShapeType);
entity.setBeginX(beginX);
entity.setBeginY(beginY);
entity.setEndX(endX);
entity.setEndY(endY);
entity.setDrawColor(drawColor);
entity.setPaintColor(paintColor);
shapeList.add(entity);
g2D.setPaint(this.drawColor);
if(entity.getShapeType() == 4)
{
g2D.drawPolygon((Polygon) shape);
}
else
g2D.draw(shape);
}
public void fillComponent(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
if(atX == -1 && atY == -1)
return;
double distance = -1;
int nearestShape = -1;
if(shapeList == null || shapeList.size() == 0)
return ;
for(int i = 0; i < shapeList.size(); i++)
{
ShapeEntity entity = (ShapeEntity)shapeList.get(i);
if(atX >= entity.getBeginX() && atX <= entity.getEndX()
&& atY >= entity.getBeginY() && atY <= entity.getEndY())
{
//找出填充点与哪个图形的中心点最近
double midX = entity.getBeginX() +((entity.getEndX() - entity.getBeginX()) /2);
double midY = entity.getBeginY() +((entity.getEndY() - entity.getBeginY()) /2);
double tempDistance = (Math.pow((midX - atX), 2) + Math.pow((midY - atY), 2));
if(distance == -1)
{
distance = tempDistance;
nearestShape = i;
}
else if(distance > 0 && distance > tempDistance)
{
distance = tempDistance;
nearestShape = i;
}
}
}
if(distance == -1 || nearestShape == -1)
return;
ShapeEntity object = (ShapeEntity)shapeList.get(nearestShape);
object.setPaintColor(paintColor);
for(int i = 0; i < shapeList.size(); i++)
{
ShapeEntity entity = (ShapeEntity)shapeList.get(i);
double x1 = entity.getBeginX();
double y1 = entity.getBeginY();
double x2 = entity.getEndX();
double y2 = entity.getEndY();
double x3 = 0;
double y3 = 0;
Shape shape = this.getShape(entity.getShapeType(),x1,y1,x2,y2);
g2D.setPaint(entity.getDrawColor());
g2D.draw(shape);
if(entity.getPaintColor() != null)
{
g2D.setPaint(entity.getPaintColor());
g2D.fill(shape);
}
}
}
public void paintShapes()
{
if(g == null)
g = this.getGraphics();
Graphics2D g2D = (Graphics2D)g;
if(shapeList == null || shapeList.size() == 0)
return ;
for(int i = 0; i < shapeList.size(); i++)
{
ShapeEntity entity = (ShapeEntity)shapeList.get(i);
double x1 = entity.getBeginX();
double y1 = entity.getBeginY();
double x2 = entity.getEndX();
double y2 = entity.getEndY();
Shape shape = this.getShape(entity.getShapeType(),x1,y1,x2,y2);
g2D.setPaint(entity.getDrawColor());
g2D.draw(shape);
if(entity.getPaintColor() != null)
{
g2D.setPaint(entity.getPaintColor());
g2D.fill(shape);
}
}
}
public void addShape(ShapeEntity shapeEntity)
{
if(shapeList == null)
shapeList = new ArrayList();
shapeList.add(shapeEntity);
}
public void paint(Graphics g)
{
super.paint(g);
this.paintShapes();
}
private Shape getShape(int shapeType,double x1,double y1,double x2,double y2)
{
Shape shape = null;
switch(shapeType)
{
case 0: //曲线
case 1: //直线
shape = shapeCreator.createLine2D(x1, y1, x2, y2);
break;
case 2: //椭圆
shape = shapeCreator.createEllipse2D(x1, y1, x2, y2);
break;
case 3: //方形
shape = shapeCreator.createRectangle(x1, y1, x2, y2);
break;
default:
;
break;
}
return shape;
}
public void mouseDragged(MouseEvent e) {
if(this.drawToolType == 0)
{
if(g == null)
g = this.getGraphics();
Point p = e.getPoint();
endX = p.getX();
endY = p.getY();
if(drawShapeType == 0)
{
this.paintShape(g);
beginX = endX;
beginY = endY;
}
}
}
public void mouseMoved(MouseEvent e) {
if(this.drawToolType == 0 || this.drawShapeType == 4)
{
Point p = e.getPoint();
beginX = p.getX();
beginY = p.getY();
}
}
public void mouseReleased(MouseEvent arg0) {
if(this.drawToolType == 0)
{
if(g == null)
g = this.getGraphics();
if(drawShapeType == 1 || drawShapeType == 2 || drawShapeType == 3)
this.paintShape(g);
}
}
public void mouseClicked(MouseEvent e) {
if(g == null)
g = this.getGraphics();
if(this.drawToolType == 1)
{
Point p = e.getPoint();
atX = p.getX();
atY = p.getY();
this.fillComponent(g);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -