Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

GUI Issues. Ghosting when moving JLabel via Mouse

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot, works like a charm!
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic