f@0: /* f@0: XYPad - a haptic xy-pad that uses the jHapticGUI library f@0: f@0: Copyright (C) 2015 Queen Mary University of London (http://depic.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: package uk.ac.qmul.eecs.depic.jhapticgui.examples; f@0: f@0: import java.awt.BasicStroke; f@0: import java.awt.Color; f@0: import java.awt.Dimension; f@0: import java.awt.Graphics; f@0: import java.awt.Graphics2D; f@0: import java.awt.RenderingHints; f@0: import java.awt.Stroke; f@0: f@0: import javax.swing.JPanel; f@0: f@0: f@0: /** f@0: * f@0: * A Panel displaying an xy cursor f@0: * f@0: * @author Fiore Martin f@0: * f@0: */ f@0: public class XYPanel extends JPanel { f@0: private static final long serialVersionUID = 1L; f@0: private static int CURSOR_SIZE = 20; f@0: private static int HALF_CURSOR_SIZE = CURSOR_SIZE/2; f@0: private static Stroke LINE_STROKE = new BasicStroke(2); f@0: private float cursorX; f@0: private float cursorY; f@0: f@0: /** f@0: * Creates a new panel with the cursor placed at the centre f@0: */ f@0: public XYPanel() { f@0: cursorX = 0.5f; f@0: cursorY = 0.5f; f@0: } f@0: f@0: /** f@0: * {@inheritDoc} f@0: */ f@0: @Override f@0: public void paintComponent(Graphics g){ f@0: super.paintComponent(g); f@0: f@0: final Graphics2D g2 = (Graphics2D)g; f@0: final Dimension size = getSize(); f@0: f@0: /* cursorX and cursorY range from 0 to 1, so multiply them respectively * f@0: * by the width and height of this panel to get the drawing position */ f@0: int x = (int)(cursorX * size.width); f@0: int y = (int)(size.height - (cursorY * size.height)); f@0: f@0: g.setColor(Color.ORANGE); f@0: f@0: /* draw the x and y lines */ f@0: g2.setStroke(LINE_STROKE); f@0: g2.drawLine(0, y, size.width, y); f@0: g2.drawLine(x, 0, x, size.height); f@0: f@0: g2.setRenderingHint( f@0: RenderingHints.KEY_ANTIALIASING, f@0: RenderingHints.VALUE_ANTIALIAS_ON); f@0: f@0: /* draw the cursor */ f@0: g2.fillOval( f@0: x - HALF_CURSOR_SIZE, f@0: y - HALF_CURSOR_SIZE, f@0: CURSOR_SIZE, f@0: CURSOR_SIZE f@0: ); f@0: f@0: } f@0: f@0: /** f@0: * Sets the cursor position, the coordinates must be passed in normalized form and get scaled by the height and width f@0: * 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: * f@0: * @param x the x coordinate in normalized form (ranging from 0 to 1) f@0: * @param y the y coordinate in normalized form (ranging from 0 to 1) f@0: */ f@0: public void setXY(float x, float y){ f@0: cursorX = x; f@0: cursorY = y; f@0: f@0: repaint(); f@0: } f@0: }