Java 使用Graphics2D,当我拖动形状时,我想移动形状
当我拖动我绘制的形状时,我想移动Java 使用Graphics2D,当我拖动形状时,我想移动形状,java,graphics,mouseevent,graphics2d,affinetransform,Java,Graphics,Mouseevent,Graphics2d,Affinetransform,当我拖动我绘制的形状时,我想移动 但我不知道怎么做 我在GPanel中尝试了make-move方法,但没有成功 知识 我已经为此工作了将近一周,并尝试了我能想到的所有解决方案 这是我第一次发布关于堆栈溢出的代码问题 我真的很想学习。 . 我爱你们所有人 我希望有一天我能成为代码新手的救世主 必须使用仿射变换 这是绘图页 这是扩展GShapeTool的shape类 您应该获得一个dragstart事件。此时,保存鼠标位置和选定对象。 当您获得dragfinished事件时,再次注意鼠标位置并相
但我不知道怎么做
我在GPanel中尝试了make-move方法,但没有成功
知识 我已经为此工作了将近一周,并尝试了我能想到的所有解决方案 这是我第一次发布关于堆栈溢出的代码问题 我真的很想学习。 . 我爱你们所有人 我希望有一天我能成为代码新手的救世主 必须使用仿射变换
这是绘图页
这是扩展GShapeTool的shape类
您应该获得一个dragstart事件。此时,保存鼠标位置和选定对象。 当您获得dragfinished事件时,再次注意鼠标位置并相应地移动对象。移动可能只是在屏幕上发生,但例如,如果垃圾桶上发生掉落,您希望将其删除
现在,当拖动处于活动状态时,您可能会获得更多的拖动事件,您应该使用这些事件来更新屏幕并向用户提供反馈。大多数情况下,这只是查看鼠标位置和选定对象,并相应地绘制它们。请参见:或了解一些想法。查看了这些想法,但我认为我无法将其用于图形。不适合我的。。好的,谢谢你的评论:)关键是要理解这些概念并修改你的代码以使用这些概念。亲爱的camickr,我取得了进展!哈哈,但是另一个问题突然出现了…:(你能检查一下这个吗?;是的,我做了一些移动,但是有一个问题,移动的形状的来源被删除了,而没有添加到形状中。嗯,,,这很难解释。等一下,我将发布新问题并评论该问题的url;我刚才给你的url就是我正在努力解决的url
package frame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Vector;
import javax.swing.JPanel;
import main.GConstants.EAnchorLocation;
import main.GConstants.EDrawingStyle;
import shapeTool.GShapeTool;
public class GPanel extends JPanel {
private static final long serialVersionUID = 1L;
private GShapeTool shapeTool;
private GShapeTool selectedShape;
private Vector<GShapeTool> shapes;
public GPanel() {
this.setBackground(Color.WHITE);
this.shapes = new Vector<GShapeTool>();
MouseHandler mouseHandler = new MouseHandler();
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
this.addMouseWheelListener(mouseHandler);
}
public void initialize() {}
public Vector<GShapeTool> getShapes() {
return this.shapes;
}
public void setShapes(Vector<GShapeTool> shapes){
this.shapes = shapes;
this.updateUI();
}
public void paint(Graphics graphics) {
super.paint(graphics);
for(GShapeTool shape: this.shapes) {
shape.draw((Graphics2D)graphics);
}
}
public void setShapeTool(GShapeTool shapeTool) {
this.shapeTool = shapeTool;
}
private boolean onShape(int x, int y) {
boolean shapeCheck = false;
for(GShapeTool shapeTool: this.shapes) {
if(shapeTool.contains(x, y)) {
this.selectedShape = shapeTool;
shapeCheck = true;
} else {
shapeTool.setSelected(false, (Graphics2D)getGraphics());
}
}
if(shapeCheck) {
this.selectedShape.setSelected(true, (Graphics2D)getGraphics());
}
return shapeCheck;
}
private void setInitialPoint(int x, int y) {
this.selectedShape = this.shapeTool.clone();
this.selectedShape.setInitialPoint(x, y);
}
private void setFinalPoint(int x, int y) {
for(GShapeTool shapeTool: this.shapes) {
shapeTool.setSelected(false, (Graphics2D)getGraphics());
}
// Graphics2D graphics2D = (Graphics2D) this.getGraphics();
//set xor mode;
// graphics2D.setXORMode(getBackground());
this.selectedShape.setFinalPoint(x, y);
this.selectedShape.setSelected(true, (Graphics2D)getGraphics());
this.shapes.add(this.selectedShape);
}
private void setIntermediatePoint(int x, int y) {
this.selectedShape.setIntermediatePoint(x, y);
}
private void animate(int x, int y) {
Graphics2D graphics2D = (Graphics2D) this.getGraphics();
//set xor mode;
graphics2D.setXORMode(getBackground());
this.selectedShape.animate(graphics2D, x, y);
}
private class MouseHandler
implements MouseListener, MouseMotionListener, MouseWheelListener {
private boolean isDrawing;
MouseHandler() {
this.isDrawing = false;
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
if(e.getClickCount() == 1) {
this.mouseLButton1Clicked(e);
} else if(e.getClickCount() == 2) {
this.mouseLButton2Clicked(e);
}
} else if(e.getButton() == MouseEvent.BUTTON2) {
if(e.getClickCount() == 1) {
this.mouseRButton1Clicked(e);
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
if(isDrawing) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
animate(e.getX(), e.getY());
}
}
}
private void mouseLButton1Clicked(MouseEvent e) {
if(!this.isDrawing) {
if(!onShape(e.getX(), e.getY())) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
setInitialPoint(e.getX(), e.getY());
this.isDrawing = true;
}
}
else if(onShape(e.getX(), e.getY())) {
onShape(e.getX(), e.getY());
}
} else {
if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
setIntermediatePoint(e.getX(),e.getY());
}
}
}
private void mouseLButton2Clicked(MouseEvent e) {
if(this.isDrawing) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
setFinalPoint(e.getX(), e.getY());
this.isDrawing = false;
}
}
}
private void mouseRButton1Clicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
if(!this.isDrawing) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
setInitialPoint(e.getX(), e.getY());
this.isDrawing = true;
}
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
if(this.isDrawing) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
animate(e.getX(), e.getY());
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(this.isDrawing) {
if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
setFinalPoint(e.getX(), e.getY());
this.isDrawing = false;
}
}
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {}
}
}
package shapeTool;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.Vector;
import main.GConstants.EAnchorLocation;
import main.GConstants.EDrawingStyle;
public abstract class GShapeTool implements Serializable {
private static final long serialVersionUID = 1L;
public enum EAnchors {
x0y0,
x0y1,
x0y2,
x1y0,
x1y2,
x2y0,
x2y1,
x2y2,
RR;
}
public final static int wAnchor = 10;
public final static int hAnchor = 10;
private EDrawingStyle eDrawingStyle;
protected boolean isSelected;
protected Shape shape;
private Ellipse2D[] anchors;
public GShapeTool(EDrawingStyle eDrawingStyle) {
this.eDrawingStyle = eDrawingStyle;
this.isSelected = false;
this.anchors = new Ellipse2D.Double[EAnchors.values().length];
for(EAnchors eAnchor: EAnchors.values()) {
this.anchors[eAnchor.ordinal()] = new Ellipse2D.Double();
}
}
public EDrawingStyle getDrawingStyle() {
return this.eDrawingStyle;
}
public boolean contains(int x, int y) {
return this.shape.contains(x , y);
}
private void drawAnchors(Graphics2D graphics2D) {
// draw bounding rectangle
Rectangle rectangle = this.shape.getBounds();
int x0 = rectangle.x-wAnchor;
int x1 = rectangle.x + rectangle.width/2;
int x2 = rectangle.x + rectangle.width;
int y0 = rectangle.y-hAnchor;
int y1 = rectangle.y + rectangle.height/2;
int y2 = rectangle.y + rectangle.height;
this.anchors[EAnchors.x0y0.ordinal()].setFrame(x0,y0,wAnchor,hAnchor);
this.anchors[EAnchors.x0y1.ordinal()].setFrame(x0,y1,wAnchor,hAnchor);
this.anchors[EAnchors.x0y2.ordinal()].setFrame(x0,y2,wAnchor,hAnchor);
this.anchors[EAnchors.x1y0.ordinal()].setFrame(x1,y0,wAnchor,hAnchor);
this.anchors[EAnchors.x1y2.ordinal()].setFrame(x1,y2,wAnchor,hAnchor);
this.anchors[EAnchors.x2y0.ordinal()].setFrame(x2,y0,wAnchor,hAnchor);
this.anchors[EAnchors.x2y1.ordinal()].setFrame(x2,y1,wAnchor,hAnchor);
this.anchors[EAnchors.x2y2.ordinal()].setFrame(x2,y2,wAnchor,hAnchor);
this.anchors[EAnchors.RR.ordinal()].setFrame(x1,y0-50,wAnchor,hAnchor);
//draw anchors
graphics2D.setColor(Color.BLACK);
for(EAnchors eAnchor: EAnchors.values()) {
graphics2D.draw(this.anchors[eAnchor.ordinal()]);
}
}
private void eraseAnchors(Graphics2D graphics2D) {
graphics2D.setColor(Color.WHITE);
for(EAnchors eAnchor: EAnchors.values()) {
graphics2D.draw(this.anchors[eAnchor.ordinal()]);
}
}
public void setSelected(boolean isSelected, Graphics2D graphics2D) {
if(this.isSelected) {
if(!isSelected) {
//erase
this.eraseAnchors(graphics2D);
}
} else {
if(isSelected) {
//draw
this.drawAnchors(graphics2D);
}
}
this.isSelected = isSelected;
}
public void draw(Graphics2D graphics) {
graphics.draw(this.shape);
}
public void animate(Graphics2D graphics2d, int x, int y) {
//erase;
this.draw(graphics2d);
// //move point
this.movePoint(x,y);
// //draw;
this.draw(graphics2d);
}
//interface
public abstract GShapeTool clone();
public abstract void setInitialPoint(int x, int y);
public abstract void setFinalPoint(int x, int y);
public abstract void setIntermediatePoint(int x, int y);
public abstract void movePoint(int x, int y);
}
package shapeTool;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import main.GConstants.EDrawingStyle;
public class GRectangle extends GShapeTool {
//attributes
private static final long serialVersionUID = 1L;
//components
//constructor
public GRectangle() {
super(EDrawingStyle.e2PointDrawing);
this.shape = new Rectangle();
}
@Override
public GShapeTool clone() {
GShapeTool cloned = new GRectangle();
return cloned;
}
// methods
@Override
public void setInitialPoint(int x, int y) {
Rectangle rectangle = (Rectangle) this.shape;
rectangle.setLocation(x, y);
rectangle.setSize(0, 0);
}
@Override
public void setIntermediatePoint(int x, int y) {
// TODO Auto-generated method stub
}
@Override
public void setFinalPoint(int x, int y) {
}
@Override
public void movePoint(int x, int y) {
Rectangle rectangle = (Rectangle) this.shape;
rectangle.setSize(x-rectangle.x, y-rectangle.y);
}
}