annotate src/uk/ac/qmul/eecs/depic/jhapticgui/examples/XYPanel.java @ 1:46671fc7d649 tip

fixed "window" message bug and brought the message outside the haptic device monitor
author Fiore Martin <f.martin@qmul.ac.uk>
date Fri, 13 Mar 2015 13:02:16 +0000
parents 011caca7515a
children
rev   line source
f@0 1 /*
f@0 2 XYPad - a haptic xy-pad that uses the jHapticGUI library
f@0 3
f@0 4 Copyright (C) 2015 Queen Mary University of London (http://depic.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19 package uk.ac.qmul.eecs.depic.jhapticgui.examples;
f@0 20
f@0 21 import java.awt.BasicStroke;
f@0 22 import java.awt.Color;
f@0 23 import java.awt.Dimension;
f@0 24 import java.awt.Graphics;
f@0 25 import java.awt.Graphics2D;
f@0 26 import java.awt.RenderingHints;
f@0 27 import java.awt.Stroke;
f@0 28
f@0 29 import javax.swing.JPanel;
f@0 30
f@0 31
f@0 32 /**
f@0 33 *
f@0 34 * A Panel displaying an xy cursor
f@0 35 *
f@0 36 * @author Fiore Martin
f@0 37 *
f@0 38 */
f@0 39 public class XYPanel extends JPanel {
f@0 40 private static final long serialVersionUID = 1L;
f@0 41 private static int CURSOR_SIZE = 20;
f@0 42 private static int HALF_CURSOR_SIZE = CURSOR_SIZE/2;
f@0 43 private static Stroke LINE_STROKE = new BasicStroke(2);
f@0 44 private float cursorX;
f@0 45 private float cursorY;
f@0 46
f@0 47 /**
f@0 48 * Creates a new panel with the cursor placed at the centre
f@0 49 */
f@0 50 public XYPanel() {
f@0 51 cursorX = 0.5f;
f@0 52 cursorY = 0.5f;
f@0 53 }
f@0 54
f@0 55 /**
f@0 56 * {@inheritDoc}
f@0 57 */
f@0 58 @Override
f@0 59 public void paintComponent(Graphics g){
f@0 60 super.paintComponent(g);
f@0 61
f@0 62 final Graphics2D g2 = (Graphics2D)g;
f@0 63 final Dimension size = getSize();
f@0 64
f@0 65 /* cursorX and cursorY range from 0 to 1, so multiply them respectively *
f@0 66 * by the width and height of this panel to get the drawing position */
f@0 67 int x = (int)(cursorX * size.width);
f@0 68 int y = (int)(size.height - (cursorY * size.height));
f@0 69
f@0 70 g.setColor(Color.ORANGE);
f@0 71
f@0 72 /* draw the x and y lines */
f@0 73 g2.setStroke(LINE_STROKE);
f@0 74 g2.drawLine(0, y, size.width, y);
f@0 75 g2.drawLine(x, 0, x, size.height);
f@0 76
f@0 77 g2.setRenderingHint(
f@0 78 RenderingHints.KEY_ANTIALIASING,
f@0 79 RenderingHints.VALUE_ANTIALIAS_ON);
f@0 80
f@0 81 /* draw the cursor */
f@0 82 g2.fillOval(
f@0 83 x - HALF_CURSOR_SIZE,
f@0 84 y - HALF_CURSOR_SIZE,
f@0 85 CURSOR_SIZE,
f@0 86 CURSOR_SIZE
f@0 87 );
f@0 88
f@0 89 }
f@0 90
f@0 91 /**
f@0 92 * Sets the cursor position, the coordinates must be passed in normalized form and get scaled by the height and width
f@0 93 * of the panel. so for example to place the cursor at the centre of the screen, the coordinates should be both 0.5.
f@0 94 *
f@0 95 * @param x the x coordinate in normalized form (ranging from 0 to 1)
f@0 96 * @param y the y coordinate in normalized form (ranging from 0 to 1)
f@0 97 */
f@0 98 public void setXY(float x, float y){
f@0 99 cursorX = x;
f@0 100 cursorY = y;
f@0 101
f@0 102 repaint();
f@0 103 }
f@0 104 }