codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  changing background colors with mouseEvent  alainap21 at 23:05 on Thursday, February 02, 2006
 

im creating a window that when the mouse moves over one side of it it will turn red and when the mouse ones over the other side it will turn green. im a little confused about the code used to calculate that within my mouseEntered and mouseExited methods.

  Re: changing background colors with mouseEvent  crwood at 21:24 on Sunday, February 05, 2006
 


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

public class MouseChanges extends JPanel
{
public void changeBackground(boolean isLeft)
{
Color color = Color.green.darker();
if(isLeft)
color = Color.red;
setBackground(color);
repaint();
}

public static void main(String[] args)
{
MouseChanges mc = new MouseChanges();
mc.addMouseMotionListener(new PanelListener());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(mc);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class PanelListener extends MouseMotionAdapter
{
int lastSide = -1;
final int LEFT = 0;
final int RIGHT = 1;

public void mouseMoved(MouseEvent e)
{
Point p = e.getPoint();
MouseChanges mc = (MouseChanges)e.getSource();
int middle = mc.getWidth()/2;
int side = p.x <= middle ? LEFT : RIGHT;
if(side != lastSide)
{
mc.changeBackground(p.x <= middle);
lastSide = side;
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  adding delay to menu
•  perl output in browser
•  Re: Small Java Program Help
•  Some questions about JS.
•  Some questions about JS.
•  Applying discount
•  Re: Hello...
•  Please Help Javascript College Assignment
•  PDF manipulation


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007