【发布时间】:2017-11-27 17:17:35
【问题描述】:
您好,我现在正在开发一个绘制形状程序,您可以选择形状然后拖动它来制作一个圆形,例如您想要的大小。 但是我面临一个问题,就是我只能从左向右拖动,而不是从右向左拖动。
看图片 enter image description here
这是我的面板画 我认为问题出在 mouseDragged
class DrawPanel extends JPanel {
MouseMotionListener m2 = new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
Point newPoint = new Point(e.getX(), e.getY());
if (shape == "1") {
image = cloneImage(originalImage);
int width = newPoint.x - oldPoint.x;
int hieght = newPoint.y - oldPoint.y;
drawCir(oldPoint.x, oldPoint.y, width, width);
repaint();
}
else if (shape == "2") {
image = cloneImage(originalImage);
int width = newPoint.x - oldPoint.x;
int hieght = newPoint.y - oldPoint.y;
drawOval(oldPoint.x, oldPoint.y, width, hieght);
}
else if (shape == "3") {
image = cloneImage(originalImage);
int width = newPoint.x - oldPoint.x;
int hieght = newPoint.y - oldPoint.y;
drawRec(oldPoint.x, oldPoint.y, width, hieght);
}
else if (shape == "4") {
image = cloneImage(originalImage);
int width = newPoint.x - oldPoint.x;
int hieght = newPoint.y - oldPoint.y;
drawSqu(oldPoint.x, oldPoint.y, width, width);
}
else if (shape == "5") {
image = cloneImage(originalImage);
drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y);
}
}
};
{
this.addMouseMotionListener(m2);
}
MouseListener m1 = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
oldPoint = new Point(e.getX(), e.getY());
originalImage = cloneImage(image);
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
};
{
this.addMouseListener(m1);
}
public DrawPanel() {
setBackground(Color.white);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (image == null) {
image = new BufferedImage(super.getWidth(), super.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE);
}
g2d.drawImage(image, 0, 0, null);
}
public void drawCir(int x, int y, int w, int h) {
Graphics2D g = image.createGraphics();
g.setColor(Color.black);
if (drawSetting.getColor().equals("Black")) {
g.setColor(Color.black);
}
else if (drawSetting.getColor().equals("Blue")) {
g.setColor(Color.BLUE);
}
else if (drawSetting.getColor().equals("Red")) {
g.setColor(Color.RED);
}
else if (drawSetting.getColor().equals("Green")) {
g.setColor(Color.GREEN);
}
if (drawSetting.getFilled() == false) {
g.drawOval(x, y, w, w);
} else {
g.fillOval(x, y, w, w);
}
repaint();
}
public void drawOval(int x, int y, int w, int h) {
Graphics2D g = image.createGraphics();
g.setColor(Color.black);
if (drawSetting.getColor().equals("Black")) {
g.setColor(Color.black);
}
else if (drawSetting.getColor().equals("Blue")) {
g.setColor(Color.BLUE);
}
else if (drawSetting.getColor().equals("Red")) {
g.setColor(Color.RED);
}
else if (drawSetting.getColor().equals("Green")) {
g.setColor(Color.GREEN);
}
if (drawSetting.getFilled() == false) {
g.drawOval(x, y, w, h);
} else {
g.fillOval(x, y, w, h);
}
repaint();
}
public void drawRec(int x, int y, int w, int h) {
Graphics2D g = image.createGraphics();
if (drawSetting.getColor().equals("Black")) {
g.setColor(Color.black);
}
else if (drawSetting.getColor().equals("Blue")) {
g.setColor(Color.BLUE);
}
else if (drawSetting.getColor().equals("Red")) {
g.setColor(Color.RED);
}
else if (drawSetting.getColor().equals("Green")) {
g.setColor(Color.GREEN);
}
if (drawSetting.getFilled() == false) {
g.drawRect(x, y, w, h);
} else {
g.fillRect(x, y, w, h);
}
repaint();
}
public void drawSqu(int x, int y, int w, int h) {
Graphics2D g = image.createGraphics();
if (drawSetting.getColor().equals("Black")) {
g.setColor(Color.black);
}
else if (drawSetting.getColor().equals("Blue")) {
g.setColor(Color.BLUE);
}
else if (drawSetting.getColor().equals("Red")) {
g.setColor(Color.RED);
}
else if (drawSetting.getColor().equals("Green")) {
g.setColor(Color.GREEN);
}
if (drawSetting.getFilled() == false) {
g.drawRect(x, y, w, w);
} else {
g.fillRect(x, y, w, w);
}
repaint();
}
public void drawLine(int x1, int y1, int x2, int y2) {
Graphics2D g = image.createGraphics();
if (drawSetting.getColor().equals("Black")) {
g.setColor(Color.black);
}
else if (drawSetting.getColor().equals("Blue")) {
g.setColor(Color.BLUE);
}
else if (drawSetting.getColor().equals("Red")) {
g.setColor(Color.RED);
}
else if (drawSetting.getColor().equals("Green")) {
g.setColor(Color.GREEN);
}
if (drawSetting.getFilled() == false) {
g.drawLine(x1, y1, x2, y2);
} else {
g.drawLine(x1, y1, x2, y2);
}
repaint();
}
public void setShape(String s) {
shape = s;
}
private BufferedImage cloneImage(BufferedImage image2) {
if (image2 == null) {
return null;
}
ColorModel cm = image2.getColorModel();
boolean isAplpha = cm.isAlphaPremultiplied();
WritableRaster raster = image2.copyData(null);
return new BufferedImage(cm, raster, isAplpha, null);
}
}
【问题讨论】:
-
从右向左拖动时,宽度变为负数(类似向上拖动会导致高度为负数)。您需要认识到,当您拖动到起点的左侧时,您需要更新 x 坐标并计算正宽度,而不是尝试将起点保持为恒定的负宽度,这是行不通的。
标签: java swing user-interface paint drag