This will provide a class for you to easily implement a Paint (optional that you can allow dragging of it over the applet).
DraggablePaint.java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
/**
*
* @author Yah Mie @ OSBot
*
*/
public class DraggablePaint {
private Point location;
private int width;
private int height;
private Image background = null;
private Map<Point, Map<String, Object>> texts;
private boolean beingDragged = false;
private Point draggedFrom;
/**
* Constructors
*/
public DraggablePaint(Point location, int width, int height) {
this.location = location;
this.width = width;
this.height = height;
flushText();
}
public DraggablePaint(int x, int y, int width, int height) {
this.location = new Point(x, y);
this.width = width;
this.height = height;
flushText();
}
/**
* Set background image
* Renders at 0,0
* @param URL
*/
public void setBackgroundImage(String URL) {
try {
background = ImageIO.read(new URL(URL));
} catch (IOException e) {
background = null;
}
}
/**
* Move location of paint
* @param location
*/
public void moveTo(Point location) {
this.location = location;
}
/**
* Write text at position
* Relative to paint
*
* @param text
* @param at
*/
public void writeText(String text, Point at) {
Map<String, Object> textConfig = new HashMap<String, Object>();
textConfig.put("text", text);
textConfig.put("font", new Font("Arial", 1, 12));
textConfig.put("color", Color.black);
texts.put(at, textConfig);
}
/**
* Write text at position
* Specified color & font
* Relative to paint
*
* @param text
* @param at
*/
public void writeText(String text, Point at, Color colour, Font font) {
Map<String, Object> textConfig = new HashMap<String, Object>();
textConfig.put("text", text);
textConfig.put("font", font);
textConfig.put("color", colour);
texts.put(at, textConfig);
}
/**
* Flushes all texts ready to be written to again
*/
public void flushText() {
texts = new HashMap<Point, Map<String, Object>>();
}
/**
* Handle Mouse Events
*
* @param eventType
* @param e
*
* Called upon MouseEvent: mousePressed
* Called upon MouseEvent: mouseReleased
* Called upon MouseEvent: mouseDragged
*/
public void draggable(String eventType, MouseEvent e) {
if (eventType.equalsIgnoreCase("mousePressed")) {
Point point = e.getPoint();
if (point.x >= location.x && point.x <= location.x + width
&& point.y >= location.y && point.y <= location.y + height) {
beingDragged = true;
draggedFrom = new Point(point.x, point.y);
}
}
else if (eventType.equalsIgnoreCase("mouseReleased")) {
Point point = e.getPoint();
if (point.x >= location.x && point.x <= location.x + width
&& point.y >= location.y && point.y <= location.y + height) {
beingDragged = false;
}
}
else if (eventType.equalsIgnoreCase("mouseDragged")) {
if (beingDragged) {
location.x = e.getX() - (draggedFrom.x - location.x);
location.y = e.getY() - (draggedFrom.y - location.y);
draggedFrom = new Point(e.getX(), e.getY());
/* Off-screen fixes */
if (location.x < 0) {
location.x = 0;
}
if (location.x + width > 763) {
location.x = 245;
}
if (location.y < 0) {
location.y = 0;
}
if (location.y + height > 502) {
location.y = 428;
}
}
}
}
/**
* Called upon onPaint
*/
public void draw(Graphics2D g) {
if (background != null) {
// Draw background
g.drawImage(background, location.x, location.y, null);
}
if (texts.size() > 0) {
for (Map.Entry<Point, Map<String, Object>> text : texts.entrySet()) {
Map<String, Object> textConfig = text.getValue();
// Set Font and Color
g.setColor((Color)textConfig.get("color"));
g.setFont((Font)textConfig.get("font"));
// Draw text at location
g.drawString((String)textConfig.get("text"), location.x + text.getKey().x, location.y + text.getKey().y);
}
}
}
}
Usage:
import java.awt.*;
import java.awt.event.MouseEvent;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
@ScriptManifest(author="Yah Mie", version=1, info="", name="Yah Mie's Draggable Paint Demo")
public class ScriptWithDraggablePaint extends Script {
private DraggablePaint paint;
public ScriptWithDraggablePaint() {
paint = new DraggablePaint(0, 268, 519, 76); // X, Y, WIDTH, HEIGHT
paint.setBackgroundImage("http://oi41.tinypic.com/2gvrbc1.jpg");
}
@Override
public void mousePressed(MouseEvent e) {
paint.draggable("mousePressed", e);
}
@Override
public void mouseReleased(MouseEvent e) {
paint.draggable("mouseReleased", e);
}
@Override
public void mouseDragged(MouseEvent e) {
paint.draggable("mouseDragged", e);
}
@Override
public void onPaint(Graphics g) {
Graphics2D gr = (Graphics2D) g;
paint.flushText();
paint.writeText("Running for: 00:30:00", new Point(20, 24));
paint.writeText("XP Gained: 500,000", new Point(20, 44));
paint.writeText("XP Per Hour: 1,000,000", new Point(20, 59));
paint.writeText("Logs Burned: 1,500", new Point(210, 44));
paint.writeText("XP Per Hour: 3,000", new Point(210, 59));
paint.draw(gr);
}
}
If you don't want to implement dragging then do not implement the mouse events (draggable() method).
All methods provided by DraggablePaint.java:
DraggablePaint(Point location, int width, int height)
DraggablePaint(int x, int y, int width, int height)
setBackgroundImage(String URL)
- Sets background image from URL (drawn at 0, 0)
moveTo(Point location)
- Moves paint to specified location of applet
writeText(String text, Point at)
Write text at specified location (relative to paint) with Arial 12pt Black
writeText(String text, Point at, Color colour, Font font)
- Write text at specified location (relative to paint) with custom font and colour
flushText()
- Clears all text form paint
draggable(String eventType, MouseEvent e)
- Handles mouse events for dragging. All three pressed, released and dragged must be implemented for dragging to work.
draw(Graphics2D g)
- Draws paint onto graphics
Finally some tests:
I'm welcome to questions or comments. All written by I, have fun!