package gui; import java.awt.*; import java.awt.geom.*; import javax.swing.*; import gui.elements.*; import java.awt.event.*; /** *

Title: Java Media for Chapter 4 of Experiencing Sensation and Perception

*

Description: draws four arrows that can be clicked * as a response, * it uses the arrow keys as backup

*

Copyright: Copyright (c) 2002

*

Company: Hanover College

* @author John H. Krantz, Ph.D. * @version 0.1 */ public class DirectionResponses extends JPanel implements KeyListener, MouseMotionListener, MouseListener { static final long serialVersionUID = 0; // the fours arrows Arrow up = new Arrow(); Arrow left = new Arrow(); Arrow down = new Arrow(); Arrow right = new Arrow(); // arrow areas Rectangle2D upRect = new Rectangle2D.Double(20,0,10,20); Rectangle2D leftRect = new Rectangle2D.Double(0,20,20,10); Rectangle2D downRect = new Rectangle2D.Double(20,30,10,20); Rectangle2D rightRect = new Rectangle2D.Double(30,20,20,10); // response variables public final static int NONE = -1; public final static int UP = 0; public final static int LEFT = 1; public final static int DOWN = 2; public final static int RIGHT = 3; private int pressed = -1; // arrow states final int NORM = 1; final int OVER = 2; final int CLICKED = 3; int upState = NORM; int leftState = NORM; int downState = NORM; int rightState = NORM; int size = 55; public DirectionResponses() { setSize(size,size); setPreferredSize(new Dimension(size,size)); defineArrows(); } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; g2d.setPaint(Color.LIGHT_GRAY); g2d.fill(new Rectangle(0,0,size,size)); // draw the arrows g2d.translate(25,25); drawArrow(g2d,up,upState); drawArrow(g2d,down,downState); g2d.rotate(-Math.PI/2.0); drawArrow(g2d,left,leftState); drawArrow(g2d,right,rightState); } private void drawArrow(Graphics2D g2d, Arrow a, int state){ Color drawColor = Color.red; float drawWidth = 2.0f; if (state == NORM) { drawColor = Color.red; drawWidth = 2.0f; } if (state == OVER) { drawColor = Color.green; drawWidth = 4.0f; } if (state == CLICKED) { drawColor = Color.blue; drawWidth = 4.0f; } g2d.setPaint(drawColor); g2d.setStroke(new BasicStroke(drawWidth)); g2d.draw(a.formArrow()); } private void defineArrows(){ up.setDirection(up.DRAWUP); up.setStart(0,-5); up.setHeight(20); up.setHeadLength(5); down.setDirection(up.DRAWDOWN); down.setStart(0,5); down.setHeight(20); down.setHeadLength(5); left.setDirection(up.DRAWUP); left.setStart(0,-5); left.setHeight(20); left.setHeadLength(5); right.setDirection(up.DRAWDOWN); right.setStart(0,5); right.setHeight(20); right.setHeadLength(5); } public void mouseClicked(MouseEvent e) { //JOptionPane.showMessageDialog(null, "here"); Point loc = e.getPoint(); //where pressed if (upRect.contains(loc.x,loc.y)){ pressed = UP; upState = CLICKED; downState = NORM; leftState=NORM; rightState=NORM; } else if (leftRect.contains(loc.x,loc.y)){ pressed = LEFT; leftState = CLICKED; downState = NORM; upState=NORM; rightState=NORM; } else if (downRect.contains(loc.x,loc.y)){ pressed = DOWN; downState = CLICKED; upState = NORM; leftState=NORM; rightState=NORM; } else if (rightRect.contains(loc.x,loc.y)){ pressed = RIGHT; rightState = CLICKED; downState = NORM; leftState=NORM; upState=NORM; } else{ pressed = NONE; upState = NORM; downState = NORM; leftState=NORM; rightState=NORM; } repaint(); } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e){ upState = NORM; downState = NORM; leftState=NORM; rightState=NORM; repaint(); } public void mouseMoved(MouseEvent e){ Point loc = e.getPoint(); //where pressed if (upRect.contains(loc.x,loc.y)){ upState = OVER; downState = NORM; leftState=NORM; rightState=NORM; } else if (leftRect.contains(loc.x,loc.y)){ leftState = OVER; downState = NORM; upState=NORM; rightState=NORM; } else if (downRect.contains(loc.x,loc.y)){ downState = OVER; upState = NORM; leftState=NORM; rightState=NORM; } else if (rightRect.contains(loc.x,loc.y)){ rightState = OVER; downState = NORM; leftState=NORM; upState=NORM; } else{ upState = NORM; downState = NORM; leftState=NORM; rightState=NORM; } repaint(); } public void mouseDragged(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){ upState = NORM; downState = NORM; leftState=NORM; rightState=NORM; repaint(); } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP){ pressed = UP; rightState = NORM; downState = NORM; leftState=NORM; upState=CLICKED; } else if (key == KeyEvent.VK_LEFT){ pressed = LEFT; rightState = NORM; downState = NORM; leftState=CLICKED; upState=NORM; } else if (key == KeyEvent.VK_DOWN){ pressed = DOWN; rightState = NORM; downState = CLICKED; leftState=NORM; upState=NORM; } else if (key == KeyEvent.VK_RIGHT){ pressed = RIGHT; rightState = CLICKED; downState = NORM; leftState=NORM; upState=NORM; } else{ pressed = NONE; rightState = NORM; downState = NORM; leftState=NORM; upState=NORM; } repaint(); } public void keyReleased(KeyEvent e){ rightState = NORM; downState = NORM; leftState=NORM; upState=NORM; repaint(); } public void keyTyped(KeyEvent e){ } // get methods public int getDirection() { int temp = pressed; pressed = NONE; return temp; } }