Cyber Syndicate
<center><p style='font-size:120%;'><strong>Welcome to Cyber Syndicate!<strong></p>

You are visiting as a guest; please <strong>register</strong> or <strong>log in</strong>.

Being logged in provides many benefits, including access to the useful <strong>Methods forum</strong>, posting permissions, and voting permissions.

<strong>Enjoy.</strong></center>
Cyber Syndicate
<center><p style='font-size:120%;'><strong>Welcome to Cyber Syndicate!<strong></p>

You are visiting as a guest; please <strong>register</strong> or <strong>log in</strong>.

Being logged in provides many benefits, including access to the useful <strong>Methods forum</strong>, posting permissions, and voting permissions.

<strong>Enjoy.</strong></center>
Cyber Syndicate
Would you like to react to this message? Create an account in a few clicks or log in to continue.



 
HomeLatest imagesRegisterLog in

 

 ClickableURL class for paint

Go down 
3 posters
AuthorMessage
UberMouse
Member
Member



Posts : 5
Join date : 2011-02-25

ClickableURL class for paint Empty
PostSubject: ClickableURL class for paint   ClickableURL class for paint Icon_minitimeSat 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);
Back to top Go down
TaffTech
Administrator
Administrator



Posts : 72
Join date : 2011-02-27

ClickableURL class for paint Empty
PostSubject: Re: ClickableURL class for paint   ClickableURL class for paint Icon_minitimeFri Mar 04, 2011 12:08 am

Awesome, I can see how this would be useful, for feedback and such.
Back to top Go down
Remo
Member
Member



Posts : 41
Join date : 2011-03-10

ClickableURL class for paint Empty
PostSubject: Re: ClickableURL class for paint   ClickableURL class for paint Icon_minitimeThu 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 Very Happy
Back to top Go down
UberMouse
Member
Member



Posts : 5
Join date : 2011-02-25

ClickableURL class for paint Empty
PostSubject: Re: ClickableURL class for paint   ClickableURL class for paint Icon_minitimeThu 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.
Back to top Go down
Sponsored content





ClickableURL class for paint Empty
PostSubject: Re: ClickableURL class for paint   ClickableURL class for paint Icon_minitime

Back to top Go down
 
ClickableURL class for paint
Back to top 
Page 1 of 1
 Similar topics
-
» static class Lobby
» class Notifier - Notification System
» Paint isn't displaying!
» Salvation's paint diary
» Paint for my UniCash script

Permissions in this forum:You cannot reply to topics in this forum
Cyber Syndicate :: Development :: Methods-
Jump to: