|
| 1 | +package Designer; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | +import java.awt.*; |
| 5 | +import java.awt.event.MouseEvent; |
| 6 | +import java.awt.event.MouseListener; |
| 7 | + |
| 8 | +/** |
| 9 | + * Created with IntelliJ IDEA. |
| 10 | + * User: thip |
| 11 | + * Date: 30/04/2013 |
| 12 | + * Time: 15:23 |
| 13 | + */ |
| 14 | +public class ColourPanel extends JPanel implements MouseListener |
| 15 | +{ |
| 16 | + |
| 17 | + private char selectedColour; |
| 18 | + private char backGroundColour; |
| 19 | + private int cellSize; |
| 20 | + private int rowCapacity; |
| 21 | + private char[] colours; |
| 22 | + |
| 23 | + private Editor editor; |
| 24 | + |
| 25 | + public ColourPanel ( Editor editor ) |
| 26 | + { |
| 27 | + |
| 28 | + this.editor = editor; |
| 29 | + |
| 30 | + selectedColour = 'n'; |
| 31 | + backGroundColour = 'w'; |
| 32 | + |
| 33 | + cellSize = 20; |
| 34 | + rowCapacity = 4; |
| 35 | + |
| 36 | + colours = new char[] |
| 37 | + { |
| 38 | + 'b', //Blue |
| 39 | + 'r', //red |
| 40 | + 'g', //green |
| 41 | + 'l', //Light Grey |
| 42 | + 'd', //Dark Grey |
| 43 | + 'w', //white |
| 44 | + 'n' //Black |
| 45 | + }; |
| 46 | + |
| 47 | + setPreferredSize ( new Dimension ( rowCapacity * cellSize + 8 , 0) ); |
| 48 | + this.addMouseListener ( this ); |
| 49 | + } |
| 50 | + |
| 51 | + public static Color getColor ( char colour ) |
| 52 | + { |
| 53 | + Color color; |
| 54 | + switch ( colour ) |
| 55 | + { |
| 56 | + |
| 57 | + case 'b': |
| 58 | + return Color.blue; |
| 59 | + case 'r': |
| 60 | + return Color.red; |
| 61 | + case 'g': |
| 62 | + return Color.green; |
| 63 | + case 'l': |
| 64 | + return Color.lightGray; |
| 65 | + case 'd': |
| 66 | + return Color.darkGray; |
| 67 | + case 'w': |
| 68 | + return Color.white; |
| 69 | + case 'n': |
| 70 | + return Color.black; |
| 71 | + |
| 72 | + default: |
| 73 | + return Color.magenta; //do this if color not recognised |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public char getSelectedColour () |
| 79 | + { |
| 80 | + return selectedColour; |
| 81 | + } |
| 82 | + |
| 83 | + public char getBackGroundColour () |
| 84 | + { |
| 85 | + return backGroundColour; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void paintComponent ( Graphics g ) |
| 90 | + { |
| 91 | + super.paintComponent ( g ); |
| 92 | + |
| 93 | + int row = 0; |
| 94 | + |
| 95 | + for ( int i = 0; i < colours.length; i++ ) |
| 96 | + { |
| 97 | + char colour = colours[i]; |
| 98 | + |
| 99 | + int place = i % rowCapacity; |
| 100 | + row = (int) Math.floor ( i / rowCapacity ); |
| 101 | + |
| 102 | + g.setColor ( getColor ( colour ) ); |
| 103 | + g.fillRect ( 4+ place * cellSize, 4+ row * cellSize, cellSize, cellSize ); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + g.setColor ( getColor ( backGroundColour ) ); |
| 108 | + g.fillRect ( 4, 4+ ( row + 1 ) * cellSize + 10, cellSize * rowCapacity, cellSize * rowCapacity ); |
| 109 | + |
| 110 | + g.setColor ( getColor ( selectedColour ) ); |
| 111 | + g.fillRect ( 4, 4+( row + 1 ) * cellSize + 10, cellSize * ( rowCapacity - 1 ), cellSize * ( rowCapacity - 1 ) ); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void mouseClicked ( MouseEvent e ) |
| 117 | + { |
| 118 | + int x = (int) Math.ceil ( (e.getX () -4) / cellSize ); |
| 119 | + int y = (int) Math.ceil ( (e.getY () -4) / cellSize ); |
| 120 | + |
| 121 | + int index = ( y * rowCapacity ) + x; |
| 122 | + |
| 123 | + |
| 124 | + if ( index < colours.length ) |
| 125 | + { |
| 126 | + char tempColour = colours[index]; |
| 127 | + |
| 128 | + if ( e.getButton () == MouseEvent.BUTTON1 ) selectedColour = tempColour; |
| 129 | + if ( e.getButton () == MouseEvent.BUTTON3 ) |
| 130 | + { |
| 131 | + backGroundColour = tempColour; |
| 132 | + editor.setBackground (); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + repaint (); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + @Override |
| 142 | + public void mousePressed ( MouseEvent e ) |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void mouseReleased ( MouseEvent e ) |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void mouseEntered ( MouseEvent e ) |
| 153 | + { |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void mouseExited ( MouseEvent e ) |
| 158 | + { |
| 159 | + } |
| 160 | +} |
0 commit comments