babashri
asked on
CURSOR DOESN'T CHANGE !!!
Hello friends,
I have deleted the previous question having the same problem , but the code seemed to be very long so I am presenting a smaller version of the code here
the following program produces a frame containing three panels in a row.The left and the right panel contains a slot while the middle panel is left empty.
when the mouse is dragged in a slot , the slot display message "Dragged"; for clicking it displays "Clicked me" and for releasing , it displays "Released me".
the problem lies here :
When the mouse-drag originates from the middle panel(REMEMBER THAT THE MIDDLE PANNEL IS EMPTY) and continued in the left/right panels (WHICH CONTAINS THE SLOTS), the slots doesn't give the appropriate messages , BUT if the mouse-drag is originated from any of the slots and continued in the remaining slots, the slots gives the appropriate messages. WHY ?????????
Please Help !!!
import java.awt.*;
import java.awt.event.*;
class Slot1 extends Canvas implements MouseMotionListener{
class SlotMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent evt)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.drawString("Clicked me",10,20);
}
public void mouseReleased(MouseEvent evt)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.drawString("Released me",10,40);
}
}
Slot1()
{
// Add the listeners to the Slot
addMouseListener(new SlotMouseAdapter());
addMouseMotionListener(thi s);
}
public void paint(Graphics g)
{
// just draws rectangle in the slot
int x1 = 0;
int y1 = 0;
int x2 = getSize().width - 1;
int y2 = getSize().height - 1;
g.drawRect(x1, y1, x2, y2);
}
public void mouseDragged(MouseEvent evt)
{
Graphics g = getGraphics();
Cursor cursor = new Cursor(Cursor.HAND_CURSOR) ;
setCursor(cursor);
g.setColor(Color.black);
g.drawString("Dragged ",10,60);
}
public void mouseMoved(MouseEvent evt) { }
}
public class Brainvita1 extends Frame {
Panel panelLeft = new Panel();
Panel panelMiddle = new Panel();
Panel panelRight = new Panel();
class MyFrameWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
}
Brainvita1()
{
setLayout(new GridLayout(1,3));
// Add a Slot to all the Panels
add(panelLeft);
panelLeft.setLayout(new GridLayout(1,1));
panelLeft.add(new Slot1());
add(panelMiddle);
panelMiddle.setLayout(new GridLayout(1,1));
//panelMiddle.add(new Slot1()); //MIDDLE PANEL LEFT EMPTY
add(panelRight);
panelRight.setLayout(new GridLayout(1,1));
panelRight.add(new Slot1());
// add the listener
addWindowListener(new MyFrameWindowListener());
}
public static void main(String[] args)
{
Brainvita1 brain = new Brainvita1();
brain.setSize(400,400);
brain.setVisible(true);
}
}
I have deleted the previous question having the same problem , but the code seemed to be very long so I am presenting a smaller version of the code here
the following program produces a frame containing three panels in a row.The left and the right panel contains a slot while the middle panel is left empty.
when the mouse is dragged in a slot , the slot display message "Dragged"; for clicking it displays "Clicked me" and for releasing , it displays "Released me".
the problem lies here :
When the mouse-drag originates from the middle panel(REMEMBER THAT THE MIDDLE PANNEL IS EMPTY) and continued in the left/right panels (WHICH CONTAINS THE SLOTS), the slots doesn't give the appropriate messages , BUT if the mouse-drag is originated from any of the slots and continued in the remaining slots, the slots gives the appropriate messages. WHY ?????????
Please Help !!!
import java.awt.*;
import java.awt.event.*;
class Slot1 extends Canvas implements MouseMotionListener{
class SlotMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent evt)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.drawString("Clicked me",10,20);
}
public void mouseReleased(MouseEvent evt)
{
Graphics g = getGraphics();
g.setColor(Color.black);
g.drawString("Released me",10,40);
}
}
Slot1()
{
// Add the listeners to the Slot
addMouseListener(new SlotMouseAdapter());
addMouseMotionListener(thi
}
public void paint(Graphics g)
{
// just draws rectangle in the slot
int x1 = 0;
int y1 = 0;
int x2 = getSize().width - 1;
int y2 = getSize().height - 1;
g.drawRect(x1, y1, x2, y2);
}
public void mouseDragged(MouseEvent evt)
{
Graphics g = getGraphics();
Cursor cursor = new Cursor(Cursor.HAND_CURSOR)
setCursor(cursor);
g.setColor(Color.black);
g.drawString("Dragged ",10,60);
}
public void mouseMoved(MouseEvent evt) { }
}
public class Brainvita1 extends Frame {
Panel panelLeft = new Panel();
Panel panelMiddle = new Panel();
Panel panelRight = new Panel();
class MyFrameWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
}
Brainvita1()
{
setLayout(new GridLayout(1,3));
// Add a Slot to all the Panels
add(panelLeft);
panelLeft.setLayout(new GridLayout(1,1));
panelLeft.add(new Slot1());
add(panelMiddle);
panelMiddle.setLayout(new GridLayout(1,1));
//panelMiddle.add(new Slot1()); //MIDDLE PANEL LEFT EMPTY
add(panelRight);
panelRight.setLayout(new GridLayout(1,1));
panelRight.add(new Slot1());
// add the listener
addWindowListener(new MyFrameWindowListener());
}
public static void main(String[] args)
{
Brainvita1 brain = new Brainvita1();
brain.setSize(400,400);
brain.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I will leave a recommendation in the Cleanup topic area that this question is:
- Points for objects
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Venabili
EE Cleanup Volunteer