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) {} }