computor Posted December 14, 2014 Posted December 14, 2014 (edited) I'm trying to get my GUI to run at all. Why doesn't it close when I click the start button. I thought i added the correct event handlers. The GUI opens, and I can interact with it, but when I click on the "start" button, nothing happens. Why? Here's my code, I know it's shitty: import org.osbot.rs07.api.ui.Skill; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import java.awt.*; import java.io.IOException; import java.net.URL; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import java.awt.*; import java.awt.event.*; import javax.swing.*; @ScriptManifest(name = "AFKer", author = "Computor", version = 1.0, info = "", logo = "") public class AFKer extends Script { private long timeBegan; private int xpPerHour; //GUI botGUI g = new botGUI(); public boolean guiWait = true; public String foodToEat; public int skillDisplay; private int beginningAttack; private int currentAttackXp; private int currentAttackLevel; private int attackXpGained; private double nextAttackLevelXp; private long timeAttackTNL; private double xpTillNextAttackLevel; private int beginningHitpoints; private int currentHitpointsXp; private int currentHitpointsLevel; private int hitpointsXpGained; private double nextHitpointsLevelXp; private double xpTillNextHitpointsLevel; private int beginningStrength; private int currentStrengthXp; private int currentStrengthLevel; private int strengthXpGained; private double nextStrengthLevelXp; private long timeStrengthTNL; private double xpTillNextStrengthLevel; private int beginningDefense; private int currentDefenseXp; private int currentDefenseLevel; private int defenseXpGained; private double nextDefenseLevelXp; private long timeDefenseTNL; private double xpTillNextDefenseLevel; final int[] XP_TABLE = { 0, 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 11805606, 13034431, 200000000 }; //paint background private final Image bg = getImage("http://s12.postimg.org/wty7h6sb1/AFKer.png"); private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) {} return null; } //end paint background @Override public void onStart() { //Code here will execute before the loop is started timeBegan = System.currentTimeMillis(); beginningAttack = skills.getExperience(Skill.ATTACK); beginningHitpoints = skills.getExperience(Skill.HITPOINTS); beginningStrength = skills.getExperience(Skill.STRENGTH); beginningDefense = skills.getExperience(Skill.DEFENCE); timeAttackTNL = 0; timeStrengthTNL = 0; timeDefenseTNL = 0; g.setVisible(true); if (guiWait == false ){ g.dispose(); } } @Override public void onExit() { //Code here will execute after the script ends } @Override public int onLoop() throws InterruptedException { if(foodToEat != null){ inventory.interact(foodToEat, "Eat"); } return 123; } @Override public void onPaint(Graphics2D g) { //This is where you will put your code for paint(s) //background paint Graphics2D gr = g; g.drawImage(bg, 4, 341, null); //attack currentAttackXp = skills.getExperience(Skill.ATTACK); currentAttackLevel = skills.getStatic(Skill.ATTACK); attackXpGained = currentAttackXp - beginningAttack; xpPerHour = (int)( attackXpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D)); nextAttackLevelXp = XP_TABLE[currentAttackLevel + 1]; xpTillNextAttackLevel = nextAttackLevelXp - currentAttackXp; if (attackXpGained >= 1) { timeAttackTNL = (long) ((xpTillNextAttackLevel / xpPerHour) * 3600000); } if (skillDisplay == 1){ g.drawString("" + ft(timeAttackTNL), 377, 427); } g.drawString("" + attackXpGained, 203, 427); //strength currentStrengthXp = skills.getExperience(Skill.STRENGTH); currentStrengthLevel = skills.getStatic(Skill.STRENGTH); strengthXpGained = currentStrengthXp - beginningStrength; xpPerHour = (int)( strengthXpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D)); nextStrengthLevelXp = XP_TABLE[currentStrengthLevel + 1]; xpTillNextStrengthLevel = nextStrengthLevelXp - currentStrengthXp; if (strengthXpGained >= 1) { timeStrengthTNL = (long) ((xpTillNextStrengthLevel / xpPerHour) * 3600000); } if (skillDisplay == 2){ g.drawString("" + ft(timeStrengthTNL), 377, 427); } g.drawString("" + strengthXpGained,220, 458); //Defence currentDefenseXp = skills.getExperience(Skill.DEFENCE); currentDefenseLevel = skills.getStatic(Skill.DEFENCE); defenseXpGained = currentDefenseXp - beginningDefense; xpPerHour = (int)( defenseXpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D)); nextDefenseLevelXp = XP_TABLE[currentDefenseLevel + 1]; xpTillNextDefenseLevel = nextDefenseLevelXp - currentDefenseXp; if (defenseXpGained >= 1) { timeDefenseTNL = (long) ((xpTillNextDefenseLevel / xpPerHour) * 3600000); } if (skillDisplay == 3){ g.drawString("" + ft(timeDefenseTNL), 377, 427); } g.drawString("" + defenseXpGained, 432, 401); //hitpoints currentHitpointsXp = skills.getExperience(Skill.HITPOINTS); currentAttackLevel = skills.getStatic(Skill.HITPOINTS); hitpointsXpGained = currentHitpointsXp - beginningHitpoints; xpPerHour = (int)( hitpointsXpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D)); nextHitpointsLevelXp = XP_TABLE[currentHitpointsLevel + 1]; xpTillNextHitpointsLevel = nextHitpointsLevelXp - currentHitpointsXp; g.drawString("" + hitpointsXpGained, 439, 458); int totalxp = attackXpGained + strengthXpGained + hitpointsXpGained + defenseXpGained; g.drawString("" + totalxp, 249, 401); } private String ft(long duration) { String res = ""; long days = TimeUnit.MILLISECONDS.toDays(duration); long hours = TimeUnit.MILLISECONDS.toHours(duration) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration)); long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS .toHours(duration)); long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS .toMinutes(duration)); if (days == 0) { res = (hours + ":" + minutes + ":" + seconds); } else { res = (days + ":" + hours + ":" + minutes + ":" + seconds); } return res; } class botGUI extends JFrame { public botGUI() { initComponents(); } private void startButtonActionPerformed(ActionEvent e) { String chosen = whatToEat.getSelectedText().toString(); if (chosen.equals("Lobster")){ foodToEat = "Lobster"; } if (chosen.equals("Shark")){ foodToEat = "Shark"; } if (chosen.equals("Tuna")){ foodToEat = "Tuna"; } if (chosen.equals("Monkfish")){ foodToEat = "Monkfish"; } if (chosen.equals("Swordfish")){ foodToEat = "Swordfish"; } else{ } String chosen2 = whatToAttack.getSelectedText().toString(); if (chosen2.equals("Attack")){ skillDisplay = 1; } if (chosen2.equals("Strength")){ skillDisplay = 2; } if (chosen2.equals("Defence")){ skillDisplay = 3; } else{ } guiWait = false; g.dispose(); } private void initComponents() { startButton = new JButton(); label1 = new JLabel(); whatToEat = new JTextField(); label2 = new JLabel(); whatToAttack = new JTextField(); //======== this ======== setAlwaysOnTop(true); setResizable(false); setTitle("Computor's AFKer"); Container contentPane = getContentPane(); contentPane.setLayout(null); //---- startButton ---- startButton.setText("Start"); startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { startButtonActionPerformed(e); } }); contentPane.add(startButton); startButton.setBounds(87, 175, 210, 65); //---- label1 ---- label1.setText("What are you going to train? (Attack, Strength, Defence)"); contentPane.add(label1); label1.setBounds(new Rectangle(new Point(35, 40), label1.getPreferredSize())); contentPane.add(whatToEat); whatToEat.setBounds(35, 120, 130, whatToEat.getPreferredSize().height); //---- label2 ---- label2.setText("Enter food here:"); contentPane.add(label2); label2.setBounds(new Rectangle(new Point(35, 100), label2.getPreferredSize())); contentPane.add(whatToAttack); whatToAttack.setBounds(35, 60, 140, 20); contentPane.setPreferredSize(new Dimension(400, 300)); pack(); setLocationRelativeTo(getOwner()); } private JButton startButton; private JLabel label1; private JTextField whatToEat; private JLabel label2; private JTextField whatToAttack; } } Edited December 14, 2014 by computor
Apaec Posted December 14, 2014 Posted December 14, 2014 try setVisible(false); in your event handler for start button also, for stuff like all of this: if (chosen.equals("Lobster")){ foodToEat = "Lobster"; } if (chosen.equals("Shark")){ foodToEat = "Shark"; } if (chosen.equals("Tuna")){ foodToEat = "Tuna"; } if (chosen.equals("Monkfish")){ foodToEat = "Monkfish"; } if (chosen.equals("Swordfish")){ foodToEat = "Swordfish"; } else{ } just do foodToEat = chosen;
Swizzbeat Posted December 14, 2014 Posted December 14, 2014 try setVisible(false); in your event handler for start button Because why disposes of resources.... 2
computor Posted December 14, 2014 Author Posted December 14, 2014 (edited) Still doesn't work, maybe I'm doing it wrong? If i remove the code: String chosen = whatToEat.getSelectedText().toString(); if (chosen.equals("Lobster")){ foodToEat = chosen; } if (chosen.equals("Shark")){ foodToEat = chosen; } if (chosen.equals("Tuna")){ foodToEat = chosen; } if (chosen.equals("Monkfish")){ foodToEat = chosen; } if (chosen.equals("Swordfish")){ foodToEat = chosen; } else{ } String chosen2 = whatToAttack.getSelectedText().toString(); if (chosen2.equals("Attack")){ skillDisplay = 1; } if (chosen2.equals("Strength")){ skillDisplay = 2; } if (chosen2.equals("Defence")){ skillDisplay = 3; } else{ } it closes the window when I hit start. Maybe the chosen and chosen2 stuff is wrong? Edited December 14, 2014 by computor