View Javadoc

1   // START LICENSE
2   // JSet - a Java JSet card board game implementation
3   // Copyright (C) 2004 Jerome Lacoste
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or (at
8   // your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful, but
11  // WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  // END LICENSE
19  
20  package org.cb.jset.client.ui;
21  
22  import javax.swing.JPanel;
23  import javax.swing.BorderFactory;
24  import javax.swing.JComponent;
25  import javax.swing.JWindow;
26  import java.awt.FlowLayout;
27  import java.awt.Color;
28  import java.awt.Image;
29  
30  import org.cb.jset.CardProperties;
31  import org.cb.jset.JSetCard;
32  
33  /***
34   * A small class to help build a logo.
35   *
36   * Would be nice to have a way to close it...
37   * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
38   * @version $Id: JSetClientUI.java 85 2004-04-13 14:29:08Z jerome $
39   */
40  public class JSetLogo extends JWindow
41  {
42    private final JPanel _logoPanel = new JPanel();
43  
44    /***
45     * Constructs a JSetLogo instance.
46     */
47    public JSetLogo()
48    {
49      init();
50    }
51  
52    /*
53    class JLogoName extends JComponent
54    {
55      private final AffineTransform at = new AffineTransform();
56      private TextLayout _textTl;
57      private final String _logoName = "JSet";
58  
59      public JLogoName()
60      {
61        init();
62      }
63  
64      void init()
65      {
66        _textTl = new TextLayout(_logoName,
67                                 new Font("Helvetica", 3, 16),
68                                 new FontRenderContext(null, false, false));
69        AffineTransform textAt = new AffineTransform();
70        textAt.translate(0, (float)_textTl.getBounds().getHeight());
71      }
72  
73      protected void paintComponent(final Graphics g)
74      {
75        super.paintComponent(g);
76  
77        Graphics2D g2 = (Graphics2D) g;
78        Dimension d = getSize();
79        int w = d.width;
80        int h = d.height;
81  
82        float width = (float) _textTl.getBounds().getWidth();
83        float height = (float) _textTl.getBounds().getHeight();
84        final float v = w/2-width/2;
85        AffineTransform at = new AffineTransform();
86        at.rotate(Math.toRadians(-6));
87        g2.transform(at);
88  
89        g2.setPaint(new GradientPaint(0, 0, Color.darkGray, w-50, h, Color.orange, false));
90        _textTl.draw(g2, v, 15);
91      }
92    }
93    */
94  
95    /***
96     * initialize the contents of the JWindow representing the logo.
97     */
98    private void init()
99    {
100 
101     this.getContentPane().add(_logoPanel);
102     _logoPanel.setLayout(null);
103     this.pack();
104     this.setBounds(50, 50, 82, 108);
105 
106     JPanel lPanel;
107     final int distanceFromCenter = 18;
108     final int centerX = 20;
109     final int centerY = 20;
110 
111     // unused for now...
112 //    {
113 //      JLogoName logoName = new JLogoName();
114 //      logoName.setSize(50, 30);
115 //      logoName.setBounds(37, centerY + 3 * distanceFromCenter,
116 //                        logoName.getWidth(), logoName.getHeight());
117 //      logoName.setPreferredSize(logoName.getPreferredSize());
118 //
119 //      _logoPanel.add(logoName);
120 //    }
121     {
122       final org.cb.jset.JSetCard card3 =
123           new JSetCard(new CardProperties((byte) 3, (byte) 3, (byte) 1, (byte) 3));
124       final int x = centerX;
125       final int y = centerY + distanceFromCenter;
126       lPanel = createCardPanel(card3, x, y);
127       _logoPanel.add(lPanel);
128     }
129     {
130       final org.cb.jset.JSetCard card1 =
131           new JSetCard(new CardProperties((byte) 1, (byte) 1, (byte) 1, (byte) 1));
132       final int x = (int) (centerX - distanceFromCenter * Math.sqrt(3) / 2);
133       final int y = (int) (centerY - distanceFromCenter / 2);
134       lPanel = createCardPanel(card1, x, y);
135       _logoPanel.add(lPanel);
136     }
137     {
138       final org.cb.jset.JSetCard card2 =
139           new JSetCard(new CardProperties((byte) 2, (byte) 2, (byte) 1, (byte) 2));
140       final int x = (int) (centerX + distanceFromCenter * Math.sqrt(3) / 2);
141       final int y = (int) (centerY - distanceFromCenter);
142       lPanel = createCardPanel(card2, x, y);
143       _logoPanel.add(lPanel);
144     }
145   }
146 
147   /***
148    * Saves the logo in a file.
149    * Note: not implemented right now
150    *
151    * @param filePath the path where the image will be contained
152    * @todo implement the save image functionality. We probably need to specify image type (e.g. png)
153    */
154   void saveLogo(final String filePath)
155   {
156     Image image = this.createImage(this.getWidth(), this.getHeight());
157     // FIXME would be nice to be able to save this image somwehere. Find a way...
158   }
159 
160   /***
161    * Helper method to create a small panel containing the specified card, located
162    * at the specified coordinates.
163    * @param card1
164    * @param x
165    * @param y
166    * @return
167    */
168   private JPanel createCardPanel(final JSetCard card1, final int x, final int y)
169   {
170     JComponent lCard;
171     JPanel lPanel;
172     lCard = new JSetCardComponent(card1).getShape(0);
173     lCard.setSize(32, 52);
174     lCard.setPreferredSize(lCard.getSize());
175     lPanel = new JPanel();
176     lPanel.setLayout(new FlowLayout());
177     lPanel.setSize(42, 64);
178     lPanel.setBackground(Color.white);
179     lPanel.setPreferredSize(lPanel.getSize());
180     lPanel.add(lCard);
181     lPanel.setBounds(x, y, lPanel.getWidth(), lPanel.getHeight());
182     lPanel.setBorder(BorderFactory.createLineBorder(Color.black));
183     return lPanel;
184   }
185 
186   /***
187    * Start and show the logo.
188    * @param args the arguments
189    */
190   public static void main(final String[] args)
191   {
192     final JSetLogo logo = new JSetLogo();
193     logo.show();
194   }
195 }