8

Okay so what I have so far is a gui(In Java) and a program(In Python). I want the button in the gui when pressed to send the outputs to the python program and to run it. I then want the gui program to display the python print commands in the text box on the right side.

So, my question is, is this possible and how would I go about making it work?

GUI

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class dogedice extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JComboBox combo;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    dogedice frame = new dogedice();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public dogedice() {
        setTitle("DogeDice Bot");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.WEST);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        //Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
        JLabel userTag = new JLabel("Username:");
        GridBagConstraints gbc_userTag = new GridBagConstraints();
        gbc_userTag.insets = new Insets(0, 0, 0, 5);
        gbc_userTag.anchor = GridBagConstraints.EAST;
        gbc_userTag.gridx = 0;//Here are your x + y coords
        gbc_userTag.gridy = 0;//Adding to x moves left, adding to y moves down
        panel.add(userTag, gbc_userTag);

        //Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel startTag = new JLabel("Starting Bid:");
        GridBagConstraints gbc_startTag = new GridBagConstraints();
        gbc_startTag.insets = new Insets(0, 0, 0, 5);
        gbc_startTag.anchor = GridBagConstraints.EAST;
        gbc_startTag.gridx = 0;
        gbc_startTag.gridy = 2;
        panel.add(startTag, gbc_startTag);

        textField = new JTextField();
        GridBagConstraints gbc_textField3 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 2;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel multTag = new JLabel("Multiplier:");
        GridBagConstraints gbc_multTag = new GridBagConstraints();
        gbc_multTag.insets = new Insets(0, 0, 0, 5);
        gbc_multTag.anchor = GridBagConstraints.EAST;
        gbc_multTag.gridx = 0;
        gbc_multTag.gridy = 3;
        panel.add(multTag, gbc_multTag);

        textField = new JTextField();
        GridBagConstraints gbc_textField4 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 3;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel minTag = new JLabel("Min Remaining:");
        GridBagConstraints gbc_minTag = new GridBagConstraints();
        gbc_minTag.insets = new Insets(0, 0, 0, 5);
        gbc_minTag.anchor = GridBagConstraints.EAST;
        gbc_minTag.gridx = 0;
        gbc_minTag.gridy = 4;
        panel.add(minTag, gbc_minTag);

        textField = new JTextField();
        GridBagConstraints gbc_textField5 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 4;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        textField = new JTextField();
        GridBagConstraints gbc_textField2 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 1;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel passTag = new JLabel("Password:");
        GridBagConstraints gbc_passTag = new GridBagConstraints();
        gbc_passTag.insets = new Insets(0, 0, 0, 5);
        gbc_passTag.anchor = GridBagConstraints.EAST;
        gbc_passTag.gridx = 0;
        gbc_passTag.gridy = 1;
        panel.add(passTag, gbc_passTag);

        textField = new JTextField();
        GridBagConstraints gbc_textField6 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 5;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel oddsTag = new JLabel("Odds %:");
        GridBagConstraints gbc_oddsTag = new GridBagConstraints();
        gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
        gbc_oddsTag.anchor = GridBagConstraints.EAST;
        gbc_oddsTag.gridx = 0;
        gbc_oddsTag.gridy = 5;
        panel.add(oddsTag, gbc_oddsTag);

        textField = new JTextField();
        GridBagConstraints gbc_textField7 = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 6;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);

        //This is the Combo Box
        combo = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
        combo.addActionListener(this);
        GridBagConstraints gbc_list = new GridBagConstraints();
        gbc_list.fill = GridBagConstraints.HORIZONTAL;
        gbc_list.gridx = 1;
        gbc_list.gridy = 7;
        panel.add(combo, gbc_list);

        JLabel maxTag = new JLabel("MaxBet:");
        GridBagConstraints gbc_maxTag = new GridBagConstraints();
        gbc_maxTag.insets = new Insets(0, 0, 0, 5);
        gbc_maxTag.anchor = GridBagConstraints.EAST;
        gbc_maxTag.gridx = 0;
        gbc_maxTag.gridy = 6;
        panel.add(maxTag, gbc_maxTag);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.SOUTH);
        panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

        JButton btnConfirm = new JButton("Turn Up");
        panel_1.add(btnConfirm);


        JScrollPane scrollPane = new JScrollPane();//This will hold the information the bot sends over such as win/loose or error
        contentPane.add(scrollPane, BorderLayout.CENTER);
        JTextArea textArea = new JTextArea("Imput bot information here...");
        textArea.setColumns(20);
        scrollPane.setViewportView(textArea);

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == combo) {
            System.out.println(combo.getSelectedIndex()+1);
        }
    }

}

