0

this is actually my first complete program which will eventually be used to write code for a robot I am making. Everything is working okay, except that when I run it, I have to drag open the window in order to see the content inside it.

Any suggestions on how to fix it. frame2 - "click to build file" works. frame - the one with the button grid, is the one I have to drag open to see.

Here is the setup file for frame (the one that doesn't work)

package Grid;

//march 13 to April 11
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;


@SuppressWarnings("serial")
public class ButtonGrid extends JFrame {
    public static int clicked[][] = new int[20][40];
    static JButton button[] = new JButton[800];
    static int x;
    static int count = 1;
    public static int clickedfinal[][];
    int value;

    public ButtonGrid() {

        JFrame frame = new JFrame();
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        GridLayout grid = new GridLayout(20, 40, 10, 8);
        frame.setLayout(grid);

        for (int c = 0; c < 20; c++) {
            for (int d = 0; d < 40; d++) {
                clicked[c][d] = 0;
            }
        }
        for (x = 0; x < 800; x++) {
            button[x] = new JButton();
            button[x].setActionCommand(Integer.toString(x));
            frame.add(button[x]);
            button[x].setBackground(Color.LIGHT_GRAY);
            button[x].setOpaque(true);

            thehandler handler = new thehandler();
            button[x].addActionListener(handler);
        }
    }

    public class thehandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            for (;;) {
                value = Integer.parseInt(e.getActionCommand());
                button[value].setBackground(Color.BLACK);

                int r = value % 40;
                int m = ((value - (value % 40)) / 40);
                // learn how to round up

                clicked[m][r] = 1;

                break;

            }

        }

    }

    public static void main(String[] args) {
        new ButtonGrid();

    }

}

Here is the file for frame2, the one that does work; To test just run this one.

package Grid;

import javax.swing.JButton;
import javax.swing.JFrame;

import Grid.ButtonGrid;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@SuppressWarnings("serial")
public class Arduinowriter extends JFrame {

    static int t = 1;
    static int buttonpressed;
    static int total;
    static String Code;
    static String oldcode;

    public Arduinowriter() {
        JFrame frame2 = new JFrame("Build File");
        JButton button = new JButton("Click to Build File");
        frame2.setSize(400, 400);
        frame2.add(button);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);

        button.setActionCommand(Integer.toString(1));
        thehandler handler = new thehandler();
        button.addActionListener(handler);

    }

    public class thehandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            buttonpressed = Integer.parseInt(e.getActionCommand());

            String newLine = System.getProperty("line.separator");
            String Motor2_half = "digitalWrite(Motor2, HIGH);" + newLine
                    + "delay(500)" + newLine + "digitalWrite(Motor2, LOW)";

            String Motor2_one = "digitalWrite(Motor2, HIGH);" + newLine
                    + "delay(1000);" + newLine + "digitalWrite(Motor2, LOW);";
            String Servo1_dispense = "digitalWrite(Servo1, HIGH);" + newLine
                    + "delay(1000)" + newLine + "digitalWrite(Servo1, LOW);";

            String Motor2_back = "digitalWrite(Motor2back, High" + newLine
                    + "delay(6000)" + newLine + "digitalWrite(Motor2back, Low)";

            String Motor1_forward = "digitalWrite(Motor1, HIGH);" + newLine
                    + "delay(1000);" + newLine + "digitalWrite(Motor1, LOW);";

            String dispenseCycle = (Motor2_one + newLine + Servo1_dispense
                    + " //Domino" + newLine);

            String skipCycle = (Motor2_one + newLine);

            String backCycle = (Motor1_forward + newLine + Motor2_back + newLine);

            while (buttonpressed == 1) {

                for (int x = 0; x < 20; x++) {

                    Code = oldcode + "//Line " + (x + 1);
                    oldcode = Code;
                    yloop: for (int y = 0; y < 40; y++) {

                        boolean empty = true;
                        for (int check = y; check < 39; check++) {

                            if (ButtonGrid.clicked[x][check] == 1) {
                                empty = false;
                                System.out.println(x + " not empty");
                            }
                        }

                        if (ButtonGrid.clicked[x][y] == 1 && y == 0) {
                            Code = oldcode + newLine + Servo1_dispense
                                    + " //Domino" + newLine;
                        } else if (ButtonGrid.clicked[x][y] == 1) {
                            Code = oldcode + newLine + dispenseCycle + newLine;
                        }

                        else if (ButtonGrid.clicked[x][y] == 0
                                && empty == false) {
                            Code = oldcode + newLine + skipCycle + newLine;
                        } else {
                            Code = oldcode + newLine + backCycle + newLine;
                            oldcode = Code;
                            break yloop;
                        }
                        oldcode = Code;

                    }
                }

                try {

                    BufferedWriter out = new BufferedWriter(new FileWriter(
                            "C:\\ArduinoCode.txt"));
                    out.write(Code);
                    out.close();
                } catch (IOException g) {
                    System.out.println("Exception ");

                }

                return;
            }

        }

    }

    // button

    public static void main(String[] args) {

        new ButtonGrid();
        new Arduinowriter();

    }
}

Note: I am a very beginner. This code has taken many many hours to write and I figured everything out using google and youtube. If anything in the code or what I have said is unclear or doesn't make sense, please forgive me and just ask.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2278269
  • 15
  • 1
  • 1
  • 6

1 Answers1

3

Invoke frame.setVisible(true) after you set the layout manager and everything else that affects its "visual" state.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
arynaq
  • 6,710
  • 9
  • 44
  • 74