1 19 package org.lucane.applications.whiteboard.gui; 20 21 import java.awt.Color ; 22 import java.awt.Graphics ; 23 import java.awt.event.MouseEvent ; 24 import java.awt.event.MouseListener ; 25 26 import javax.swing.JButton ; 27 28 import org.lucane.applications.whiteboard.WhiteBoard; 29 30 public class LinePatternButton extends JButton 31 implements MouseListener 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 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 e) {} 87 public void mouseExited(MouseEvent e) {} 88 public void mouseReleased(MouseEvent e) {} 89 public void mousePressed(MouseEvent e) {} 90 public void mouseClicked(MouseEvent 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 |