Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  MouseOver UI thingy  (Read 853 times)
0 Members and 1 Guest are viewing this topic.
Offline Alistair_Dickie

Senior Newbie




Java games rock!


« Posted 2005-02-18 09:18:38 »

After a couple of days of coding I must say thanks to all who have contributed to xith3d.

I spent quite a bit of time getting a pretty UI exit button for my app. The behaviour I want is a standard mouse over button that changes icon when you mouse over it. I extened UIWindow, made the root a JPanel, added a JButton to it and tried to use the standard setRolloverEnabled(true) and setRolloverIcon(icon) methods. This did not work.

After a bit of investigation it is apparant that the UIEventAdaptor was processing all events on the canvas and these were not being transferred to the various UIWindows. They were being transferred to the UIWindowManager however. Essentially what I had to do was fiddle with the UIWindowManager a bit so the MOUSE_MOVED events triggered MOUSED_ENTERED and MOUSE_EXITED events on the UIWindows. This still did not do the automatic rollover thing so I had to make my custom UIWindow a MouseListener and "manually" change the icons.

The really cool thing is that I now have pretty buttons that change when you mouse over them. Maybe there was an easier way...

There is still a bit of mystery. The mouse over looks to work about 10 pixels below the button?

I am not sure if someone wants to put this code in the repository. It is posted here just in case.

Regards,

Alistair

The changes to UIWindowManager:

near the top

//objects needed for enter/exit support
   UIPositionedWindow inWindow = null;


the first bit of the processMouseEvent method with my changes in bold
public synchronized void processMouseEvent(MouseEvent me) {

           boolean rightButton = ((MouseEvent.BUTTON3_MASK & me.getModifiers()) != 0);
           boolean leftButton = ((MouseEvent.BUTTON1_MASK & me.getModifiers()) != 0);

           UIPositionedWindow w = getWindow(me.getPoint());


           boolean eventConsumed = false;

           if (me.getID() == MouseEvent.MOUSE_MOVED) {
               if (mode == OM_MOVE_WINDOW) {
                   focus.setPosition(me.getX(), me.getY(), canvasDim);
                   eventConsumed = true;
               }
               else if(mode == OM_NONE){
                 if(w == null && inWindow != null){
                   //mouseExited inWindow
                   UIOverlayInterface o = inWindow.getOverlay();
                   if (o instanceof UIWindow) {
                       MouseEvent mec = new MouseEvent(me.getComponent(),
                               MouseEvent.MOUSE_EXITED,
                               me.getWhen(),
                               me.getModifiers(), me.getX(),
                               me.getY(),
                               me.getClickCount(), false);

                       ((UIWindow) o).dispatchEvent(mec);
                   }
                   inWindow = null;


                 }
                 else if(w != null && inWindow == null){
                   //mouseEntered w

                   UIOverlayInterface o = w.getOverlay();
                   if (o instanceof UIWindow) {
                       MouseEvent mec = new MouseEvent(me.getComponent(),
                               MouseEvent.MOUSE_ENTERED,
                               me.getWhen(),
                               me.getModifiers(), me.getX(),
                               me.getY(),
                               me.getClickCount(), false);
                   
                       ((UIWindow) o).dispatchEvent(mec);

                   }
                   inWindow = w;
                 }
                 else if(w != null && inWindow != null && w != inWindow){
                   //mouseEntered w and Exited inWindow

                   UIOverlayInterface o = inWindow.getOverlay();
                   if (o instanceof UIWindow) {
                       MouseEvent mec = new MouseEvent(me.getComponent(),
                               MouseEvent.MOUSE_EXITED,
                               me.getWhen(),
                               me.getModifiers(), me.getX(),
                               me.getY(),
                               me.getClickCount(), false);

                       ((UIWindow) o).dispatchEvent(mec);
                   }

                   o = w.getOverlay();
                   if (o instanceof UIWindow) {
                       MouseEvent mec = new MouseEvent(me.getComponent(),
                               MouseEvent.MOUSE_ENTERED,
                               me.getWhen(),
                               me.getModifiers(), me.getX(),
                               me.getY(),
                               me.getClickCount(), false);

                       ((UIWindow) o).dispatchEvent(mec);
                   }
                   inWindow = w;
                 }

                 //else if (w==null) setOverlayFocus((UIPositionedWindow) null);
               }
           }

And here is my custom UIWindow that I use it with:

package islandtest;

import com.xith3d.userinterface.UIWindow;


import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class IslandExitUIWindow extends UIWindow implements ActionListener, MouseListener {

 private IslandMain main;
 private Icon grey;
 private Icon red;
 private JButton button;


 public IslandExitUIWindow(IslandMain main) {
   super(50,50, true, true);

   this.main = main;
   JPanel panel = new JPanel();
   panel.setLayout(new GridLayout(1,1));
   panel.setDoubleBuffered(true);
   panel.setSize(new Dimension(40,40));
   panel.setLocation(0,0);
   panel.setBackground(new Color(0,0,0,0));

   button = new JButton();
//    button.setLayout(new BoxLayout(button,BoxLayout.X_AXIS));
   button.setSize(40,40);
   button.addActionListener(this);
   button.setMargin(new Insets(0,0,0,0));
   button.setBorder(BorderFactory.createEmptyBorder());
   button.setLocation(0,0);
   button.setBackground(new Color(0,0,0,0));
 
   panel.addMouseListener(this);

   try {
     grey = new ImageIcon(ImageIO.read(new File("d:/code/island/src/islandtest/data/exitgrey.png")));
     red = new ImageIcon(ImageIO.read(new File("d:/code/island/src/islandtest/data/exitred.png")));
     button.setIcon(grey);

   }
   catch (IOException e) {
     e.printStackTrace();
     button.setText("Exit");
   }
   panel.add(button);

   this.setRoot(panel);

 }

 public void actionPerformed(ActionEvent e) {
    main.doExit();

 }

 public void mouseClicked(MouseEvent e) {

 }

 public void mousePressed(MouseEvent e) {

 }

 public void mouseReleased(MouseEvent e) {

 }

 public void mouseEntered(MouseEvent e) {
   button.setIcon(red);


 }

 public void mouseExited(MouseEvent e) {
   button.setIcon(grey);
 }


}



Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!