KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > gui > LinePatternButton


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.gui;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.awt.event.MouseListener JavaDoc;
25
26 import javax.swing.JButton JavaDoc;
27
28 import org.lucane.applications.whiteboard.WhiteBoard;
29
30 public class LinePatternButton extends JButton JavaDoc
31 implements MouseListener JavaDoc
32 {
33     private static float[][] PATTERNS =
34         new float[][] {
35             new float[] { 1, 0 },
36             new float[] { 1, 1 },
37             new float[] { 4, 2 },
38             new float[] { 4, 4, 8, 4 }
39     };
40
41     private static float[][] DISPLAY_PATTERNS =
42         new float[][] {
43             new float[] { 1, 0 },
44             new float[] { 2, 2 },
45             new float[] { 8, 4 },
46             new float[] { 4, 4, 8, 4 }
47     };
48     
49     private WhiteBoard plugin;
50     private int patternIndex = 0;
51     
52     public LinePatternButton(WhiteBoard plugin)
53     {
54         super(plugin.getImageIcon("blank.png"));
55         setToolTipText(plugin.tr("tip.linePattern"));
56         setFocusable(false);
57         addMouseListener(this);
58         
59         this.plugin = plugin;
60     }
61     
62     protected void paintChildren(Graphics JavaDoc g)
63     {
64         g.setColor(Color.BLACK);
65         float[] pattern = DISPLAY_PATTERNS[patternIndex];
66         
67         int xStart = 4;
68         int xEnd = g.getClipBounds().width-6;
69         int y = g.getClipBounds().height/2-1;
70         
71         int i=0;
72         for(int x=xStart;x<xEnd;)
73         {
74             int x2 = (int)(x+pattern[i]);
75             if(x2 >= xEnd)
76                 x2 = xEnd;
77             
78             g.drawLine(x, y, x2, y);
79             x += pattern[i];
80             i = (i+1) % pattern.length;
81             x += pattern[i];
82             i = (i+1) % pattern.length;
83         }
84     }
85
86     public void mouseEntered(MouseEvent JavaDoc e) {}
87     public void mouseExited(MouseEvent JavaDoc e) {}
88     public void mouseReleased(MouseEvent JavaDoc e) {}
89     public void mousePressed(MouseEvent JavaDoc e) {}
90     public void mouseClicked(MouseEvent JavaDoc e)
91     {
92         if(!isEnabled())
93             return;
94         
95         if(e.getButton() == MouseEvent.BUTTON3)
96             this.patternIndex = 0;
97         else
98             this.patternIndex = (this.patternIndex + 1) % PATTERNS.length;
99         
100         plugin.getGraph().setDashPattern(PATTERNS[this.patternIndex]);
101         repaint();
102     }
103     
104 }
Popular Tags