Results 1 to 5 of 5
  1. #1
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default 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.

    Java 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: repaint in mouselisteners with GradientPaint

    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?

  3. #3
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: repaint in mouselisteners with GradientPaint

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: repaint in mouselisteners with GradientPaint


  5. #5
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: repaint in mouselisteners with GradientPaint

    Thanks. I never heard about the concept before.

Similar Threads

  1. mouseListeners
    By imorio in forum AWT / Swing
    Replies: 6
    Last Post: 08-12-2010, 05:31 PM
  2. GradientPaint: iron
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:49 PM
  3. Another GradientPaint Demo
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:48 PM
  4. GradientPaint Ellipse
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:48 PM
  5. GradientPaint demo
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •