Example #1
0
  public void repaintObjects(List<Component> list) {
    this.removeAll();
    for (Component component : list) {
      StandartFigure standartFigure = (StandartFigure) component;

      switch (standartFigure.getTypeFigure()) {
        case Circle:
          standartFigure.setBounds(
              standartFigure.getX(),
              standartFigure.getY(),
              standartFigure.getWidth() + 2,
              standartFigure.getHeight() + 2);
          break;
        case Square:
          standartFigure.setBounds(
              standartFigure.getX(),
              standartFigure.getY(),
              standartFigure.getWidth() + 2,
              standartFigure.getHeight() + 2);
          break;
        case RoundRect:
          standartFigure.setBounds(
              standartFigure.getX(),
              standartFigure.getY(),
              standartFigure.getWidth() + 2,
              standartFigure.getHeight() + 2);
          break;
        case Line:
          standartFigure.setBounds(
              standartFigure.getX(),
              standartFigure.getY(),
              standartFigure.getWidth() + 2,
              standartFigure.getHeight() + 2);
          break;

        default:
          break;
      }
      add(standartFigure);
      repaint();
      standartFigure = null;
    }
  }
  @Override
  public void save(List<Component> components) {
    try {
      FileOutputStream fos = new FileOutputStream(filename);
      StringBuilder xml = new StringBuilder();
      xml.append("<FigureList>");
      for (Component component : components) {
        xml.append("<Figure>");
        StandartFigure figure = (StandartFigure) component;

        switch (figure.getTypeFigure()) {
          case Circle:
            xml.append("<Type>").append("Circle").append("</Type>");
            xml.append("<X>").append(figure.getX()).append("</X>");
            xml.append("<Y>").append(figure.getY()).append("</Y>");
            xml.append("<Width>").append(figure.getWidth()).append("</Width>");
            xml.append("<Height>").append(figure.getHeight()).append("</Height>");
            xml.append("<BoundColor>");
            xml.append("<R>").append(figure.getColor().getRed()).append("</R>");
            xml.append("<G>").append(figure.getColor().getGreen()).append("</G>");
            xml.append("<B>").append(figure.getColor().getBlue()).append("</B>");
            xml.append("</BoundColor>");
            break;
          case Square:
            xml.append("<Type>").append("Circle").append("</Type>");
            xml.append("<X>").append(figure.getX()).append("</X>");
            xml.append("<Y>").append(figure.getY()).append("</Y>");
            xml.append("<Width>").append(figure.getWidth()).append("</Width>");
            xml.append("<Height>").append(figure.getHeight()).append("</Height>");
            xml.append("<BoundColor>");
            xml.append("<R>").append(figure.getColor().getRed()).append("</R>");
            xml.append("<G>").append(figure.getColor().getGreen()).append("</G>");
            xml.append("<B>").append(figure.getColor().getBlue()).append("</B>");
            xml.append("</BoundColor>");
            break;
          case RoundRect:
            xml.append("<Type>").append("RoundRect").append("</Type>");
            xml.append("<X>").append(figure.getX()).append("</X>");
            xml.append("<Y>").append(figure.getY()).append("</Y>");
            xml.append("<Width>").append(figure.getWidth()).append("</Width>");
            xml.append("<Height>").append(figure.getHeight()).append("</Height>");
            xml.append("<BoundColor>");
            xml.append("<R>").append(figure.getColor().getRed()).append("</R>");
            xml.append("<G>").append(figure.getColor().getGreen()).append("</G>");
            xml.append("<B>").append(figure.getColor().getBlue()).append("</B>");
            xml.append("</BoundColor>");
            break;
          case Line:
            xml.append("<Type>").append("Line").append("</Type>");
            xml.append("<X>").append(figure.getX()).append("</X>");
            xml.append("<Y>").append(figure.getY()).append("</Y>");
            xml.append("<Width>").append(figure.getWidth()).append("</Width>");
            xml.append("<BoundColor>");
            xml.append("<R>").append(figure.getColor().getRed()).append("</R>");
            xml.append("<G>").append(figure.getColor().getGreen()).append("</G>");
            xml.append("<B>").append(figure.getColor().getBlue()).append("</B>");
            xml.append("</BoundColor>");
            break;
          default:
            break;
        }
        xml.append("</Figure>");
      }
      fos.write(new String(xml).getBytes());
      String footer = "</FigureList>";
      fos.write(footer.getBytes());
      fos.flush();
      fos.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #3
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;
  }