2
안녕하세요 저는 셰이프를 선택할 수있는 페인트 셰이프 프로그램에서 이제 원하는 크기로 원을 만들기 위해 드래그합니다. 그러나 나는 오른쪽에서 왼쪽으로 왼쪽에서 오른쪽으로 끌 수있는 한 가지 문제에 직면합니다.자바 스윙에서 오른쪽에서 왼쪽으로 셰이프를 드래그
참조 사진 enter image description here
이 내 패널 그릴 나는 문제가 에 당신은 그 그리기 기능을 부정 폭과 높이 값을 사용할 수 없습니다
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 좌표를 업데이트하고 양수 너비를 계산해야한다는 것을 알아야합니다. – AJNeufeld