Re: Pick up and drop with image moving with the mouse
On 29/07/2008 11:29, Icarus allegedly wrote:
I am currently working on creating a game platform where one can pick
up tokens of various shapes and place them on a game board. For
precise placement, I need an image of these tokens in their original
size to be moving around with the mouse cursor until the token is
placed on the board.
My initial approach was to convert the token into an original-sized
picture and setting this picture as mouse cursor. However, my system
determines a maximum size for custom cursors which is far too small
for my needs.
Now I am at a loss. My idea now is to draw the picture on the
GlassPane of my current JFrame and move it according to
MouseMotionEvents.
Don't think using the GlassPane for this is the best idea. It should be
reserved for other uses, IMHO.
But that seems to be the most performance-heavy
solution there is.
Don't know about that.
Here's a way to do it. As you can see, I've used the ContentPane instead
of the GlassPane.
<code>
package scratch;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
/**
*
* @author da.futt
*/
public class DragPaneTest {
private static Icon createIcon() throws Exception {
return new ImageIcon(
ImageIO.read(
DragPaneTest.class.getResourceAsStream("withAMelon.png")
)
);
}
public static void main(String[] s){
EventQueue.invokeLater(new Runnable(){
public void run(){
try {
run0();
}
catch (Exception x){
x.printStackTrace();
}
}
private void run0() throws Exception {
JFrame f = new JFrame("DragPaneTest");
final DraggingPane cgp = new DraggingPane();
cgp.setIcon( createIcon() );
f.setContentPane(cgp);
ControllerInterface ci = new ControllerInterface(){
public void setIconLocation(Point p) {
cgp.setIconLocation(p);
}
public void setIconVisible(boolean b) {
cgp.setIconShowing(b);
}
public boolean getIconVisible() {
return cgp.getIconShowing();
}
};
MouseTracker mt = new MouseTracker(ci);
cgp.addMouseListener( mt );
cgp.addMouseMotionListener( mt );
f.setSize(new Dimension(500, 400));
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
private static interface ControllerInterface {
void setIconLocation(Point p);
void setIconVisible(boolean b);
boolean getIconVisible();
}
private static class MouseTracker
extends MouseAdapter
{
private final
ControllerInterface controller
;
private boolean
showing = false
;
public MouseTracker(ControllerInterface ci){
controller = ci;
}
@Override
public void mouseClicked(MouseEvent e) {
showing = ! showing;
controller.setIconVisible(showing);
}
@Override
public void mouseMoved(MouseEvent e) {
controller.setIconLocation(e.getPoint());
}
}
private static class DraggingPane
extends JPanel
{
private Icon
currentIcon
;
private Rectangle
iconBounds
;
private boolean
showIcon = false
;
public DraggingPane(){
setOpaque(true);
}
protected Rectangle getCurrentIconBounds(){
return iconBounds;
}
protected void setCurrentIconBounds(Rectangle r){
iconBounds = r;
}
public Icon getIcon(){
return currentIcon;
}
public boolean getIconShowing(){
return showIcon;
}
public void setIcon(Icon i){
if( i == null )
throw new NullPointerException();
currentIcon = i;
Rectangle r = getCurrentIconBounds();
if( getIcon() != null && getIconShowing() ){
RepaintManager.currentManager(this).addDirtyRegion(this, r.x, r.y,
r.width, r.height);
}
Point p = r == null ?
new Point( getWidth() >> 1, getHeight() >> 1 )
: r.getLocation();
r = new Rectangle( p, new Dimension(i.getIconWidth(),
i.getIconHeight()) );
setCurrentIconBounds( r );
if( getIconShowing() ){
RepaintManager.currentManager(this).addDirtyRegion(this, r.x, r.y,
r.width, r.height);
}
}
public void setIconShowing(boolean b){
if( ! (b ^ showIcon) ){
return ;
}
showIcon = b;
if( getIcon() != null ){
Rectangle r = getCurrentIconBounds();
RepaintManager.currentManager(this).addDirtyRegion(this, r.x, r.y,
r.width, r.height);
}
}
public void setIconLocation(Point p){
if( p.equals( getCurrentIconBounds().getLocation() ) ){
return ;
}
Rectangle r = getCurrentIconBounds();
if( getIconShowing() ){
RepaintManager.currentManager(this).addDirtyRegion(this, r.x, r.y,
r.width, r.height);
}
r.setLocation(p);
if( getIconShowing() ){
RepaintManager.currentManager(this).addDirtyRegion(this, r.x, r.y,
r.width, r.height);
}
}
@Override
public void paint(Graphics g){
super.paint(g);
if( getIconShowing() && getIcon() != null ){
Point p = getCurrentIconBounds().getLocation();
getIcon().paintIcon(this, g, p.x, p.y);
}
}
}
}
</code>
--
DF.