Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Last active November 24, 2016 22:45
Show Gist options
  • Save CalebWhiting/8b5bb0c411f155992c60 to your computer and use it in GitHub Desktop.
Save CalebWhiting/8b5bb0c411f155992c60 to your computer and use it in GitHub Desktop.
DTM Maker
import org.dang.api.script.methods.DPainter;
import org.runedream.api.Script;
import org.runedream.api.ScriptManifest;
import org.runedream.api.methods.Game;
import org.runedream.api.util.Log;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;
/**
* User: Caleb Date: 02/02/12 Time: 17:28
*/
@ScriptManifest(name = "Template creator", authors = "dang", version = 1.0)
public class TemplateMaker extends Script {
final Double VERSION = this.getManifest().version();
final boolean string = true;
TFrame TFrame;
BufferedImage image = Game.getImage();
public List<SubString> strings = new LinkedList<SubString>();
CentralString central = null;
public javax.swing.JPanel colorPanel;
public javax.swing.JButton copy;
public javax.swing.JButton deleteAll;
public javax.swing.JButton deleteLast;
public javax.swing.JButton getScreen;
public ImagePanel s;
public javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
public javax.swing.JSpinner tolerance;
public String colorToString(final Color color) {
return "new Color(" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + ")";
}
public class CentralString {
private Color color;
private int tolerance;
private Point point;
public CentralString(final Color color, final int tolerance, final Point point) {
this.color = color;
this.tolerance = tolerance;
this.point = point;
}
@Override
public String toString() {
return "new DTMCentral(" + colorToString(central.color) + ", " + central.tolerance + ")";
}
}
public class SubString {
private final int x;
private final int y;
private final Color color;
private final int tolerance;
public SubString(final int x, final int y, final Color color, final int tolerance) {
this.tolerance = tolerance;
this.x = x;
this.y = y;
this.color = color;
}
@Override
public String toString() {
return "new DTMSubPoint(" + colorToString(color) + ", " + this.x + ", " + this.y + ", " + this.tolerance + ")";
}
}
@Override
public int loop() {
if (s.getBounds().contains(MouseInfo.getPointerInfo().getLocation())) {
final Point mouse = s.getMousePosition();
colorPanel.setBackground(new Color(image.getRGB(mouse.x, mouse.y)));
}
s.repaint();
return 0;
}
@Override
public boolean onStart() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
TFrame = new TFrame();
TFrame.setVisible(true);
}
});
} catch (Exception e) {
Log.log("Can't open the GUI");
return false;
}
return true;
}
public class TFrame extends javax.swing.JFrame {
/**
* Creates new form TFrame
*/
public TFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code.
* The content of this method is always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
s = new ImagePanel();
copy = new javax.swing.JButton();
deleteLast = new javax.swing.JButton();
tolerance = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
colorPanel = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
deleteAll = new javax.swing.JButton();
getScreen = new javax.swing.JButton();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
s.setRequestFocusEnabled(false);
s.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
sMouseClicked(evt);
}
});
setTitle("[" + VERSION + "] Dang's Template creator");
javax.swing.GroupLayout sLayout = new javax.swing.GroupLayout(s);
s.setLayout(sLayout);
sLayout.setHorizontalGroup(
sLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 764, Short.MAX_VALUE)
);
sLayout.setVerticalGroup(
sLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 505, Short.MAX_VALUE)
);
copy.setText("Copy");
copy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
copyActionPerformed(evt);
}
});
deleteLast.setText("Delete last point");
deleteLast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteLastActionPerformed(evt);
}
});
jLabel1.setText("Tolerance");
javax.swing.GroupLayout colorPanelLayout = new javax.swing.GroupLayout(colorPanel);
colorPanel.setLayout(colorPanelLayout);
colorPanelLayout.setHorizontalGroup(
colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 162, Short.MAX_VALUE)
);
colorPanelLayout.setVerticalGroup(
colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jLabel2.setText("Color under mouse");
deleteAll.setText("Delete all");
deleteAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteAllActionPerformed(evt);
}
});
getScreen.setText("Get Screen");
getScreen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
getScreenActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(s, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(colorPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(jLabel2))
.addComponent(copy, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(deleteLast, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(deleteAll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(getScreen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(tolerance, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
.addComponent(jLabel1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(colorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 240, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tolerance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(5, 5, 5)
.addComponent(getScreen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteAll)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteLast)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(copy))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(s, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void deleteLastActionPerformed(java.awt.event.ActionEvent evt) {
if (strings.size() > 0) {
}
}
private void copyActionPerformed(java.awt.event.ActionEvent evt) {
if (!string) {
String code = "new DTM(";
code = code + central.toString();
for (final SubString string : strings) {
code = code + ", " + string.toString();
}
code = code + ")";
copyStringToClipboard(code);
} else {
final Color c = central.color;
SpinnerNumberModel model = (SpinnerNumberModel) tolerance.getModel();
final int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), t = central.tolerance;
String code = "DTM.fromString(\""+r+"_"+g+"_"+b+"_"+t+"\"";
for(final SubString string : strings) {
final Color sc = string.color;
final int sr = sc.getRed(), sg = sc.getGreen(), sb = sc.getBlue(), st = string.tolerance, x = string.x, y = string.y;
//r g b x y t
code = code + ", \""+sr+"_"+sg+"_"+sb+"_"+x+"_"+y+"_"+st+"\"";
}
code = code + ")";
copyStringToClipboard(code);
}
}
public void copyStringToClipboard(String str) {
StringSelection stringSelection = new StringSelection(str);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}
private void deleteAllActionPerformed(java.awt.event.ActionEvent evt) {
central = null;
strings.clear();
}
private void sMouseClicked(java.awt.event.MouseEvent evt) {
Set_string(evt);
}
private void getScreenActionPerformed(java.awt.event.ActionEvent evt) {
image = Game.getImage();
}
/**
* @param args the command line arguments
*/
public void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TFrame().setVisible(true);
}
});
}
}
private void Set_string(MouseEvent event) {
final Point e = event.getPoint();
final Color c = new Color(image.getRGB(e.x, e.y));
SpinnerNumberModel model = (SpinnerNumberModel) tolerance.getModel();
int tol = model.getNumber().intValue();
if (central == null) {
final Point p = e;
central = new CentralString(c, tol, p);
Log.log("Mid point: " + central.toString());
} else {
strings.add(new SubString(e.x - central.point.x, e.y - central.point.y, c, tol));
}
}
class ImagePanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
DPainter.setGraphics(g);
super.paintComponent(g);
g.setColor(Color.red);
if (image != null) g.drawImage(image, 0, 0, null);
if (central != null)
DPainter.drawCross(central.point, 6);
if (strings.size() > 0) {
for (final SubString string : strings) {
final Point p = new Point(central.point.x + string.x, central.point.y + string.y);
DPainter.drawCross(p, 6);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment