UberMouse Member
Posts : 5 Join date : 2011-02-25
| Subject: ClickableURL class for paint Sat Feb 26, 2011 12:56 am | |
| Here is the class - Code:
-
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; import java.text.AttributedString;
public class ClickableURL { private String url; private String shortName = null; private int x; private int y; private Point mouse = new Point(-1, -1); private Rectangle2D hoverArea = new Rectangle(-1, -1); private static final Font font1 = new Font("Arial", Font.ITALIC, 13); private static final Color white = new Color(255, 255, 255); private static final Color grey = new Color(150, 150, 150);
/** * Creates a new ClickableURL * * @param url the url to open on click * @param shortName the name to display the URL as * @param x the x co-ord to draw at * @param y the y co-ord to draw at */ public ClickableURL(String url, String shortName, int x, int y) { this.url = url; this.shortName = shortName; this.x = x; this.y = y; }
/** * Creates a new ClickableURL * * @param url the url to open on click and display name * @param x the x co-ord to draw at * @param y the y co-ord to draw at */ public ClickableURL(String url, int x, int y) { this.url = url; this.x = x; this.y = y; }
public void repaint(Graphics2D g) { String name = (shortName != null) ? shortName : url; AttributedString as = new AttributedString(name); as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, name.length() - 1); as.addAttribute(TextAttribute.FONT, font1); Rectangle2D bounds = font1.getStringBounds(name, g.getFontRenderContext()); hoverArea = new Rectangle(x, y - (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight()); if (hoverArea.contains(mouse)) { g.setColor(grey); } else g.setColor(white); new TextLayout(as.getIterator(), g.getFontRenderContext()).draw(g, (float) x, (float) y); }
public void mouseMoved(MouseEvent mouseEvent) { mouse = mouseEvent.getPoint(); }
public void mouseClicked(MouseEvent mouseEvent) { if (hoverArea.contains(mouseEvent.getPoint())) { util.openURL(url); } } }
Now to use that create a new instance in your script. Ex - Code:
-
ClickableURL test = new ClickableURL("http://google.com/", "Google", 50,50);
To use them you will have to put implements MouseMotionListener, MouseListener in your script declaration ex - Code:
-
public class scriptname extends ActiveScript implements MouseMotionListener, MouseListener
Then implement all the methods required by the interfaces Then in mouseClicked and mouseMoved you will need to call the methods in the ClickableURL ex - Code:
-
public void mouseMoved(MouseEvent e) { threadURL.mouseMoved(e); for (ClickableURL url : urls) url.mouseMoved(e); }
is how I do it in UberDungeon, urls is an ArrayList I store most of my URL classes in. Then do the same in your onRepaint() function Except call .repaint(Graphics 2D g); - Code:
-
threadURL.repaint(g); for (ClickableURL url : urls) url.mouseMoved(g);
| |
|
TaffTech Administrator
Posts : 72 Join date : 2011-02-27
| Subject: Re: ClickableURL class for paint Fri Mar 04, 2011 12:08 am | |
| Awesome, I can see how this would be useful, for feedback and such. | |
|
Remo Member
Posts : 41 Join date : 2011-03-10
| Subject: Re: ClickableURL class for paint Thu Mar 10, 2011 5:51 pm | |
| Sweet. But why would you need the /**param stuff? Why would you need to view the Javadoc? Otherwise, very sweet. I'll be using this soon | |
|
UberMouse Member
Posts : 5 Join date : 2011-02-25
| Subject: Re: ClickableURL class for paint Thu Mar 10, 2011 5:52 pm | |
| Creating JDocs is good practice, I do it for the majority of my functions, especially since UberDungeon has an API so I am required to JDocs the majority of the functions. | |
|
Sponsored content
| Subject: Re: ClickableURL class for paint | |
| |
|