view 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
line wrap: on
line source
/*
XYPad - a haptic xy-pad that uses the jHapticGUI library

Copyright (C) 2015  Queen Mary University of London (http://depic.eecs.qmul.ac.uk/)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.depic.jhapticgui.examples;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;

import javax.swing.JPanel;


/**
 * 
 * A Panel displaying an xy cursor 
 * 
 * @author Fiore Martin
 *
 */
public class XYPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private static int CURSOR_SIZE = 20;
	private static int HALF_CURSOR_SIZE = CURSOR_SIZE/2;
	private static Stroke LINE_STROKE = new BasicStroke(2);
	private float cursorX;
	private float cursorY;
	
	/**
	 * Creates a new panel with the cursor placed at the centre
	 */
	public XYPanel() {
		cursorX = 0.5f;
		cursorY = 0.5f;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		final Graphics2D g2 = (Graphics2D)g;
		final Dimension size = getSize();
		
		/* cursorX and cursorY range from 0 to 1, so multiply them respectively * 
		 * by the width and height of this panel to get the drawing position    */
		int x = (int)(cursorX * size.width);
		int y = (int)(size.height - (cursorY * size.height));
		
		g.setColor(Color.ORANGE);
		
		/* draw the x and y lines */
		g2.setStroke(LINE_STROKE);
		g2.drawLine(0, y, size.width, y);
		g2.drawLine(x, 0, x, size.height);
		
		g2.setRenderingHint(
			    RenderingHints.KEY_ANTIALIASING,
			    RenderingHints.VALUE_ANTIALIAS_ON);
		
		/* draw the cursor */
		g2.fillOval(
				x - HALF_CURSOR_SIZE, 
				y - HALF_CURSOR_SIZE, 
				CURSOR_SIZE, 
				CURSOR_SIZE
			);
		
	}
	
	/**
	 * Sets the cursor position, the coordinates must be passed in normalized form and get scaled by the height and width 
	 * of the panel. so for example to place the cursor at the centre of the screen, the coordinates should be both 0.5.  
	 * 
	 * @param x the x coordinate in normalized form (ranging from 0 to 1) 
	 * @param y the y coordinate in normalized form (ranging from 0 to 1)
	 */
	public void setXY(float x, float y){
		cursorX = x;
		cursorY = y;
		
		repaint();
	}
}