wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes GUI Issues.  Ghosting when moving JLabel via Mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI Issues.  Ghosting when moving JLabel via Mouse" Watch "GUI Issues.  Ghosting when moving JLabel via Mouse" New topic
Author

GUI Issues. Ghosting when moving JLabel via Mouse

Michael Re
Greenhorn

Joined: Sep 17, 2004
Posts: 2
Hey guys. This is my first post so forgive me!!!

I am trying to create a basic GUI that will allow me to add "PC's" (Just a JLabel with an image icon of a PC at the moment) to a JFrames layeredPane and then just to be able to move its location around using the mouse via dragging. At the moment when I drag my PC, it will ghost the image and the PC will not follow the movements of the mouse x,y co-ords.

Here is my code ...

import javax.swing.*;
import java.awt.*;
import javax.accessibility.*;
import java.awt.event.*;


public class GUI extends JFrame
{
private JLayeredPane layeredPane = new JLayeredPane();
private JPanel pContentPane = (JPanel)getContentPane();
private static int screenHeight;
private static int screenWidth;

public GUI()
{
// Cheat the taskbar problem by getting the current screen height/width.
screenHeight = (Toolkit.getDefaultToolkit().getScreenSize().height) - 30;
screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
pContentPane.setLayout(new FlowLayout());
pContentPane.add(layeredPane);
layeredPane.setPreferredSize(new Dimension(screenWidth,screenHeight));

this.setResizable(false);

this.setTitle("GUI PROTOTYPE TEST");
this.setSize(screenWidth, screenHeight);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultLookAndFeelDecorated(true);
addPC("HIYA");
}

public static void main(String args[])
{
GUI myGUI = new GUI();
}

public void addPC(String inName)
{
PC myPC = new PC(inName);
myPC.setBounds(100,100,100,100);
layeredPane.add(myPC);

}
}


import javax.swing.*;

// Base class that PC's, Routers, HUB's etc will be based on!
public class Node extends JLabel
{
private String mName;
public Node(String inName)
{
mName = inName;

}

public String getNodeName()
{
return mName;
}

}



import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

public class PC extends Node implements MouseListener, MouseMotionListener
{
Image pcImage;
ImageIcon pcIcon = new ImageIcon("Gopher.gif");

public PC(String inName)
{
super(inName);
this.setIcon(pcIcon);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setBounds(50,50,30,30);

}

public int getIconHeight()
{
int pcIconHeight = pcIcon.getIconHeight();
return pcIconHeight;
}

public int getIconWidth()
{
int pcIconWidth = pcIcon.getIconHeight();
return pcIconWidth;
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
//System.out.println(this.getNodeName()+ " pressed");
}
public void mouseDragged(MouseEvent e)
{
//System.out.println(x + ", " + y);
this.setLocation(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e)
{
//System.out.println("Hi, i am" + this.getNodeName());
}


}

Sorry about the length of the post but can anyone suggest any way to stop the ghosting issue?

Thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this.setLocation(this.getX()+e.getX(),this.getY()+e.getY());
Michael Re
Greenhorn

Joined: Sep 17, 2004
Posts: 2
Thanks alot, works like a charm!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
GeeCON Prague 2014
 
subject: GUI Issues. Ghosting when moving JLabel via Mouse