Python Program:

import random
import time

username = gtk.entry.get_text("usernameimput")
password = gtk.entry.get_text("passwordimput")

odds = gtk.entry.get_text("oddsimput")
RollBelow = odds
RollAbove = 100 - odds - 0.0001

currency = gtk.entry.get_text("currencyimput")

LowerStopLimit = gtk.entry.get_text("minimumimput")

Low = "0"
High = "1"

startbet = gtk.entry.get_text("startingbetimput")
multiplyer = gtk.entry.get_text("multiplierimput")
maxbet = gtk.entry.get_text("maxbetimput")
bet = startbet

key = -1
print "Logging in..."
while key == -1:
    try:
        key = peerbetapi.__login(username, password)
    except:
        key = -1
    if key == -1:
        print "Error logging in. Retrying..."
print "Logged in..."

result = peerbetapi.__roll_dice(key, High, RollAbove,  currency, bet, random.randint(1,999999999))
startbalance = float(result["user_balance"])
Stopbot = False
while Stopbot == False:
    try:
        if float(result["game_won"]) == 0:
            # we lost so multiply the bet
            bet = "%0.8f" % (float(bet) * float(multiplyer))
            if float(bet) > float(maxbet):
                print "*** MAX BET REACHED RESETING ***"
                bet = startbet
            print "Bet: " + bet
            print "Profit: %0.8f" % (float(result["user_balance"]) - startbalance)
        else:
            # we won so reset the bet to the start bet
            bet = startbet
            print "Bet: " + bet
            print "Profit: %0.8f" % (float(result["user_balance"]) - startbalance)
        if float(result["dice_roll"]) >= 50:
            result = peerbetapi.__roll_dice(key, Low, RollBelow, currency, bet, random.randint(1,999999999))
        else:
            result = peerbetapi.__roll_dice(key, High, RollAbove, currency, bet, random.randint(1,999999999))
        print "Balance: %3.4f" % float(result["user_balance"]) + " Rolled: %06.4f" % float(result["dice_roll"]) + " Target: " + str(result["dice_target"]) + " Won: " + str(result["game_won"])
        if float(result["user_balance"]) <= float(LowerStopLimit):
            Stopbot = True
    except:
        print "Error: Last multiplyer %0.4f" % float(bet)
        time.sleep(30)
    #time.sleep(1)
1
  • Search about Java and Python integration. Commented Jun 6, 2014 at 20:04

1 Answer 1

9

You can spawn a python process:

Process p = Runtime.getRuntime().exec("python "+yourpythonprog+" "+yourargs);

Then use the Process object to read the output of your python program.

Sign up to request clarification or add additional context in comments.

6 Comments

That's great, I'll try that but I'm not sure if I'll be able to work the code correctly. I have not been working with java for too long, just recently started.
Better to call exec() with an argument list, not a string that requires a shell to interpret.
How exactly would I go about doing that, as I said, not really skilled in java just yet.
Does anyone here feel like taking a try at maybe editing my code so that the two parts will send information back and forth? I can't seem to work it too well...
@michaeladair, "please fix my code" is a better thing for codereview.stackexchange.com, or better, a paid consulting site. We're here to build up a canonical set of questions and answers useful to others, not to provide free labor.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.