Java 使用JFrame时何时调用绘制方法?

Java 使用JFrame时何时调用绘制方法?,java,Java,听着,我对Java是个新手。在过去的五年里,我基本上是一名使用VBA的电子表格程序员。我知道编程逻辑,而不知道语言的复杂性。请帮帮我。课堂作业:编写一个程序,询问用户他们想建多少栋房子。询问用户是否希望在其房屋旁边包括一棵树。建造房屋和树木的数量。我的代码不起作用。如果我输入“1”作为房屋数量,它会打印2,任何超过2的数字,它会做一些奇怪的事情。tree函数似乎工作得很好,只是要构建的树的数量不好 package javasrc_jacob_loden; import javax.sw

听着,我对Java是个新手。在过去的五年里,我基本上是一名使用VBA的电子表格程序员。我知道编程逻辑,而不知道语言的复杂性。请帮帮我。课堂作业:编写一个程序,询问用户他们想建多少栋房子。询问用户是否希望在其房屋旁边包括一棵树。建造房屋和树木的数量。我的代码不起作用。如果我输入“1”作为房屋数量,它会打印2,任何超过2的数字,它会做一些奇怪的事情。tree函数似乎工作得很好,只是要构建的树的数量不好

package javasrc_jacob_loden;    

import javax.swing.JOptionPane;
import javax.swing.JFrame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.awt.Color;
import java.awt.Graphics; 
//import java.util.Scanner;
/*
 * 1)  Prompt user to enter the number of houses they would like to build using JOptionPane. Store as integer.
 * 2)  Ask user if they would like to include a tree next to their new houses.
 * 3)  Modules to load arrays with initial house coordinates.
 * 4)  Module to load arrays with initial tree coordinates.
 * 5)  Module to determine the size of an array.
 * 6)  Module to loop and load array with next house build coordinates.
 * 7)  Module to loop and load array with next tree build coordinates.  
 * 8)  Module to print the total number of houses built.
 */

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

    static int Roof[][] = new int[2][3];
    static int houseDimensions[][] = new int[6][4];
    static int windowFrame[][] = new int[4][2];
    static int tree[][] = new int[2][3];
    static int treeTrunk[][] = new int[2][4];
    static int treeYesNoResult;
    static int houses;
    boolean plantTree;

    public static void main(String[] args){
        String numOfHouses;
        //int houses;


            //1)  Prompt user to enter the number of houses they would like to build using JOptionPane. Store as integer.
            numOfHouses = JOptionPane.showInputDialog("How many houses would you like to build?");
                houses = Integer.parseInt(numOfHouses);

            //2)  Ask user if they would like to include a tree next to their new houses.
            int treeYesNo = JOptionPane.YES_NO_OPTION;
            treeYesNoResult = JOptionPane.showConfirmDialog(null,  "Would you like to plant trees next to your new houses?", "Question", treeYesNo);

            //3)Modules to load arrays with initial house coordinates.
            //Roof = loadInitialRoofDimensions(Roof);
            //houseDimensions = loadInitialHouseDimensions(houseDimensions);
            //windowFrame = loadInitialWindowFrameDimensions(windowFrame);
            //tree = loadInitialTree(tree);
            //treeTrunk = loadInitialTreeTrunk(treeTrunk);


            newNeighborhoodBuilder buildTheHouses = new newNeighborhoodBuilder();
            buildTheHouses.setVisible(true);

    }

            public void paint(Graphics canvas){
                //setVisible(true);
                //setSize(800, 400);
                //setDefaultCloseOperation(EXIT_ON_CLOSE);
                System.out.println(houses);
                //int i = 1;

            for(int i=0; i<houses; i++){
                if(plantTreeYesNo()==true){
                    canvas.setColor(Color.green);
                    canvas.fillPolygon(tree[0], tree[1], tree[0].length);
                    canvas.setColor(Color.ORANGE);
                    canvas.fillPolygon(treeTrunk[0], treeTrunk[1], treeTrunk[0].length);
                }


                    canvas.setColor(Color.blue);
                    canvas.fillPolygon(Roof[0], Roof[1], Roof[0].length);
                    canvas.setColor(Color.RED);
                    canvas.fillPolygon(houseDimensions[0], houseDimensions[1], houseDimensions[0].length);
                    canvas.setColor(Color.darkGray);
                    canvas.fillPolygon(houseDimensions[2], houseDimensions[3], houseDimensions[0].length);
                    canvas.setColor(Color.lightGray);
                    canvas.fillPolygon(houseDimensions[4], houseDimensions[5], houseDimensions[0].length);
                    canvas.setColor(Color.BLACK);
                    canvas.drawPolyline(houseDimensions[2],  houseDimensions[3],  houseDimensions[0].length);
                    canvas.drawPolyline(windowFrame[0],  windowFrame[1],  windowFrame[0].length);
                    canvas.drawPolyline(windowFrame[2],  windowFrame[3],  windowFrame[0].length);
                    canvas.drawPolygon(houseDimensions[4], houseDimensions[5], houseDimensions[0].length);

                        Roof = moveHouse(Roof);
                        houseDimensions = moveHouse(houseDimensions);
                        windowFrame = moveHouse(windowFrame);
                        tree = moveHouse(tree);
                        treeTrunk = moveHouse(treeTrunk);

            }
            //System.exit(0);


        }
            //Constructor
            public newNeighborhoodBuilder(){
                //setVisible(true);
                setSize(800, 400);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                Roof = loadInitialRoofDimensions(Roof);
                houseDimensions = loadInitialHouseDimensions(houseDimensions);
                windowFrame = loadInitialWindowFrameDimensions(windowFrame);
                tree = loadInitialTree(tree);
                treeTrunk = loadInitialTreeTrunk(treeTrunk);

                }



        //5)  Module to loop and load array with next house build coordinates.
        //6)  Module to loop and load array with next tree build coordinates. 
        public static int[][] moveHouse(int[][] movers){
            int[][] shiftX = new int[movers.length][movers[0].length];
            shiftX = movers;
            switch (shiftX.length){
            case 6:
                for(int i=0; i<shiftX[0].length; i++){
                    shiftX[0][i] = shiftX[0][i]+200;
                }
                for(int j=0; j<shiftX[0].length; j++){
                    shiftX[2][j] = shiftX[2][j]+200;
                }
                for(int k=0; k<shiftX[0].length; k++){
                    shiftX[4][k] = shiftX[4][k]+200;
                }
                break;
            case 4:
                for(int l=0; l<shiftX[0].length; l++){
                    shiftX[0][l] = shiftX[0][l]+200;
                }
                for(int m=0; m<shiftX[0].length; m++){
                    shiftX[2][m] = shiftX[2][m]+200;
                }
                break;
            case 2:
                for(int n=0; n<shiftX[0].length; n++){
                    shiftX[0][n] = shiftX[0][n]+200;
                }
                break;
            default:
                System.exit(0);

            }
            return shiftX;
        }
        public static boolean plantTreeYesNo(){
            if(treeYesNoResult==0){
                return true;
            }
            else{
                return false;
            }
        }
        //3)Modules to load arrays with initial house coordinates.
        public static int[][] loadInitialRoofDimensions(int[][] a){
            int[][] Roof = {{120, 180, 240},//x roof
                    {100, 80, 100}};// y roof
            a = Roof;
            return a;
        }



        public static int[][] loadInitialHouseDimensions(int[][] b){
            int houseDimensions[][] = {{130, 130, 230, 230},//x house box
                                        {160, 100, 100, 160},//y house box
                                        {155, 155, 180, 180},//x door
                                        {160, 120, 120, 160},//y door
                                        {200, 200, 220, 220},//x window
                                        {120, 140, 140, 120}};//y window
            b = houseDimensions;
            return b;
        }

        public static int[][] loadInitialWindowFrameDimensions(int[][] c){
            int windowFrame[][] = {{200, 220},//x horizontal window pane
                                    {130, 130},//y horizontal window pane
                                    {210, 210},//x vertical window pane
                                    {120, 140}};// y vertical window pane
            c = windowFrame;
            return c;
        }
        //4)Module to load arrays with initial tree coordinates.
        public static int[][] loadInitialTree(int[][] d){
            int tree[][] = {{250, 270, 290},//x tree green
                            {140, 80, 140}};//y tree green
            d = tree;
            return d;
        }

        public static int[][] loadInitialTreeTrunk(int[][] e){
            int treeTrunk[][] = {{265, 265, 275, 275},//x tree trunk
                                {160, 140, 140, 160}};//y tree trunk
            e = treeTrunk;
            return e;
        }
        //5)  Module to determine the size of an array.
        public static int arrayColumnSize(int[][] howBig){
            int howManyRows = howBig.length;
            List<Integer> howBigList = new ArrayList<Integer>();
            for (int i=0; i<howBig.length; i++) {
                 //fill howBigList
                 for (int j=0; j<howBig[i].length; j++) { 
                    howBigList.add(howBig[i][j]); 
                }
            }
          int rowCount = howBigList.size()/howManyRows;

        return rowCount;
        }


}

我问过google和JavaAPI,但得到的结果相互矛盾……paint到底什么时候执行?我如何控制执行?这是一个非常粗略的草案…我在最后有一个我不需要的方法。事实证明,我可以通过询问数组[0]来找到2d数组中的列数。长度…现在没有。