order of operation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: order of operation

  1. #1
    Join Date
    Feb 2005
    Posts
    10

    order of operation

    I have a gui that I am working on. I have a frame "myFrame" which i add a panel "centerPanel" to in the center of the BorderLayout. I want to add graphics to my center panel when i click on a button.
    It works fine when I create it all in my GUI construction:

    myPersonArray = new MyDraw[1];
    myPersonArray[0] = new MyDraw(50,Color.black);
    centerPanel.add(myPersonArray[0]);
    myFrame.getContentPane().add(BorderLayout.CENTER, centerPanel);

    But when I take the first three lines and put them into my ActionListener it doesn't work. I guess that I don't understand the order of operation. I don't want to have myPersonArray to have a limit until the user sets it in a JTextField and then presses the button to draw it.

    Could someone please help me?

    Thanks
    Jay

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    From this minimal code fragment it looks to me like you should make a JPanel extension
    and override its paint/update methods and use that as your centerPanel. This panel
    should have an ArrayList that stores the MyDraw objects. The MyDraw objects
    (whatever they are) should have a draw(Graphics g) method that renders the object.

    I have no idea what MyDraw is, so this is all I can say for now.
    eschew obfuscation

  3. #3
    Join Date
    Feb 2005
    Posts
    10
    Sorry, I never know how much code to write so that it makes sense. I also don't want to put down a whole bunch of pointless code too.
    Maybe this will make more sense:


    public class JavaTwo {
    //*****Global Variables*****
    JFrame myFrame; //create global frame name
    MyDraw[] myPersonArray;
    MyDraw rect;

    JComboBox sizeBox; //create global var for people size
    JComboBox speedBox; // speed of game
    JComboBox activeBox; //active people color
    JComboBox inactiveBox; //inactive people color
    JCheckBox soundControl; //sound on/off

    JTextField gridWidthField; //number of people in a row
    JTextField gridHeightField; //number of people in a column
    JComboBox numbTypeBox; //number type of people
    JCheckBox randStartControl; //create a random start on/off

    int gridWidth = 0; //Global variables for the grid constructor
    int gridHeight = 0;
    boolean gridRand = false;
    int gridNumType = 0;
    CA2D hisCode; //make instance of Code Class

    JPanel centerPanel;

    Color[] myColorArray = {Color.black,Color.black,Color.white,Color.blue,Color.green,Color.red,Color.yellow};//for choosing people color

    public static void main (String args[]) {
    JavaTwo myProgram = new JavaTwo(); //Make instance of GUI class
    myProgram.makeGui(); //tell instance to run the GUI method
    }

    //GUI Method
    void makeGui(){

    //*****Panels*****
    myFrame = new JFrame("Cellular Automata"); //create my program frame
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close program when close window
    JPanel southPanel = new JPanel(); //make panel for SOUTH
    JPanel eastPanel = new JPanel();
    eastPanel.setLayout(new BoxLayout(eastPanel,BoxLayout.Y_AXIS));
    JPanel eastBottomPanel1 = new JPanel(); //create panels to shrink size of text fields
    JPanel eastBottomPanel2 = new JPanel();

    centerPanel = new JPanel(); //Create a panel to hold people
    FlowLayout DrawLayout = new FlowLayout(); //create layout for panel
    DrawLayout.setHgap(0); //make sure the layout has no gaps between people next to each other
    DrawLayout.setVgap(0); //make sure the layout has no gaps between people above or below
    centerPanel.setLayout(DrawLayout); //give the panel the layout

    //****Buttons*****
    JButton StartButton = new JButton("Start"); //Make button
    StartButton.addActionListener(new StartListener()); //Tell the button I'm listening for events
    southPanel.add(StartButton); //add button to panel

    JButton StopButton = new JButton("Stop");
    StopButton.addActionListener(new StopListener());
    southPanel.add(StopButton);

    JButton ResetButton = new JButton("Reset");
    ResetButton.addActionListener(new ResetListener());
    southPanel.add(ResetButton);

    JLabel sizeName = new JLabel("Size"); //create name for box
    eastPanel.add(sizeName); //place name in panel
    String[] sizeCombo = {"Size","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"}; //list to put in size box
    sizeBox = new JComboBox(sizeCombo); //make size box
    sizeBox.addActionListener(new SizeListener()); //Tell it to listen
    eastPanel.add(sizeBox); //place box in panel

    JLabel speedName = new JLabel("Speed");
    eastPanel.add(speedName);
    String[] speedCombo = {"Speed","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"};
    speedBox = new JComboBox(speedCombo);
    speedBox.addActionListener(new SpeedListener());
    eastPanel.add(speedBox);

    JLabel activeColor = new JLabel("Active Color");
    eastPanel.add(activeColor);
    String[] activeCombo = {"Active Color","Black","White","Blue","Green","Red","Yellow"};
    activeBox = new JComboBox(activeCombo);
    activeBox.addActionListener(new ActiveListener());
    eastPanel.add(activeBox);

    JLabel inactiveName = new JLabel("Inactive Color");
    eastPanel.add(inactiveName);
    String[] inactiveCombo = {"Inactive Color","Black","White","Blue","Green","Red","Yellow"};
    inactiveBox = new JComboBox(inactiveCombo);
    inactiveBox.addActionListener(new InactiveListener());
    eastPanel.add(inactiveBox);

    JLabel numbTypeName = new JLabel("Number Type");
    eastPanel.add(numbTypeName);
    String[] numbTypeCombo = {"0","1","2","3","4","5","6","7","8","9","10"};
    numbTypeBox = new JComboBox(numbTypeCombo);
    numbTypeBox.addActionListener(new NumbTypeListener());
    eastPanel.add(numbTypeBox);

    randStartControl = new JCheckBox("Random Start");
    randStartControl.addItemListener(new RandStartListener());
    eastPanel.add(randStartControl);

    JLabel widthName = new JLabel("Width");
    eastBottomPanel1.add(widthName);
    gridWidthField = new JTextField(5);
    gridWidthField.addFocusListener(new GridWidthListener());
    eastBottomPanel1.add(gridWidthField);

    JLabel heightName = new JLabel("Height");
    eastBottomPanel2.add(heightName);
    gridHeightField = new JTextField(5);
    gridHeightField.addFocusListener(new GridHeightListener());
    eastBottomPanel2.add(gridHeightField);

    eastPanel.add(eastBottomPanel1); //add Width text panel to eastpanel
    eastPanel.add(eastBottomPanel2); //add height text panel to east panel

    soundControl = new JCheckBox("Sound");
    soundControl.addItemListener(new SoundListener());
    eastPanel.add(soundControl);



    myFrame.getContentPane().add(BorderLayout.SOUTH, southPanel);//add panel to frame
    myFrame.getContentPane().add(BorderLayout.CENTER, centerPanel); //put rect in the center
    myFrame.getContentPane().add(BorderLayout.EAST, eastPanel);


    myFrame.setSize(800,600); //set its size
    myFrame.setLocation(100,100); //set where it appears on screen
    myFrame.setVisible(true); //make visible
    }

    //****Button Events*****
    //Start button
    class StartListener implements ActionListener { //inner class for multiple ActionListeners
    public void actionPerformed(ActionEvent event){
    myPersonArray = new MyDraw[1];
    myPersonArray[0] = new MyDraw(50,Color.black);
    centerPanel.add(myPersonArray[0]);


    }
    }
    }

    class MyDraw extends JPanel{

    int rectSize = 20;
    Color rectColor;

    public MyDraw(int theSize, Color rectColor) {
    super();
    this.rectSize = theSize;
    this.rectColor = rectColor;
    setPreferredSize(new Dimension(theSize,theSize)); //create canvas to draw square.
    }

    public void paint(Graphics g) {

    Graphics2D myGraphic = (Graphics2D)g;
    myGraphic.setColor(rectColor);
    myGraphic.fill(new Rectangle2D.Float(0,0,rectSize,rectSize));



    }
    }

  4. #4
    Join Date
    Feb 2005
    Posts
    10
    Okay I got my problem. I needed to put centerPanel.validate() in my button to update the images in my panel.

    Thanks for your time.
    Jay

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Just for the record:

    As you have implemented this you really don't have to make a special
    MyDraw class. All you have to to is create a JPanel, set its background
    color and its preferred size, and you're there.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center