// Scott DeRuiter 3/10/2003 // Paint.java // Paint some figures using MouseListener and MouseMotionListener // 3/13/2013 Greenstein: changed applet to JFrame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Paint { private JFrame frame; private DrawingArea canvas; public static void main(String[] args) { Paint p = new Paint(); p.Run(); } public void Run() { // Create a frame to hold everything frame = new JFrame ("Paint"); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setBackground(Color.black); // only needed if frame is larger than panel // Define panel to draw on canvas = new DrawingArea(); // create a panel to draw on canvas.setBackground(Color.lightGray); frame.addMouseListener(canvas); // connects the MouseListerner to the panel window // Put frame together frame.getContentPane().add(canvas); // puts panel on frame frame.setVisible(true); } } class DrawingArea extends JPanel implements MouseListener, MouseMotionListener { private int x, y; private int interiorcolor, exteriorcolor, shape, size; private boolean initialdraw, freetodraw; public DrawingArea ( ) { x = y = interiorcolor = exteriorcolor = shape = size = 0; initialdraw = freetodraw = true; setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { if ( initialdraw == true ) { super.paintComponent(g); initialdraw = false; } if ( freetodraw ) DrawFigure ( g, x, y, shape ); DrawPanels ( g ); DrawHighlights ( g ); } public void DrawPanels ( Graphics g ) { g.setColor ( Color.white ); g.fillRect ( 0, 0, 60, 720 ); g.fillRect ( 660, 0, 60, 720 ); g.setColor ( Color.gray ); g.fillRect ( 0, 0, 60, 60 ); g.setColor ( Color.green ); g.fillRect ( 0, 60, 60, 60 ); g.setColor ( Color.blue ); g.fillRect ( 0, 120, 60, 60 ); g.setColor ( Color.cyan ); g.fillRect ( 0, 180, 60, 60 ); g.setColor ( Color.magenta ); g.fillRect ( 0, 240, 60, 60 ); g.setColor ( Color.yellow ); g.fillRect ( 660, 0, 60, 60 ); g.setColor ( Color.black ); g.fillRect ( 660, 60, 60, 60 ); g.setColor ( Color.darkGray ); g.fillRect ( 660, 120, 60, 60 ); g.setColor ( Color.lightGray ); g.fillRect ( 660, 180, 60, 60 ); g.setColor ( Color.black ); g.drawString ( "LARGE", 11, 335 ); g.drawString ( "MEDIUM", 8, 395 ); g.drawString ( "SMALL", 12, 455 ); DrawFigure ( g, 690, 270, 0 ); DrawFigure ( g, 690, 330, 1 ); DrawFigure ( g, 690, 390, 2 ); DrawFigure ( g, 690, 450, 3 ); g.drawRect ( 0, 0, 720, 480 ); for ( int i = 0; i < 8; i++ ) { g.drawRect ( 0, i * 60, 60, 60 ); g.drawRect ( 660, i * 60, 60, 60 ); } } public void DrawHighlights ( Graphics g ) { g.setColor ( Color.red ); g.drawRect ( 0, interiorcolor * 60 + 0, 60 - 0, 60 - 0 ); g.drawRect ( 1, interiorcolor * 60 + 1, 60 - 2, 60 - 2 ); g.drawRect ( 2, interiorcolor * 60 + 2, 60 - 4, 60 - 4 ); g.drawRect ( 660, exteriorcolor * 60 + 0, 60 - 0, 60 - 0 ); g.drawRect ( 661, exteriorcolor * 60 + 1, 60 - 2, 60 - 2 ); g.drawRect ( 662, exteriorcolor * 60 + 2, 60 - 4, 60 - 4 ); g.drawRect ( 0, size * 60 + 300, 60 - 0, 60 - 0 ); g.drawRect ( 1, size * 60 + 301, 60 - 2, 60 - 2 ); g.drawRect ( 2, size * 60 + 302, 60 - 4, 60 - 4 ); g.drawRect ( 660, shape * 60 + 240, 60 - 0, 60 - 0 ); g.drawRect ( 661, shape * 60 + 241, 60 - 2, 60 - 2 ); g.drawRect ( 662, shape * 60 + 242, 60 - 4, 60 - 4 ); } public void DrawFigure ( Graphics g, int x, int y, int shape ) { int sizer = 0; switch ( size ) { case 0: sizer = 20; break; case 1: sizer = 12; break; case 2: sizer = 6; break; } switch ( interiorcolor ) { case 0: g.setColor ( Color.gray ); break; case 1: g.setColor ( Color.green ); break; case 2: g.setColor ( Color.blue ); break; case 3: g.setColor ( Color.cyan ); break; case 4: g.setColor ( Color.magenta ); break; } switch ( shape ) { case 0: g.fillOval ( x - sizer, y - sizer, 2 * sizer, 2 * sizer ); break; case 1: g.fillOval ( x - sizer, y - sizer / 2, 2 * sizer, sizer ); break; case 2: g.fillRect ( x - sizer, y - sizer, 2 * sizer, 2 * sizer ); break; case 3: g.fillRect ( x - sizer, y - sizer / 2, 2 * sizer, sizer ); break; } switch ( exteriorcolor ) { case 0: g.setColor ( Color.yellow ); break; case 1: g.setColor ( Color.black ); break; case 2: g.setColor ( Color.darkGray ); break; case 3: g.setColor ( Color.lightGray ); break; } switch ( shape ) { case 0: g.drawOval ( x - sizer, y - sizer, 2 * sizer, 2 * sizer ); break; case 1: g.drawOval ( x - sizer, y - sizer / 2, 2 * sizer, sizer ); break; case 2: g.drawRect ( x - sizer, y - sizer, 2 * sizer, 2 * sizer ); break; case 3: g.drawRect ( x - sizer, y - sizer / 2, 2 * sizer, sizer ); break; } g.setColor ( Color.black ); } public void mousePressed(MouseEvent evt) { x = evt.getX ( ); y = evt.getY ( ); if ( evt.isShiftDown() ) { initialdraw = true; freetodraw = false; repaint(); return; } int ychoice = y / 60; if ( x <= 60 ) { freetodraw = false; switch ( ychoice ) { case 0: interiorcolor = 0; break; case 1: interiorcolor = 1; break; case 2: interiorcolor = 2; break; case 3: interiorcolor = 3; break; case 4: interiorcolor = 4; break; case 5: size = 0; break; case 6: size = 1; break; case 7: size = 2; break; } } else if ( x >= 660 ) { freetodraw = false; switch ( ychoice ) { case 0: exteriorcolor = 0; break; case 1: exteriorcolor = 1; break; case 2: exteriorcolor = 2; break; case 3: exteriorcolor = 3; break; case 4: shape = 0; break; case 5: shape = 1; break; case 6: shape = 2; break; case 7: shape = 3; break; } } repaint ( ); } public void mouseReleased(MouseEvent evt) { freetodraw = true; } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { x = evt.getX ( ); y = evt.getY ( ); if ( freetodraw ) repaint ( ); } }