Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java AWT Code Examples

Paint Shapes and Text of different colors and fonts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
Paint Shapes and Text of different colors and fonts import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Painter2 extends JFrame { private int topX, topY, width, fontSize, height, bottomX, bottomY, shape; private boolean clear, textOn, filled; private Color drawingColor; private String font; private JTextField text; private JPanel panel1, panel2, panel3, panel4; private JRadioButton ovalBox, rectBox, lineBox; private ButtonGroup shapeGroup; private JCheckBox fillBox; private JComboBox colorList, fontList, sizeList; private JButton clearButton; private String colorNames[] = { "Black", "Green", "Blue", "Red", "Cyan" }; private Color colors[] = { Color.black, Color.green, Color.blue, Color.red, Color.cyan }; private String fontNames[] = { "Serif", "SansSerif", "Monospaced" }; private String sizeNames[] = { "9", "10", "22", "72" }; private int sizes[] = { 9, 10, 22, 72 }; private final int OVAL = 1, LINE = 2, RECT = 3; private ToolWindow tools; // Painter2 constructor public Painter2() { super( "Painting Window" ); addMouseListener( new MouseHandler() ); // set defaults for painting drawingColor = Color.black; shape = OVAL; font = "Serif"; fontSize = 9; setDefaultCloseOperation( EXIT_ON_CLOSE ); setBackground( Color.white ); setSize( 300, 300 ); show(); // create new ToolWindow tools = new ToolWindow(); } // end constructor // paint the new window. super is not called so // that the previous images will not be erased. public void paint( Graphics g ) { g.setColor( drawingColor ); // draw text if ( textOn ) { g.setFont( new Font( font, Font.PLAIN, fontSize ) ); g.drawString( text.getText(), topX, topY ); textOn = false; return; } // end if statement // set shape's top left coordinates if ( shape != LINE ) { topX = Math.min( topX, bottomX ); topY = Math.min( topY, bottomY ); } // end if statement // draw filled shape if ( filled && shape != LINE ) switch ( shape ) { case OVAL: g.fillOval( topX, topY, width, height ); break; case RECT: g.fillRect( topX, topY, width, height ); break; } // end switch statement // draw unfilled shapes else switch ( shape ) { case OVAL: g.drawOval( topX, topY, width, height ); break; case LINE: g.drawLine( topX, topY, bottomX, bottomY ); break; case RECT: g.drawRect( topX, topY, width, height ); break; } // ens switch statement // clear background if ( clear == true ) { g.setColor( Color.white ); g.fillRect( 0, 0, getSize().width, getSize().height ); clear = false; } // end if statement } // end method paint // inner class for window containing GUI private class ToolWindow extends JFrame { // ToolWindow constructor public ToolWindow() { super( "Tool Window" ); // set up to edit text text = new JTextField( "Text", 25 ); text.addActionListener( // anonymous inner class to handle text drawing new ActionListener () { public void actionPerformed( ActionEvent event ) { textOn = true; repaint(); } // end actionPerformed method } // end anonymous inner class ); // end call to addActionListener // set up to choose font fontList = new JComboBox( fontNames ); fontList.setMaximumRowCount( 3 ); fontList.addItemListener( // anonymous inner class to select font new ItemListener() { // change font public void itemStateChanged( ItemEvent event ) { font = fontNames[ fontList.getSelectedIndex() ]; } // end itemStateChanged method } // end anonymous inner class ); // end call to addItemListener // set up to choose font size sizeList = new JComboBox( sizeNames ); sizeList.setMaximumRowCount( 3 ); sizeList.addItemListener( // anonymous inner class to select font size new ItemListener() { // change font size public void itemStateChanged( ItemEvent event ) { fontSize = sizes[ sizeList.getSelectedIndex() ]; } // end itemStateChanged method } // end anonymous inner class ); // end call to addItemListener // set up to choose color colorList = new JComboBox( colorNames ); colorList.setMaximumRowCount( 3 ); colorList.addItemListener( // anonymous inner class to select color new ItemListener() { // change color public void itemStateChanged( ItemEvent event ) { drawingColor = colors[ colorList.getSelectedIndex() ]; } // end itemStateChanged method } // end anonymous inner class ); // end call to addItemListener // set up clear button clearButton = new JButton( "Clear" ); clearButton.addActionListener( new ClearButtonHandler() ); // set up to choose filled fillBox = new JCheckBox( "Filled" ); FillBoxHandler fillHandle = new FillBoxHandler(); fillBox.addItemListener( fillHandle ); // set up to choose shapes ovalBox = new JRadioButton( "Oval", true ); lineBox = new JRadioButton( "Line", false ); rectBox = new JRadioButton( "Rectangle", false ); RadioButtonHandler handler = new RadioButtonHandler(); ovalBox.addItemListener( handler ); lineBox.addItemListener( handler ); rectBox.addItemListener( handler ); shapeGroup = new ButtonGroup(); shapeGroup.add(ovalBox); shapeGroup.add(lineBox); shapeGroup.add(rectBox); // set up GUI layout panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); panel1.setLayout( new GridLayout( 1, 4 ) ); panel2.setLayout( new GridLayout( 1, 3 ) ); panel3.setLayout( new FlowLayout() ); panel4.setLayout( new FlowLayout() ); panel1.add( ovalBox ); panel1.add( lineBox ); panel1.add( rectBox ); panel1.add( fillBox ); panel2.add( new JScrollPane( colorList ) ); panel2.add( new JScrollPane( fontList ) ); panel2.add( new JScrollPane( sizeList ) ); panel3.add( text ); panel4.add( clearButton ); Container container = getContentPane(); container.setLayout( new FlowLayout() ); container.add( panel1 ); container.add( panel2 ); container.add( panel3 ); container.add( panel4 ); setDefaultCloseOperation( EXIT_ON_CLOSE ); setSize( 350, 175 ); setLocation( 300, 0 ); setVisible( true ); } // end ToolWindow constructor } // end inner class ToolWindow // set coordinate and dimension values private class MouseHandler extends MouseAdapter { public void mousePressed( MouseEvent event ) { topX = event.getX(); topY = event.getY(); } // end mousePressed method public void mouseReleased( MouseEvent event ) { bottomX = event.getX(); bottomY = event.getY(); width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); repaint(); } // end mouseReleased method } // end inner class MouseHandler // clear background private class ClearButtonHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { clear = true; repaint(); } // end actionPerformed method } // end inner class ClearButtonHandler // determine which type of shape to draw private class RadioButtonHandler implements ItemListener { public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == ovalBox ) shape = OVAL; else if ( event.getSource() == lineBox ) shape = LINE; else if ( event.getSource() == rectBox ) shape = RECT; } // end itemStateChanged method } // end inner class RadioButtonHandler // determine if shape should be filled private class FillBoxHandler implements ItemListener { public void itemStateChanged( ItemEvent event ) { if ( event.getStateChange() == ItemEvent.SELECTED ) filled = true; else filled = false; } // end ItemStateChanged method } // end inner class FillBoxHandler // execute application public static void main( String args[] ) { Painter2 application = new Painter2(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end main method } // end class Painter2