| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Visualizador | 
 | 
 | 1.6;1,6 | 
| 1 |  /** | |
| 2 |   * Copyright 2008 WebPhotos | |
| 3 |   * | |
| 4 |   * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 |   * you may not use this file except in compliance with the License. | |
| 6 |   * You may obtain a copy of the License at | |
| 7 |   * | |
| 8 |   *      http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 |   * | |
| 10 |   * Unless required by applicable law or agreed to in writing, software | |
| 11 |   * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 |   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 |   * See the License for the specific language governing permissions and | |
| 14 |   * limitations under the License. | |
| 15 |   */ | |
| 16 |  package net.sf.webphotos.gui; | |
| 17 | ||
| 18 |  import java.awt.BorderLayout; | |
| 19 |  import java.awt.Container; | |
| 20 |  import java.awt.Dimension; | |
| 21 |  import java.awt.Frame; | |
| 22 |  import java.awt.Graphics; | |
| 23 |  import java.awt.Image; | |
| 24 |  import java.awt.event.ActionEvent; | |
| 25 |  import java.awt.event.ActionListener; | |
| 26 |  import javax.swing.ImageIcon; | |
| 27 |  import javax.swing.JButton; | |
| 28 |  import javax.swing.JDialog; | |
| 29 | ||
| 30 |  /** | |
| 31 |   * Cria um visualizador de fotos numa janela popup. | |
| 32 |   * Calcula as dimensões da imagem e implementa a imagem na janela. | |
| 33 |   */ | |
| 34 | public class Visualizador extends JDialog implements ActionListener { | |
| 35 | ||
| 36 |      private Image foto; | |
| 37 | private int largura; | |
| 38 | private int altura; | |
| 39 | ||
| 40 |      /** | |
| 41 |       * Construtor da classe. | |
| 42 |       * Abre a janela popup com a foto. | |
| 43 |       * Recebe um frame para inclusão, nome da foto e um título para a janela. | |
| 44 |       * Testa se é necessário redimensionar a foto, caso positivo chama a função calculaRedução. | |
| 45 |       * Implementa um botão para fechar a janela e por último mostra a tela ao usuário. | |
| 46 |       * @param caminhoImagem Caminho da foto. | |
| 47 |       * @param frame Janela frame para alocar a foto. | |
| 48 |       * @param titulo Título da janela. | |
| 49 |       */ | |
| 50 |      public Visualizador(String caminhoImagem, Frame frame, String titulo) { | |
| 51 | 0 |          super(frame, titulo, true); | 
| 52 | 0 |          Container cp = this.getContentPane(); | 
| 53 | ||
| 54 | 0 |          cp.setLayout(new BorderLayout()); | 
| 55 | 0 |          foto = new ImageIcon(caminhoImagem).getImage(); | 
| 56 | 0 |          largura = foto.getWidth(this); | 
| 57 | 0 |          altura = foto.getHeight(this); | 
| 58 | ||
| 59 |          // no caso de uma imagem muito grande, utiliza de redução | |
| 60 |          // (por ex. usuário acabou de adicionar uma imagem ao album) | |
| 61 | 0 |          if (largura > 550 || altura > 550) { | 
| 62 | 0 |              Dimension d = calculaReducao(new Dimension(largura, altura), 550); | 
| 63 | 0 |              largura = d.width; | 
| 64 | 0 |              altura = d.height; | 
| 65 | } | |
| 66 | ||
| 67 | 0 |          this.setSize(largura + 10, altura + 60); | 
| 68 | 0 |          JButton btFechar = new JButton("fechar"); | 
| 69 | 0 |          cp.add(btFechar, BorderLayout.SOUTH); | 
| 70 | 0 |          btFechar.addActionListener(this); | 
| 71 | 0 |          this.setLocationRelativeTo(null); | 
| 72 | 0 |          show(); | 
| 73 | ||
| 74 | 0 |      } | 
| 75 | ||
| 76 |      /** | |
| 77 |       * Faz uma redução de dimensão da foto. | |
| 78 |       * Recebe um valor de dimensão e um tamanho máximo. | |
| 79 |       * Faz um cálculo baseado na divisão da largura pela altura para saber se a imagem | |
| 80 |       * está na vertical ou na horizontal, então calcula e retorna a nova dimensão. | |
| 81 |       * @param original Dimensão original. | |
| 82 |       * @param tamMaximo Tamanho máximo da nova dimensão. | |
| 83 |       * @return Retorna a nova dimensão. | |
| 84 |       */ | |
| 85 | public static Dimension calculaReducao(Dimension original, int tamMaximo) { | |
| 86 | ||
| 87 | 0 |          Dimension d = new Dimension(); | 
| 88 |          // proporção | |
| 89 | 0 |          double p = original.getWidth() / original.getHeight(); | 
| 90 | ||
| 91 |          // imagem horizontal | |
| 92 | 0 |          if (p > 1) { | 
| 93 | 0 |              d.width = tamMaximo; | 
| 94 | 0 |              d.height = (int) (tamMaximo / p); | 
| 95 | ||
| 96 |              // imagem vertical | |
| 97 |          } else { | |
| 98 | 0 |              d.width = (int) (tamMaximo * p); | 
| 99 | 0 |              d.height = tamMaximo; | 
| 100 | } | |
| 101 | 0 |          return d; | 
| 102 | } | |
| 103 | ||
| 104 |      /** | |
| 105 |       * Faz uma chamada a classe base e pinta todos os componentes. | |
| 106 |       * Esse método não é utilizado. | |
| 107 |       * TODO: avaliar a funcionalidade deste método. | |
| 108 |       * @param g Contexto gráfico. | |
| 109 |       */ | |
| 110 | @Override | |
| 111 | public void paint(Graphics g) { | |
| 112 | 0 |          super.paintComponents(g); | 
| 113 | 0 |          g.drawImage(foto, 5, 28, largura, altura, null); | 
| 114 | 0 |      } | 
| 115 | ||
| 116 |      /** | |
| 117 |       * Desabilita a tela. | |
| 118 |       * Desabilita sua visibilidade e encerra seus dados. | |
| 119 |       * Esse método não é utilizado. | |
| 120 |       * TODO: avaliar a funcionalidade deste método. | |
| 121 |       * @param e Evento de ação. | |
| 122 |       */ | |
| 123 | @Override | |
| 124 | public void actionPerformed(ActionEvent e) { | |
| 125 | 0 |          this.setVisible(false); | 
| 126 | 0 |          this.dispose(); | 
| 127 | 0 |      } | 
| 128 | ||
| 129 |      /** | |
| 130 |       * Método principal. | |
| 131 |       * Cria um objeto {@link java.awt.Dimension Dimension} e seta seus valores. | |
| 132 |       * Logo após utiliza o método calculaRedução da própria classe para reduzir a dimensão original. | |
| 133 |       * Ao final, imprime as duas dimensões. | |
| 134 |       * @param a args do método main. | |
| 135 |       */ | |
| 136 | public static void main(String a[]) { | |
| 137 | 0 |          Dimension dim = new Dimension(1154, 1772); | 
| 138 | 0 |          Dimension novo = calculaReducao(dim, 550); | 
| 139 | 0 |          System.out.println("original:" + dim.toString()); | 
| 140 | 0 |          System.out.println("reduzido:" + novo.toString()); | 
| 141 | 0 |      } | 
| 142 | } |