Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI Issues. Ghosting when moving JLabel via Mouse

 
Michael Re
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this.setLocation(this.getX()+e.getX(),this.getY()+e.getY());
 
Michael Re
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot, works like a charm!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic