形状之间的连接线位置错误

时间:2018-08-30 13:24:14

标签: java swing graphics geometry shapes

这里是场景:我可以在JFrame中绘制圆形和矩形,也可以选择它们(一个或两个)。

这些形状存储在Shapes的ArrayList中(有3个类,即Line,Rectangle和Circle,它们扩展了Shape的超类)。

它运行良好,但是我需要添加另一个功能。当我选择2种形状时,必须在它们之间画一条线,将它们连接起来。

问题是:即使我调用repaint()方法,线也没有被绘制。

与问题相关的代码:

形状类(超类):

import java.awt.*;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.*;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

public class Shape extends JPanel implements Serializable {
protected Point begin;
protected Point end;
protected int width;
protected int height;
protected int id;
@XStreamOmitField protected boolean full;
protected Color color;
protected String shape;
@XStreamOmitField protected boolean isSelected; 

public int getId(){
    return id;
}
public void setId(int id){
    this.id = id;
}   
public boolean getIsSelected(){
    return isSelected;
}
public void setIsSelected(boolean isSelected){
    this.isSelected = isSelected;
}
public Point getBegin() {
    return begin;
}
public void setBegin(Point begin) {
    this.begin = begin;
}
public Point getEnd() {
    return end;
}
public void setEnd(Point end) {
    this.end = end;
}
public int getWidth() {
    return width;
}
public void setWidth(int width) {
    this.width = width;
}
public int getHeight() {
    return height;
}
public void setHeight(int height){
    this.height = height;
}
public boolean getIsFull(){
    return full;
}
public void setFull(boolean full){
    this.full = full;
}
public Color getColor(){
    return color;
}
public void setColor(Color color){
    this.color = color;
}
public void setShape(String shape){
    this.shape = shape;
}
public String getShape(){
    return shape;
}

@Override
public String toString() {
    return "ID: " + getId() + java.lang.System.lineSeparator() +  "Begin x: " + begin.x + java.lang.System.lineSeparator() + "Begin y: " + begin.y + java.lang.System.lineSeparator() + "End x: " + end.x + java.lang.System.lineSeparator() + "End y: " + end.y + java.lang.System.lineSeparator() + "Width: " + width + java.lang.System.lineSeparator() + "Height: " + height + java.lang.System.lineSeparator() + "Color: " + color + java.lang.System.lineSeparator() + "Shape: " + shape + java.lang.System.lineSeparator() + java.lang.System.lineSeparator();
    //return "ID: " + getId() + java.lang.System.lineSeparator() +  "Begin x: " + begin.x + java.lang.System.lineSeparator() + "Begin y: " + begin.y + java.lang.System.lineSeparator() + "End x: " + end.x + java.lang.System.lineSeparator() + "End y: " + end.y + java.lang.System.lineSeparator() + "Width: " + width + java.lang.System.lineSeparator() + "Height: " + height + java.lang.System.lineSeparator() + "Color: " + color + java.lang.System.lineSeparator() + "Shape: " + shape + java.lang.System.lineSeparator() + java.lang.System.lineSeparator() + "New Fields: " + newFields + java.lang.System.lineSeparator();
    //return "Begin: " + getBegin() + "\nEnd: " + "\nWidth: " + getWidth() + "\nHeight: " + getHeight() + "\nFull: " + isFull() + "\nColor: " + getColor();

}

//Constructors

public Shape(Point begin, Point end, int width, int height, boolean full, Color color, String shape) {
    this();
    this.begin = begin;
    this.end = end;
    this.width = width;
    this.height = height;
    this.full = full;
    this.color = color;
    this.shape = shape;
}

public Shape(Point begin, Point end, int width, int height, boolean full, Color color, String shape, HashMap newFields) {
    this();
    this.begin = begin;
    this.end = end;
    this.width = width;
    this.height = height;
    this.full = full;
    this.color = color;
    this.shape = shape;
    this.newFields = newFields;
}

public Shape(Point begin, Point end, int width, int height, boolean full, Color color, String shape, boolean isSelected) {
    this();
    this.begin = begin;
    this.end = end;
    this.width = width;
    this.height = height;
    this.full = full;
    this.color = color;
    this.shape = shape;
    this.isSelected = isSelected;
}

public Shape(Point begin, Point end, int width, int height, boolean full, Color color, String shape, boolean isSelected, HashMap newFields) {
    this();
    this.begin = begin;
    this.end = end;
    this.width = width;
    this.height = height;
    this.full = full;
    this.color = color;
    this.shape = shape;
    this.isSelected = isSelected;
    this.newFields = newFields;
}

public Shape(Point begin, Point end, boolean full, Color color, String shape) {
    this();
    this.begin = begin;
    this.end = end;
    this.full = full;
    this.color = color;
    this.shape = shape;
}

public Shape(Point begin, Point end, boolean full, Color color, String shape, boolean isSelected) {
    this();
    this.begin = begin;
    this.end = end;
    this.full = full;
    this.color = color;
    this.shape = shape;
    this.isSelected = isSelected;
}

public Shape() {
    super();
    setOpaque(false);
}
}

