repaint in mouselisteners with GradientPaint
Is it possible to make a class (a custom shape) and add mouselisteners inside it even if we don't have a main method in that class? I want to use objects from that class in another class, and when I click on an object I want it to change its Gradient. I've tried a hundred ways using pain, repaint and mouselisteners. But I don't know where to add the listeners or if they have any effect if i add them in that specific class. The code works perfectly, and the buttons are displayed on the screen, but I cannot make the mouse events to work. I should also mention I have mouse listeners in my main class as i need them for other components.
Code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.RoundRectangle2D;
import acm.graphics.*;
public class GButton extends GCompound implements MouseListener{
RoundRectangle2D button;
Color bC1 = new Color(255,85,85);
Color bC2 = new Color(150,0,5);
Color bC1s = new Color(255,120,120);
Color bC2s = new Color(90,0,0);
GradientPaint gp1=new GradientPaint(0, 0, bC1, 0, 25, bC2,true);
GradientPaint gp2=new GradientPaint(0, 0, bC2, 0, 25, bC1,true);
GradientPaint gp=gp1;
Stroke stroke = new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0);
Stroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0);
boolean pressed=false;
GButton (){
button = new RoundRectangle2D.Double(0,0,40,35,10,10);
addMouseListener(this);
}
GButton(int x, int y, int h, int w, int r){
button = new RoundRectangle2D.Double(x,y,h,w,r,r);
addMouseListener(this);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(getMyGradient());
g2.fill(button);
g2.setColor(bC1s);
g2.setStroke(stroke);
g2.draw(button);
g2.setColor(bC2s);
g2.setStroke(stroke2);
g2.draw(button);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
setGradient(gp2);
repaint();
}
public void mouseReleased(MouseEvent e) {
setGradient(gp1);
repaint();
}
public void setGradient(GradientPaint g) {
gp = g;
}
public GradientPaint getMyGradient() {
return gp;
}
}
Also, how do I use the GradientPaint correctly? It seems to start at 0,0 on the screen and is the same over the entire screen for all the objects that are build from this class. I want each object from this class to have its own Gradient starting point / pattern.
Re: repaint in mouselisteners with GradientPaint
Quote:
make a class (a custom shape) and add mouselisteners inside it even if we don't have a main method in that class
Yes, there is NO requirement for a class to have a main method.
Does the class that you want to have mouse listeners for have the focus?
Re: repaint in mouselisteners with GradientPaint
Quote:
Originally Posted by
Norm
Does the class that you want to have mouse listeners for have the focus?
What do you mean by have the focus? It is my main class and mouse listeners work in it for other objects, if that's what you are referring to.
Re: repaint in mouselisteners with GradientPaint
Re: repaint in mouselisteners with GradientPaint
Thanks. I never heard about the concept before.