在 java swing 中从右向左拖动一个形状
Drag a shape from right to left in java swing
你好,我现在正在开发一个绘制形状程序,你可以 select 形状然后拖动它来制作一个圆圈,例如你想要的大小。
但是我面临一个问题,就是我只能从左向右拖动而不是从右向左拖动。
见图
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);
}
}
这些绘图函数不能使用负宽度和高度值。
相反,您必须检测负宽度,并调整起始坐标以保持宽度非负。
public void mouseDragged(MouseEvent e) {
Point newPoint = new Point(e.getX(), e.getY());
int xStart = oldPoint.x;
int yStart = oldPointy;
int width = newPoint.x - xStart;
int height = newPoint.y - yStart;
if (width < 0) {
width = -width;
xStart -= width;
}
if (height < 0) {
height = -height;
yStart -= height;
}
if (shape.equals("1")) {
image = cloneImage(originalImage);
drawCir(xStart, yStart, width, height);
repaint();
}
...etc...
你好,我现在正在开发一个绘制形状程序,你可以 select 形状然后拖动它来制作一个圆圈,例如你想要的大小。 但是我面临一个问题,就是我只能从左向右拖动而不是从右向左拖动。
见图 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);
}
}
这些绘图函数不能使用负宽度和高度值。
相反,您必须检测负宽度,并调整起始坐标以保持宽度非负。
public void mouseDragged(MouseEvent e) {
Point newPoint = new Point(e.getX(), e.getY());
int xStart = oldPoint.x;
int yStart = oldPointy;
int width = newPoint.x - xStart;
int height = newPoint.y - yStart;
if (width < 0) {
width = -width;
xStart -= width;
}
if (height < 0) {
height = -height;
yStart -= height;
}
if (shape.equals("1")) {
image = cloneImage(originalImage);
drawCir(xStart, yStart, width, height);
repaint();
}
...etc...