Пример #1
0
 public void reset() {
   Contact c = selectedContact;
   if (c != null) {
     name.setText(c.getName());
     try {
       ImageIcon icon =
           new ImageIcon(
               (ResourceHelper.getImageObject(c.getImageLoc()))
                   .getScaledInstance(110, 100, Image.SCALE_DEFAULT));
       picture.setIcon(icon);
     } catch (Exception ex) {
       picture.setIcon(null);
       System.out.println("DetPane > changeContact(...) > Error in setting image : " + ex);
     }
     detail1.setText("");
     detail2.setText("");
     if (c.getSummary() != null) {
       String[] smry = c.getSummary();
       if (smry.length >= 1) {
         detail1.setText(smry[0]);
       } else {
         detail1.setText("");
       }
       if (smry.length >= 2) {
         detail2.setText(smry[1]);
       } else {
         detail2.setText("");
       }
     }
   }
   resetDetailPane();
 }
Пример #2
0
 private void setButtonIcon(String res, JButton button) {
   try {
     Image image = ResourceHelper.getImageObject(res);
     image = image.getScaledInstance(25, 25, Image.SCALE_SMOOTH);
     ImageIcon editicon = new ImageIcon(image);
     button.setIcon(editicon);
   } catch (IOException ex) {
     System.out.println("DetInnerPane > setButtonIcon(...) > Error : " + ex);
   }
 }
  @Override
  public Image createNewImage(int parentWidth, int parentHeight) {
    Image image = createEmptyImage(parentWidth, parentHeight);
    try {
      File temp = ResourceHelper.getResource("APPDATA_DIRECTORY");
      int width = image.getWidth(null);
      int height = image.getHeight(null);
      Graphics2D g2d = (Graphics2D) image.getGraphics();
      float opacity = (float) getOpacity() / 100;
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));

      // Create image
      if (geocodeResult == null) {
        retrieveLocation();
      }
      DailyWeatherForecast dwf = new DailyWeatherForecast();
      DailyForecastResponse resp = dwf.forecast(latitude, longitude, 6);
      DayForecast currDay = resp.getForecasts().get(0);
      DayWeather currWeather = currDay.getWeather().get(0);

      // Create image
      {
        int py = 0, ny = 10, gap = 2, pad = 10, nw = 10;
        int w = 300;
        int h =
            pad
                + (locationIncluded ? 27 : 0)
                + ((iconIncluded || descriptionIncluded) ? 32 : 0)
                + ((temperatureIncluded || windSpeedIncluded || humidityIncluded) ? 34 : 0)
                + (forecastIncluded ? 40 : 0)
                + pad;
        Image tempImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) tempImage.getGraphics();
        Rectangle2D rect;
        FontRenderContext frc = g.getFontRenderContext();
        Font addressFont = new Font("Ebrima", Font.PLAIN, 18);
        Font normFont = new Font("Ebrima", Font.PLAIN, 18);
        Font smallFont = new Font("Ebrima", Font.PLAIN, 10);
        g.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        Color txtClr = Color.BLACK;
        Color addrBarClr = Color.GRAY;
        Color col1 = Color.BLACK;
        Color col2 = Color.GRAY;
        Color col3 = Color.WHITE;

        if (isRectangular) {
          g.setPaint(
              new GradientPaint(new Point(0, 0), col2, new Point(0, height), backgroundColor));
          g.fillRoundRect(0, 0, w, h, 20, 20);
        }

        if (locationIncluded) {
          g.setFont(addressFont);
          rect = addressFont.getStringBounds(location, frc);
          g.setPaint(
              new GradientPaint(
                  new Point(pad, ny), col2, new Point(pad, (int) (ny + rect.getHeight())), col3));
          g.fillRoundRect(pad, ny, w - pad * 2, (int) rect.getHeight(), 10, 10);
          g.setColor(txtClr);
          g.drawString(location, (int) ((w - rect.getWidth()) / 2), (int) (ny - rect.getY()));
          py = ny;
          ny += rect.getHeight() + gap;
        }

        if (iconIncluded || descriptionIncluded) {

          g.setPaint(new GradientPaint(new Point(pad, ny), col2, new Point(pad, ny + 30), col3));
          g.fillRoundRect(pad, ny, w - pad * 2, 30, 10, 10);

          if (iconIncluded) {
            String iconStr = currWeather.getIcon();
            File iconFile = new File(temp, iconStr + "_info.png");
            Image icon = ResourceHelper.getImageObject(iconFile);
            if (icon == null) {
              icon =
                  ResourceHelper.getImageObject(
                      new URL("http://openweathermap.org/img/w/" + iconStr + ".png"));
              ImageIO.write((RenderedImage) icon, "png", iconFile);
            }
            g.drawImage(icon, pad, ny - 9, null);
          }

          if (isDescriptionIncluded()) {
            g.setFont(normFont);
            g.setColor(txtClr);
            rect = normFont.getStringBounds(currWeather.getDescription(), frc);
            g.drawString(
                currWeather.getDescription(),
                (int) (pad + 20 + (w - pad - 20 - rect.getWidth()) / 2),
                (int) (ny - rect.getY()));
          }

          py = ny;
          ny += 30 + gap;
        }

        if (temperatureIncluded || windSpeedIncluded || humidityIncluded) {
          g.setPaint(new GradientPaint(new Point(pad, ny), col2, new Point(pad, ny + 32), col3));
          g.fillRoundRect(pad, ny, w - pad * 2, 32, 10, 10);
          g.setColor(txtClr);

          int x = pad + gap + 5;
          if (temperatureIncluded) {
            DayTemperature dTemp = currDay.getTemp();
            String min = String.format("%.1f%cC", dTemp.getMinTemp(), DayWeather.DEGREE);
            rect = normFont.getStringBounds(min, frc);
            g.setFont(normFont);
            g.drawString(min, x, (int) (ny - rect.getY()));
            g.setFont(smallFont);
            g.drawString("min", x + 5, (int) (ny + 5 + rect.getHeight()));
            x += gap + 60;
            String max = String.format("%.1f%cC", dTemp.getMaxTemp(), DayWeather.DEGREE);
            rect = normFont.getStringBounds(max, frc);
            g.setFont(normFont);
            g.drawString(max, x, (int) (ny - rect.getY()));
            g.setFont(smallFont);
            g.drawString("max", x + 5, (int) (ny + 5 + rect.getHeight()));
            x += gap + 60;
          }

          if (windSpeedIncluded) {
            String speed = String.format("%2.1fm/s", currDay.getWindSpeed());
            rect = normFont.getStringBounds(speed, frc);
            g.setFont(normFont);
            g.drawString(speed, x + 5, (int) (ny - rect.getY()));
            g.setFont(smallFont);
            g.drawString("wind", x + 15, (int) (ny + 5 + rect.getHeight()));
            x += gap + 80;
          }

          if (humidityIncluded) {
            String humidity = String.format("%2d%%", currDay.getHumidity());
            rect = normFont.getStringBounds(humidity, frc);
            g.setFont(normFont);
            g.drawString(humidity, x + 5, (int) (ny - rect.getY()));
            g.setFont(smallFont);
            g.drawString("humidity", x + 5, (int) (ny + 5 + rect.getHeight()));
          }
          py = ny;
          ny += 32 + gap;
        }

        if (forecastIncluded) {
          g.setPaint(new GradientPaint(new Point(pad, ny), col2, new Point(pad, ny + 40), col3));
          g.fillRoundRect(pad, ny, w - pad * 2, 40, 10, 10);
          g.setColor(txtClr);
          g.setFont(smallFont);
          List<DayForecast> forecasts = resp.getForecasts();
          int x = pad;
          for (int i = 1; i < 5; i++) {
            DayWeather weather = forecasts.get(i).getWeather().get(0);
            String iconStr = weather.getIcon();
            File iconFile = new File(temp, iconStr + "_info.png");
            Image icon = ResourceHelper.getImageObject(iconFile);
            if (icon == null) {
              icon =
                  ResourceHelper.getImageObject(
                      new URL("http://openweathermap.org/img/w/" + iconStr + ".png"));
              ImageIO.write((RenderedImage) icon, "png", iconFile);
            }
            g.drawImage(icon, x, ny - 9, null);
            g.drawString(weather.getMain(), x + 10, ny + 35);
            x += 70;
          }
        }

        g.dispose();

        float s1 = (float) width / w;
        float s2 = (float) height / h;
        float s = s1 < s2 ? s1 : s2;
        int newWidth = (int) (s * w);
        tempImage = tempImage.getScaledInstance(newWidth, -1, Image.SCALE_SMOOTH);
        w = tempImage.getWidth(null);
        h = tempImage.getHeight(null);
        int locx, locy;
        locx = (width - w) / 2;
        locy = (height - h) / 2;
        g2d.drawImage(tempImage, locx, locy, null);
      }

      g2d.dispose();
    } catch (Exception ex) {
      ResourceHelper.errLog("EmbedTypeWeatherInfo > createNewImage(...) > Error : " + ex);
    }
    embedTypeImage = image;
    return image;
  }
  @Override
  protected void readXMLElementInnerData(Node node) {
    Node n;
    NamedNodeMap nnm;
    String nm;

    NodeList nodes = node.getChildNodes();
    int l = nodes.getLength();

    for (int i = 0; i < l; i++) {
      n = nodes.item(i);
      if (n.getNodeType() == Node.ELEMENT_NODE) {
        try {
          nm = n.getNodeName();
          nnm = n.getAttributes();
          switch (nm) {
            case "location":
              location = nnm.getNamedItem("value").getNodeValue();
              break;
            case "address":
              address = nnm.getNamedItem("value").getNodeValue();
              break;
            case "latitude":
              latitude = Float.parseFloat(nnm.getNamedItem("value").getNodeValue());
              break;
            case "longitude":
              longitude = Float.parseFloat(nnm.getNamedItem("value").getNodeValue());
              break;
            case "location_included":
              locationIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "icon_included":
              iconIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "description_included":
              descriptionIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "temperature_included":
              temperatureIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "is_fahrenheit":
              isFahrennheit = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "humidity_included":
              humidityIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "precipitation_included":
              windSpeedIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "forecast_included":
              forecastIncluded = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "is_rectangular":
              isRectangular = Boolean.parseBoolean(nnm.getNamedItem("value").getNodeValue());
              break;
            case "text_color":
              textColor = new Color(Integer.parseInt(nnm.getNamedItem("value").getNodeValue()));
              break;
            case "background_color":
              backgroundColor =
                  new Color(Integer.parseInt(nnm.getNamedItem("value").getNodeValue()));
              break;
          }
        } catch (Exception ex) {
          ResourceHelper.errLog(
              "EmbedTypeWeatherInfo > readXMLElementInnerData(Node) > Error : " + ex);
        }
      }
    }
  }