Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Panel Problems?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Panel Problems?

    Hello there, I'm very new to JAVA programming, and I'm currently enrolled in a JAVA programming class at College. I've gotten through most of our other projects fine (mainly because they were very simple) but this time we were given a more complex program to work with. In this program, there were originally 3 buttons labeled Circle, 3d Rectangle, and Rounded Rectangle. What we have to do as students is to add 3 additional buttons to the panel BELOW the 3 existing buttons. When I try to do this, all of the buttons end up coming out on one line even if I set the # of rows to 2 and the # of columns to 3. I've tried other variations for the rows/columns but they always end up only being on one line. I've even tried creating a new panel for the three new buttons but it still places all of the buttons on one line. Could someone point out what I'm doing wrong? After making the 2 row panel, I've also got to make the buttons work, as well as add a clear button.

    Also the program comes in two parts, the first part is named Draw.Java, and the second is named Draw Canvas.Java.

    /******************************************************************
     *  COURSE:             CSC231 Computer Science and Programming II
     *	Lab:			    Number 4
     *	FILE:				Draw.java
     *	TARGET:				Java 5.0 and 6.0
     *****************************************************************/
     
    // Import Core Java packages
    import java.awt.*;
    import java.awt.event.*;
     
    public class Draw extends Frame implements ActionListener, ItemListener {
     
    	// Initial Frame size
    	static final int WIDTH = 400;                // frame width
    	static final int HEIGHT = 300;               // frame height
     
        // Color choices
        static final String COLOR_NAMES[] = {"None", "Red", "Blue", "Green"};
        static final Color COLORS[] = {null, Color.red, Color.blue, Color.green};
     
        // Button control
        Button circle;
        Button roundRec;
        Button threeDRec;
        Button square;
        Button oval;
        Button line;
     
        // Color choice box
        Choice colorChoice;
     
        // the canvas
        DrawCanvas canvas;
     
        /**
         * Constructor
         */
    	public Draw() {
    	    super("Java Draw");
            setLayout(new BorderLayout());
     
            // create panel for controls
            Panel topPanel = new Panel(new GridLayout(2, 3));
            add(topPanel, BorderLayout.PAGE_START);
     
            // create button control
            Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
            topPanel.add(buttonPanel);
            circle = new Button("Circle");
            buttonPanel.add(circle);
            roundRec = new Button("Rounded Rectangle");
            buttonPanel.add(roundRec);
            threeDRec = new Button("3D Rectangle");
            buttonPanel.add(threeDRec);
            square = new Button("Square");
            buttonPanel.add(square);
            oval = new Button("Oval");
            buttonPanel.add(oval);
            line = new Button("Line");
            buttonPanel.add(line);
     
            // add button listener
            circle.addActionListener(this);
            roundRec.addActionListener(this);
            threeDRec.addActionListener(this);
            square.addActionListener(this);
            oval.addActionListener(this);
            line.addActionListener(this);
     
            // create panel for color choices
            Panel colorPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
            topPanel.add(colorPanel);
            Label label = new Label("Filled Color:");
            colorPanel.add(label);
            colorChoice = new Choice();
            for(int i=0; i<COLOR_NAMES.length; i++) {
                colorChoice.add(COLOR_NAMES[i]);
            }
            colorPanel.add(colorChoice);
            colorChoice.addItemListener(this);
     
            // create the canvas
            canvas = new DrawCanvas();
            add(canvas, BorderLayout.CENTER);
    	} // end of constructor
     
     
        /**
         *  Implementing ActionListener
         */
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == circle) {  // circle button
                canvas.setShape(DrawCanvas.CIRCLE);
            }
            else if(event.getSource() == roundRec) {  // rounded rectangle button
                canvas.setShape(DrawCanvas.ROUNDED_RECTANGLE);
            }
            else if(event.getSource() == threeDRec) { // 3D rectangle button
                canvas.setShape(DrawCanvas.RECTANGLE_3D);
            }
            else if(event.getSource() == square) { // square button
                canvas.setShape(DrawCanvas.SQUARE);
            }
            else if(event.getSource() == oval) { // oval button
                canvas.setShape(DrawCanvas.OVAL);
            }
            else if(event.getSource() == line) { // line button
                canvas.setShape(DrawCanvas.LINE);
            }
        }
     
        /**
         * Implementing ItemListener
         */
        public void itemStateChanged(ItemEvent event) {
            Color color = COLORS[colorChoice.getSelectedIndex()];
            canvas.setFilledColor(color);
        }
     
        /**
         * the main method
         */
        public static void main(String[] argv) {
            // Create a frame
            Draw frame = new Draw();
            frame.setSize(WIDTH, HEIGHT);
            frame.setLocation(150, 100);
     
            // add window closing listener
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent event) {
                    System.exit(0);
                }
            });
     
            // Show the frame
            frame.setVisible(true);
        }
    }


    Second Part
    /******************************************************************
     *  COURSE:             CSC231 Computer Science and Programming II
     *	Lab:			    Number 4
     *	FILE:				DrawCanvas.java
     *	TARGET:				Java 5.0 and 6.0
     *****************************************************************/
     
    // Import Core Java packages
    import java.awt.*;
    import java.awt.event.*;
     
    public class DrawCanvas extends Canvas implements MouseListener,
                                                      MouseMotionListener {
     
        // Constants for shapes
        public static final int CIRCLE = 1;
        public static final int ROUNDED_RECTANGLE = 2;
        public static final int RECTANGLE_3D = 3;
        public static final int SQUARE = 4;
        public static final int OVAL = 5;
        public static final int LINE = 6;
     
        // Coordinates of points to draw
        private int x1, y1, x2, y2;
     
        // shape to draw
        private int shape = CIRCLE;
        /**
         * Method to set the shape
         */
        public void setShape(int shape) {
            this.shape = shape;
        }
     
        // filled color
        private Color filledColor = null;
        /**
         * Method to set filled color
         */
        public void setFilledColor(Color color) {
            filledColor = color;
        }
     
        /**
         * Constructor
         */
    	public DrawCanvas() {
    	    addMouseListener(this);
    	    addMouseMotionListener(this);
    	} // end of constructor
     
        /**
         * painting the component
         */
        public void paint(Graphics g) {
     
            // the drawing area
            int x, y, width, height;
     
            // determine the upper-left corner of bounding rectangle
            x = Math.min(x1, x2);
            y = Math.min(y1, y2);
     
            // determine the width and height of bounding rectangle
            width = Math.abs(x1 - x2);
            height = Math.abs(y1 - y2);
     
            if(filledColor != null)
                g.setColor(filledColor);
            switch (shape) {
                case ROUNDED_RECTANGLE :
                    if(filledColor == null)
                        g.drawRoundRect(x, y, width, height, width/4, height/4);
                    else
                        g.fillRoundRect(x, y, width, height, width/4, height/4);
                    break;
                case CIRCLE :
                    int diameter = Math.max(width, height);
                    if(filledColor == null)
                        g.drawOval(x, y, diameter, diameter);
                    else
                        g.fillOval(x, y, diameter, diameter);
                    break;
                case RECTANGLE_3D :
                    if(filledColor == null)
                        g.draw3DRect(x, y, width, height, true);
                    else
                        g.fill3DRect(x, y, width, height, true);
                    break;
            }
        }
     
        /**
         * Implementing MouseListener
         */
        public void mousePressed(MouseEvent event) {
            x1 = event.getX();
            y1 = event.getY();
        }
     
        public void mouseReleased(MouseEvent event) {
            x2 = event.getX();
            y2 = event.getY();
            repaint();
        }
     
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
     
        /**
         * Implementing MouseMotionListener
         */
        public void mouseDragged(MouseEvent event) {
            x2 = event.getX();
            y2 = event.getY();
            repaint();
        }
     
        public void mouseMoved(MouseEvent e) {}
    }
    Last edited by Dellick17; October 22nd, 2010 at 01:52 PM. Reason: forgot highlight


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,355
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Panel Problems?

    See the following: A Visual Guide to Layout Managers . Using the appropriate layout for the JPanel will give you much more control on where things are displayed.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Panel Problems?

    Quote Originally Posted by copeg View Post
    See the following: A Visual Guide to Layout Managers . Using the appropriate layout for the JPanel will give you much more control on where things are displayed.
    Oh wow, I just got it to work by changing it into a Grid Layout. I feel really dumb now, was working on that for more than an hour....

    Thank you.

Similar Threads

  1. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 08:15 PM
  2. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 06:31 PM
  3. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 05:00 PM
  4. data mapping betweeb different collapsible panel
    By varuntcs in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 23rd, 2010, 03:33 PM
  5. suggestionbox inside modal panel
    By smackdown90 in forum Web Frameworks
    Replies: 1
    Last Post: April 8th, 2010, 02:16 PM

Tags for this Thread