线路类别:

import java.awt.*;
import java.io.Serializable;

    public class Line extends Shape implements Serializable{

    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(s);
        g2d.setColor(Color.RED);
        if((begin.x > end.x) && (begin.y < end.y)) {
            g2d.drawLine(0, height, width, 0);
        }
        else if((begin.x < end.x) && (begin.y > end.y)){
            g2d.drawLine(0, height, width, 0);
        }
        else g2d.drawLine(0,  0, width, height);
    }

      //Constructors
      public Line() {
         super();
      }

      public Line(Point begin, Point end, int width, int height, boolean full, Color color, String shape) {
        super(begin, end, width, height, full, color, shape);
      }
   }

一段代码(正在运行),该代码打印圆圈(在mouseListener中):

                c.setShape("Circle");                   
                //Axis
                x1 = Math.min(mousePressed.x, mouseReleased.x);
                y1 = Math.min(mousePressed.y, mouseReleased.y);
                begin.setLocation(x1, y1);  
                c.setBegin(mousePressed);
                c.setEnd(mouseReleased);
                c.setId(count);
                c.setWidth(Math.abs(mouseReleased.x - mousePressed.x));
                c.setHeight(Math.abs(mouseReleased.y - mousePressed.y));
                c.setColor(choosenColor);
                c.setLocation(begin);
                c.setSize(c.getWidth(), c.getHeight());
                c.setFull(full);
                for(int i=0; i<images.size(); i++){
                    if(c.getBounds().intersects(images.get(i).getBounds())){
                        flag = false;
                        }
                }                   
                if(flag == true){
                    drawPanel.add(c);
                    drawPanel.repaint();
                    images.add(c);
                    colors.add(choosenColor);
                    count++;
                }

这就是代码损坏的一部分:

public void mouseClicked(MouseEvent e){
                boolean found = false;
                int counter = 0;
                Line line = new Line();
                //TRYING TO DEBUG PRINTING THE LINE, BUT IT DOES NOT PRINT
                line.setColor(Color.RED);
                JOptionPane.showMessageDialog(null, line);
                for(int i=0; i<images.size(); i++){
                //CHECKING IF CLICK IS INSIDE A SHAPE
                    if((images.get(i).getLocation().getX() < e.getX() && images.get(i).getLocation().getY() < e.getY() && images.get(i).getX() + images.get(i).getWidth() > e.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > e.getY())){
                        images.get(i).setColor(Color.BLUE);
                        images.get(i).setIsSelected(true);
                        found = true;
                    }
                    if(images.get(i).getColor() == Color.BLUE){
                        counter++;
                    }
                    if(counter > 2){
                        images.get(i).setColor(colors.get(i));
                        images.get(i).setIsSelected(false);
                        found = false;
                        selectedImages.clear();
                    }
                }

                if (!found){
                    counter = 0;
                    selectedImages.clear();
                    for(int j=0; j<images.size(); j++){
                        images.get(j).setColor(colors.get(j));
                        images.get(j).setIsSelected(false);
                    }
                }
                repaint();

                if(counter == 2){
                    for(Shape s : images){
                        if(s.getIsSelected() == true){
                            selectedImages.add(s);
                        }
                    }
                if(selectedImages.size() == 2){
                    Point p1 = new Point();
                    Point p2 = new Point();
                    p1.x = selectedImages.get(0).getWidth()/2;
                    p1.y = selectedImages.get(0).getHeight()/2;
                    p2.x = selectedImages.get(1).getWidth()/2;
                    p2.y = selectedImages.get(1).getHeight()/2;
                    line.setShape("Line");
                    line.setBegin(p1);
                    line.setEnd(p2);
                    line.setId(count);
                    line.setFull(full);
                    line.setWidth(Math.abs(p2.x - p1.x));
                    line.setHeight(Math.abs(p2.y - p1.y));
                    line.setColor(Color.RED);
                    drawPanel.add(line);
                    repaint();
                    JOptionPane.showMessageDialog(null, line);
                    JOptionPane.showMessageDialog(null, line.toString());
                    }                       
                }
                    selectedImages.clear();
  

PS:当我打印line.toString()时,它向我显示了所有正确的属性..奇怪..例如:

     
      
  • ID:2
  •   
  • x开头:68
  •   
  • 从y开始:45
  •   
  • 结尾x:73
  •   
  • y尾:37
  •   
  • 宽度:5
  •   
  • 高度:8
  •   
  • 颜色:java.awt.Color [r = 255,g = 0,b = 0]
  •   
  • 形状:线
  •   

如果我不清楚自己的意思,请随时询问更多代码。

谢谢!

编辑:这是一个截屏,可以准确显示我在说什么。看红线。高度和宽度正确,但是起始坐标不正确。该行应从第一个选定矩形的中心开始,并应在第二个选定矩形的中心结束。

An example

0 个答案:

没有答案