Results 1 to 5 of 5
- 11-18-2011, 03:27 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
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.
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.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; } }
- 11-18-2011, 08:26 PM #2
Re: repaint in mouselisteners with GradientPaint
Yes, there is NO requirement for a class to have a main method.make a class (a custom shape) and add mouselisteners inside it even if we don't have a main method in that class
Does the class that you want to have mouse listeners for have the focus?
- 11-19-2011, 07:53 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
- 11-19-2011, 09:07 PM #4
Re: repaint in mouselisteners with GradientPaint
You should read up on focus if you do not know what it is.
How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- 11-19-2011, 09:08 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
mouseListeners
By imorio in forum AWT / SwingReplies: 6Last Post: 08-12-2010, 05:31 PM -
GradientPaint: iron
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:49 PM -
Another GradientPaint Demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:48 PM -
GradientPaint Ellipse
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:48 PM -
GradientPaint demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks