change background color on mouse event

Hi there,

This seems like a really dumb question, which is why i'm so frustrated at the moment, can anybody explain why, when the mouse enters the an imagePanel, it doesn't change colour?

Any help is appreciated,

Thanks

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.*;

publicclass GridBagLayoutDemo{

publicstaticvoid main(String[] args){

create();

}

publicstaticvoid create(){

JFrame frame =new JFrame();

JLabel titleLabel =new JLabel("Title: ");

JLabel storyLabel =new JLabel("Story: ");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel =new JPanel();

frame.add(panel);

panel.setLayout(new BorderLayout());

JPanel titlePanel =new JPanel();

JPanel mainImagePanel =new JPanel();

titlePanel.setBackground(Color.RED);

titlePanel.setLayout(new GridLayout(2,0));

panel.add(titlePanel,BorderLayout.PAGE_START);

panel.add(mainImagePanel);

mainImagePanel.setLayout(new GridLayout(2,5));

imagePanel imagePanel0 =new imagePanel();

imagePanel imagePanel1 =new imagePanel();

imagePanel imagePanel2 =new imagePanel();

imagePanel imagePanel3 =new imagePanel();

imagePanel imagePanel4 =new imagePanel();

imagePanel imagePanel5 =new imagePanel();

imagePanel imagePanel6 =new imagePanel();

imagePanel imagePanel7 =new imagePanel();

imagePanel imagePanel8 =new imagePanel();

imagePanel imagePanel9 =new imagePanel();

mainImagePanel.add(imagePanel0);

mainImagePanel.add(imagePanel1);

mainImagePanel.add(imagePanel2);

mainImagePanel.add(imagePanel3);

mainImagePanel.add(imagePanel4);

mainImagePanel.add(imagePanel5);

mainImagePanel.add(imagePanel6);

mainImagePanel.add(imagePanel7);

mainImagePanel.add(imagePanel8);

mainImagePanel.add(imagePanel9);

titlePanel.add(titleLabel);

titlePanel.add(storyLabel);

mainImagePanel.setSize(550, 275);

int height = (mainImagePanel.getHeight() + titlePanel.getHeight());

int width = (mainImagePanel.getWidth());

System.out.println(height);

System.out.println(width);

imagePanel0.setToolTipText("One");

imagePanel1.setToolTipText("Two");

imagePanel2.setToolTipText("Three");

imagePanel3.setToolTipText("Four");

imagePanel4.setToolTipText("Five");

imagePanel5.setToolTipText("Six");

imagePanel6.setToolTipText("Seven");

imagePanel7.setToolTipText("Eight");

imagePanel8.setToolTipText("Nine");

imagePanel9.setToolTipText("Ten");

frame.setSize(width, height);

frame.show();

}

}

class imagePanelextends JPanelimplements MouseListener

{

publicvoid paint(Graphics g)

{

Image image = Toolkit.getDefaultToolkit().getImage("/home/University Work/Java/src/Picture.jpg");

this.setSize(image.getWidth(this) + 10,image.getHeight(this) + 10);

g.drawImage(image,5,5,this);

addMouseListener(this);

}

publicvoid mousePressed(MouseEvent e)

{

System.out.println("Pressed");

}

publicvoid mouseReleased(MouseEvent e)

{

System.out.println("Released");

}

publicvoid mouseEntered(MouseEvent e)

{

this.setBackground(Color.BLUE);

}

publicvoid mouseExited(MouseEvent e)

{

System.out.println("Exited");

}

publicvoid mouseClicked(MouseEvent e)

{

System.out.println("Clicked");

}

}

[6669 byte] By [oookiezoooa] at [2007-10-2 14:31:24]
# 1
Try this.>public void mouseEntered(MouseEvent e) >{> this.setBackground(Color.BLUE);repaint();>}
tjacobs01a at 2007-7-13 12:53:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the reply, but it didn't work, i realised as well it is impossible to change the imagePanel background colour at all!
oookiezoooa at 2007-7-13 12:53:39 > top of Java-index,Desktop,Core GUI APIs...
# 3

There are a number of problems with this example:

a) you need a super.paint(g) in order for the background to be painted.

b) however, in Swing you should be overriding paintComponent(...) not paint(...)

c) Your class name should be "ImagePanel" not "imagePanel". Classes are upper cased.

d) You shouldn't be reading the image in the paint method. This is inefficient as the paint method is called every time the component needs to be repainted. Read the image in the constructor or pass it to the Object if it is the same image for all panels.

e) using setSize(...) does nothing when using a LayoutManager. You should be using setPreferredSize(..). Again this should be done in the contructor not the paintComponent(..) method.

f) you are adding the MouseListener in the paintComponent(..) method. Again you will have a new listener added every time the component is repainted. It should be added in the constructor.

g) you probably don't even need to create a custom panel. Just add the image to a JLabel.

camickra at 2007-7-13 12:53:39 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thank you fo rthe detailed answer, i'm really unfamiliar with painting etc, so you've answered alot of my doubts there, thanks again
oookiezoooa at 2007-7-13 12:53:39 > top of Java-index,Desktop,Core GUI APIs...