Example #1
0
    @Override
    public void mouseClicked(MouseEvent e) {
      if (SwingUtilities.isRightMouseButton(e)) {
        grabFocus();
      }

      if (SwingUtilities.isLeftMouseButton(e)) {
        curPoint = e.getPoint();
        Color color = new Color(randomInt(), randomInt(), randomInt());
        switch (paint.getSelectedIndex()) {
          case 1:
            StandartFigure circle = new StandartFigure(TypeFigure.Circle, curPoint);
            circle.setBounds(curPoint.x, curPoint.y, 52, 52);
            circle.setColor(color);
            add(circle);
            break;
          case 2:
            StandartFigure square = new StandartFigure(TypeFigure.Square, curPoint);
            square.setBounds(curPoint.x, curPoint.y, 52, 52);
            square.setColor(color);
            add(square);
            break;
          case 3:
            StandartFigure line = new StandartFigure(TypeFigure.Line, curPoint);
            line.setBounds(curPoint.x, curPoint.y, 52, 52);
            line.setColor(color);
            add(line);
            break;
          case 4:
            StandartFigure roundRectangle = new StandartFigure(TypeFigure.RoundRect, curPoint);
            roundRectangle.setBounds(curPoint.x, curPoint.y, 52, 52);
            roundRectangle.setColor(color);
            add(roundRectangle);
            break;
          default:
            System.out.println("Error index Figure");
            break;
        }
      }
    }
  @Override
  public List<Component> load() {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    List<Component> components = new ArrayList<Component>();
    int x = 0;
    int y = 0;
    int width = 0;
    int height = 0;
    Color color = null;
    Document doc = null;
    try {
      dBuilder = dbFactory.newDocumentBuilder();
      doc = dBuilder.parse(filename);
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    doc.getDocumentElement().normalize();

    Node figureList = doc.getChildNodes().item(0);
    NodeList figures = figureList.getChildNodes();

    StandartFigure standartFigure;
    for (int i = 0; i < figures.getLength(); i++) {
      Node figure = figures.item(i);

      if (figure.getFirstChild().getTextContent().equals("Circle")) {
        NodeList nodes = figure.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
          if (nodes.item(j).getNodeName().equals("X"))
            x = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Y"))
            y = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Width"))
            width = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Height"))
            height = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("BoundColor")) {
            NodeList rgb = nodes.item(j).getChildNodes();
            color =
                new Color(
                    Integer.parseInt(rgb.item(0).getTextContent()),
                    Integer.parseInt(rgb.item(1).getTextContent()),
                    Integer.parseInt(rgb.item(2).getTextContent()));
          }
        }
        standartFigure = new StandartFigure(TypeFigure.Circle, new Point(x, y));
        standartFigure.setLocation(new Point(x, y));
        standartFigure.setDimension(width, height);
        standartFigure.setColor(color);
        components.add(standartFigure);
      }
      if (figure.getFirstChild().getTextContent().equals("Square")) {
        NodeList nodes = figure.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
          if (nodes.item(j).getNodeName().equals("X"))
            x = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Y"))
            y = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Width"))
            width = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Height"))
            height = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("BoundColor")) {
            NodeList rgb = nodes.item(j).getChildNodes();
            color =
                new Color(
                    Integer.parseInt(rgb.item(0).getTextContent()),
                    Integer.parseInt(rgb.item(1).getTextContent()),
                    Integer.parseInt(rgb.item(2).getTextContent()));
          }
        }
        standartFigure = new StandartFigure(TypeFigure.Square, new Point(x, y));
        standartFigure.setDimension(width, height);
        standartFigure.setLocation(new Point(x, y));
        standartFigure.setColor(color);
        components.add(standartFigure);
      }
      if (figure.getFirstChild().getTextContent().equals("Line")) {
        NodeList nodes = figure.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
          if (nodes.item(j).getNodeName().equals("X"))
            x = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Y"))
            y = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Width"))
            width = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Height"))
            height = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("BoundColor")) {
            NodeList rgb = nodes.item(j).getChildNodes();
            color =
                new Color(
                    Integer.parseInt(rgb.item(0).getTextContent()),
                    Integer.parseInt(rgb.item(1).getTextContent()),
                    Integer.parseInt(rgb.item(2).getTextContent()));
          }
        }
        standartFigure = new StandartFigure(TypeFigure.Line, new Point(x, y));
        standartFigure.setDimension(width, height);
        standartFigure.setLocation(new Point(x, y));
        standartFigure.setColor(color);
        components.add(standartFigure);
      }
      if (figure.getFirstChild().getTextContent().equals("RoundRect")) {
        NodeList nodes = figure.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
          if (nodes.item(j).getNodeName().equals("X"))
            x = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Y"))
            y = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Width"))
            width = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("Height"))
            height = Integer.parseInt(nodes.item(j).getTextContent());
          if (nodes.item(j).getNodeName().equals("BoundColor")) {
            NodeList rgb = nodes.item(j).getChildNodes();
            color =
                new Color(
                    Integer.parseInt(rgb.item(0).getTextContent()),
                    Integer.parseInt(rgb.item(1).getTextContent()),
                    Integer.parseInt(rgb.item(2).getTextContent()));
          }
        }
        standartFigure = new StandartFigure(TypeFigure.RoundRect, new Point(x, y));
        standartFigure.setDimension(width, height);
        standartFigure.setLocation(new Point(x, y));
        standartFigure.setColor(color);
        components.add(standartFigure);
      }
    }
    return components;
  }