Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default basic shape compose gui

    hello.

    I am working on a basic shape composition application.

    this is my first post, apologies for all my newbyness..

    i've got some things working, but so far not very well.

    anybody so inclined to offer help of any sort is appreciated.

    things I've been working on but still haven't figured out -

    - a triangle..! polygons have not been my friends

    - adding the arraylist Shape items to the JList for display and selection for editing

    - properly implementing the textfields to take configurable location and size coordinates. the default size selector under the options menu adds shapes, but the PAGE_END JTextField option doesn't work.

    - o yeah, almost forgot the shape outline color aspect. still working on that too..

    thanks in advance

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    import java.io.*;
    
    public class CompShapes  
    {
    
       public static void main(String[] args) 
       {
         new CompShapes();
       }
    
       // Fields for CompShapes, View, class -------------------------------------------
       // Menu forms for shape characteristics
       private JMenuBar menubar;
       private JCheckBoxMenuItem defaultShpSz;     
       private JCheckBoxMenuItem addBorderedShapes;    
       private JRadioButtonMenuItem red, green, blue,      
                            cyan, magenta, yellow,  
                            black, gray, white, clear;
       private JMenu background;
       
       // Popup menu form for editing shapes, 
       // accessed either by rt-clicking or option-clicking, depending on OS   
       private JPopupMenu popup;  
       
       // Text listing of shapes on display
       private JList shapeList;
       
       // Form fields for ht, wdth, x (horizontal), and y (vertical) attributes
       private JPanel form;  
       private JTextField height;
       private JTextField width;
       private JTextField xPt;
       private JTextField yPt; 
      
       private JLabel heightLabel;
       private JLabel widthLabel;
       private JLabel horizLabel;
       private JLabel vertLabel;
       
       private int heightInt;
       private int widthInt;
       private int xPtInt;
       private int yPtInt;
       
       // Canvas for displaying shapes and program Control event handlers
       // ShapeCanvas is a JPanel class, inner classed below
       private ShapeCanvas canvas;
       
       // end fields for CompShapes class ---------------------------------------------
       
      /** 
       * CompShapes, View, constructor
       * 
       */ 
       public CompShapes() 
       {  
         // Window frame
         JFrame win = new JFrame("Shape Composer");
         
         // Text listing of shapes on display 
         shapeList = new JList();
          
         // Canvas for displaying shapes and program Control event handlers 
         canvas = new ShapeCanvas();
         
         // Listing of shapes GUI layout compositioning
         shapeList.setPreferredSize(new Dimension(200, 400));
         
         // Form field instantiations for ht, wdth, x (horizontal), and y (vertical) attributes
         height = new JTextField(2);
         width = new JTextField(2);
         xPt = new JTextField(2);
         yPt = new JTextField(2);
         heightLabel = new JLabel("Height:");
         widthLabel = new JLabel("Width:");
         horizLabel = new JLabel("Horizontal:");
         vertLabel = new JLabel("Vertical:");    
         form = new JPanel();
         form.add(heightLabel);
         form.add(height);
         form.add(widthLabel);
         form.add(width);
         form.add(horizLabel);
         form.add(xPt);
         form.add(vertLabel);
         form.add(yPt);
         
         // Menubar items provide additional format functionality
         menubar = new JMenuBar();
         
         // Primary menubar options
         JMenu fileMenu = new JMenu("File");
         menubar.add(fileMenu);
         JMenu addShapeMenu = new JMenu("Add Shape");
         menubar.add(addShapeMenu);
         JMenu shapeColorMenu = new JMenu("Fill Color");
         menubar.add(shapeColorMenu);
         JMenu shapeColorOutMenu = new JMenu("Outline Color");
         menubar.add(shapeColorOutMenu);
         JMenu optionsMenu = new JMenu("Options");
         menubar.add(optionsMenu);
    
         // File menu options not currently fully functional - 03.23.09
         JMenuItem newCmd = new JMenuItem("New");
         newCmd.addActionListener(canvas);
         fileMenu.add(newCmd);
         JMenuItem openCmd = new JMenuItem("Open...");
         openCmd.addActionListener(canvas);
         fileMenu.add(openCmd);
         JMenuItem saveCmd = new JMenuItem("Save...");
         saveCmd.addActionListener(canvas);
         fileMenu.add(saveCmd);
         JMenuItem quitCmd = new JMenuItem("Quit");
         quitCmd.addActionListener(canvas);
         fileMenu.add(quitCmd);      
         
         // Shape choices
         JMenuItem rect = new JMenuItem("Rectangle");
         addShapeMenu.add(rect);
         rect.addActionListener(canvas);
         JMenuItem oval = new JMenuItem("Oval");
         addShapeMenu.add(oval);
         oval.addActionListener(canvas);
         // Triangle currently not functional - 03.23.09
         JMenuItem triangle = new JMenuItem("Triangle");
         addShapeMenu.add(triangle);
         triangle.addActionListener(canvas);
         JMenuItem roundrect = new JMenuItem("Round Rect");
         addShapeMenu.add(roundrect);
         roundrect.addActionListener(canvas);
         
         // Menu button group for shape outline colors
         ButtonGroup outLnColorGrp = new ButtonGroup();
         red = new JRadioButtonMenuItem("Red");
         shapeColorOutMenu.add(red);
         outLnColorGrp.add(red);
         red.setSelected(true);
         green = new JRadioButtonMenuItem("Green");
         shapeColorOutMenu.add(green);
         outLnColorGrp.add(green);
         blue = new JRadioButtonMenuItem("Blue");
         shapeColorOutMenu.add(blue);
         outLnColorGrp.add(blue);
         cyan = new JRadioButtonMenuItem("Cyan");
         shapeColorOutMenu.add(cyan);
         outLnColorGrp.add(cyan);
         magenta = new JRadioButtonMenuItem("Magenta");
         shapeColorOutMenu.add(magenta);
         outLnColorGrp.add(magenta);
         yellow = new JRadioButtonMenuItem("Yellow");
         shapeColorOutMenu.add(yellow);
         outLnColorGrp.add(yellow);
         black = new JRadioButtonMenuItem("Black");
         shapeColorOutMenu.add(black);
         outLnColorGrp.add(black);
         gray = new JRadioButtonMenuItem("Gray");
         shapeColorOutMenu.add(gray);
         outLnColorGrp.add(gray);
         white = new JRadioButtonMenuItem("White");
         shapeColorOutMenu.add(white);
         outLnColorGrp.add(white);
          
         // Menu button group for shape fill colors
         ButtonGroup fillColorGrp = new ButtonGroup();
         red = new JRadioButtonMenuItem("Red");
         shapeColorMenu.add(red);
         fillColorGrp.add(red);
         red.setSelected(true);
         green = new JRadioButtonMenuItem("Green");
         shapeColorMenu.add(green);
         fillColorGrp.add(green);
         blue = new JRadioButtonMenuItem("Blue");
         shapeColorMenu.add(blue);
         fillColorGrp.add(blue);
         cyan = new JRadioButtonMenuItem("Cyan");
         shapeColorMenu.add(cyan);
         fillColorGrp.add(cyan);
         magenta = new JRadioButtonMenuItem("Magenta");
         shapeColorMenu.add(magenta);
         fillColorGrp.add(magenta);
         yellow = new JRadioButtonMenuItem("Yellow");
         shapeColorMenu.add(yellow);
         fillColorGrp.add(yellow);
         black = new JRadioButtonMenuItem("Black");
         shapeColorMenu.add(black);
         fillColorGrp.add(black);
         gray = new JRadioButtonMenuItem("Gray");
         shapeColorMenu.add(gray);
         fillColorGrp.add(gray);
         white = new JRadioButtonMenuItem("White");
         shapeColorMenu.add(white);
         fillColorGrp.add(white);
         clear = new JRadioButtonMenuItem("Clear");
         shapeColorMenu.add(clear);
         fillColorGrp.add(clear);
    
         JMenuItem clear = new JMenuItem("Clear");
         clear.addActionListener(canvas);
         optionsMenu.add(clear);
         optionsMenu.addSeparator();  // Add a separating line to the menu.
          
         defaultShpSz = new JCheckBoxMenuItem("Default Size");
         defaultShpSz.setSelected(true);
         optionsMenu.add(defaultShpSz);
         addBorderedShapes = new JCheckBoxMenuItem("Add Shapes with Border");
         addBorderedShapes.setSelected(true);
         optionsMenu.add(addBorderedShapes);
         optionsMenu.addSeparator();
          
         background = new JMenu("Background Color");
         optionsMenu.add(background);
         background.add("Red").addActionListener(canvas);
         background.add("Green").addActionListener(canvas);
         background.add("Blue").addActionListener(canvas);
         background.add("Cyan").addActionListener(canvas);
         background.add("Magenta").addActionListener(canvas);
         background.add("Yellow").addActionListener(canvas);
         background.add("Black").addActionListener(canvas);
         background.add("Gray").addActionListener(canvas);
         background.add("White").addActionListener(canvas);
          
         // Popup menu form for editing shapes, 
         // accessed either by rt-clicking or option-clicking, depending on OS        
         popup = new JPopupMenu();
         popup.add("Delete Shape").addActionListener(canvas);
         popup.add("Bring to Front").addActionListener(canvas);
         popup.addSeparator();
         popup.add("Make Larger").addActionListener(canvas);
         popup.add("Make Smaller").addActionListener(canvas);
         popup.addSeparator();
         popup.add("Add Black Border").addActionListener(canvas);
         popup.add("Remove Black Border").addActionListener(canvas);
         popup.addSeparator();
         popup.add("Set Color to Red").addActionListener(canvas);
         popup.add("Set Color to Green").addActionListener(canvas);
         popup.add("Set Color to Blue").addActionListener(canvas);
         popup.add("Set Color to Cyan").addActionListener(canvas);
         popup.add("Set Color to Magenta").addActionListener(canvas);
         popup.add("Set Color to Yellow").addActionListener(canvas);
         popup.add("Set Color to Black").addActionListener(canvas);
         popup.add("Set Color to Gray").addActionListener(canvas);
         popup.add("Set Color to White").addActionListener(canvas);
         popup.add("Empty out the fill color").addActionListener(canvas);
         
         BorderLayout layout = new BorderLayout(0,0); 
         win.setJMenuBar(menubar);
         win.setLayout(new BorderLayout());
         win.add(canvas, BorderLayout.CENTER);
         win.add(shapeList, BorderLayout.WEST);
         win.add(form, BorderLayout.SOUTH);
         win.pack();    
         win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
         win.setLocation(30,80);
         win.setSize(800,500);
         win.setVisible(true);
          
       } // end constructor     
    
       public class ShapeCanvas extends JPanel
                         implements ActionListener, MouseListener, MouseMotionListener 
       {
          ArrayList<Shape> shapes = new ArrayList<Shape>(); 
          // holds a list of the shapes that are displayed on the canvas
          
          ShapeCanvas() 
          {  
            // set up listeners to respond to mouse actions
            setBackground(Color.gray);
            addMouseListener(this);
            addMouseMotionListener(this);
          }   
    
          public void paintComponent(Graphics g) 
          {
               // In the paint method, all the shapes in ArrayList are
               // copied onto the canvas.
             super.paintComponent(g);  // First, fill with background color.
             int count = shapes.size();
             for (int i = 0; i < count; i++) 
             {
                Shape s = (Shape)shapes.get(i);
                s.draw(g);
             }
          } 
          
          public int getHt()
          {
            try
            {
              //gets the int out of the textfield
              heightInt = Integer.valueOf(height.getText()).intValue();
            } catch(NumberFormatException e)
              {
                //if invalid, sets to -1
                heightInt = -1;
              }
        
            return heightInt;
          }
          
          
          public int getWdth()
          {
            try
            {
               //gets the int out of the textfield
               widthInt = Integer.valueOf(width.getText()).intValue();
            } catch(NumberFormatException e)
              {
                //if invalid, sets to -1
                widthInt = -1;
              }
        
            return widthInt;
          }
       
          public int getHoriz()
          {
            try
            {
              //gets the int out of the textfield
              xPtInt = Integer.valueOf(xPt.getText()).intValue();
            } catch(NumberFormatException e)
              {
                //if invalid, sets to -1
                xPtInt = -1;
              }
        
            return xPtInt;
          } 
      
          public int getVert()
          {
            try
            {
              //gets the int out of the textfield
              yPtInt = Integer.valueOf(yPt.getText()).intValue();
            } catch(NumberFormatException e)
              {
                //if invalid, sets to -1
                yPtInt = -1;
              }
        
            return yPtInt;
          }
    
          public void actionPerformed(ActionEvent evt) 
          {
                 // Called to respond to action events from the
                 // menus or pop-up menu.
             String command = evt.getActionCommand();
             if (command.equals("New")) 
             {
                shapes.clear();  // Remove all items
                setBackground(Color.white);  // Clear to white.
             }
             else if (command.equals("Clear")) 
             {
                shapes.clear(); // Remove all items from the ArrayList
                repaint(); // Clear to current color.
             }        
             else if (command.equals("Triangle"))
                addShape(new TriangleShape());
             else if (command.equals("Rectangle"))
                addShape(new RectShape());
             else if (command.equals("Oval"))
                addShape(new OvalShape());
             else if (command.equals("Round Rect"))
                addShape(new RoundRectShape());
             
             else if (command.equals("Red"))
                setBackground(Color.red);
             else if (command.equals("Green"))
                setBackground(Color.green);
             else if (command.equals("Blue"))
                setBackground(Color.blue);
             else if (command.equals("Cyan"))
                setBackground(Color.cyan);
             else if (command.equals("Magenta"))
                setBackground(Color.magenta);
             else if (command.equals("Yellow"))
                setBackground(Color.yellow);
             else if (command.equals("Black"))
                setBackground(Color.black);
             else if (command.equals("Gray"))
                setBackground(Color.gray);
             else if (command.equals("White"))
                setBackground(Color.white);
             else if (clickedShape != null) 
             {
                // Process a command from the pop-up menu.
                if (command.equals("Delete Shape"))
                   shapes.remove(clickedShape);
                else if (command.equals("Bring to Front")) 
                {
                   shapes.remove(clickedShape);
                   shapes.add(clickedShape);  
                }
                else if (command.equals("Make Larger"))
                   clickedShape.setSizeLgr();
                else if (command.equals("Make Smaller"))
                   clickedShape.setSizeSmlr();
                
                else if (command.equals("Add Black Border"))
                   clickedShape.setDrawOutline(true);
                else if (command.equals("Remove Black Border"))
                   clickedShape.setDrawOutline(false);
                else if (command.equals("Set Color to Red"))
                   clickedShape.setColor(Color.red);
                else if (command.equals("Set Color to Green"))
                   clickedShape.setColor(Color.green);
                else if (command.equals("Set Color to Blue"))
                   clickedShape.setColor(Color.blue);
                else if (command.equals("Set Color to Cyan"))
                   clickedShape.setColor(Color.cyan);
                else if (command.equals("Set Color to Magenta"))
                   clickedShape.setColor(Color.magenta);
                else if (command.equals("Set Color to Yellow"))
                   clickedShape.setColor(Color.yellow);
                else if (command.equals("Set Color to Black"))
                   clickedShape.setColor(Color.black);
                else if (command.equals("Set Color to Gray"))
                   clickedShape.setColor(Color.gray);
                else if (command.equals("Set Color to White"))
                   clickedShape.setColor(Color.white);
                else if (command.equals("Empty out the fill color"))
                   clickedShape.setColor(this.getBackground());
                repaint();
             }
          } // end actionPerformed() method
    
          void addShape(Shape shape)
          {
             if (red.isSelected())
                shape.setColor(Color.red);
             else if (blue.isSelected())
                shape.setColor(Color.blue);
             else if (green.isSelected())
                shape.setColor(Color.green);
             else if (cyan.isSelected())
                shape.setColor(Color.cyan);
             else if (magenta.isSelected())
                shape.setColor(Color.magenta);
             else if (yellow.isSelected())
                shape.setColor(Color.yellow);
             else if (black.isSelected())
                shape.setColor(Color.black);
             else if (white.isSelected())
                shape.setColor(Color.white);
             else
                shape.setColor(Color.gray);
             
             shape.setDrawOutline( addBorderedShapes.isSelected() );
             if (defaultShpSz.isSelected())
                shape.setBounds(203,3,100,60);
             else
                shape.setBounds(xPtInt, yPtInt, widthInt, heightInt);
             shapes.add(shape);
             repaint();
             System.out.println(shapes);
          } // end addShape() method
          
          Shape clickedShape = null;  // This is the shape that the user clicks on.
          Shape draggedShape = null;  // This is null unless a shape is being dragged.
    
          int prevDragX;  // During dragging, these record the x and y coordinates of the
          int prevDragY;  //    previous position of the mouse.
          
          public void mousePressed(MouseEvent evt)
          {
             if (draggedShape != null) 
             {
                return;
             }
             int x = evt.getX();  // x-coordinate of point where mouse was clicked
             int y = evt.getY();  // y-coordinate of point 
             
             clickedShape = null;  // This will be set to the clicked shape, if any.
             for ( int i = shapes.size() - 1; i >= 0; i-- ) 
             {  
                    // Check shapes from front to back.
                Shape s = (Shape)shapes.get(i);
                if (s.containsPoint(x,y)) 
                {
                   clickedShape = s;
                   break;
                }
             }
             if (clickedShape == null) 
             {
                   // The user did not click on a shape.
                return;
             }
             else if (evt.isPopupTrigger()) 
             {
                // Show the pop-up menu
                popup.show(this,x-10,y-2);
             }
             else if (evt.isShiftDown()) 
             {
                // Z-axis - Bring the clicked shape to the front
                shapes.remove(clickedShape);
                shapes.add(clickedShape);
                repaint();
             }
             else 
             {
                // Start dragging the shape.
                draggedShape = clickedShape;
                prevDragX = x;
                prevDragY = y;
             }
             System.out.println("You pressed at (" + x + ", " + y + ")");
          } // end mousePressed() method
    
          public void mouseDragged(MouseEvent evt) 
          {
                 // User has moved the mouse.  Move the dragged shape by the same amount.
             if (draggedShape == null) 
             {
                    // User did not click a shape.  There is nothing to do.
                return;
             }
             int x = evt.getX();
             int y = evt.getY();
             draggedShape.moveBy(x - prevDragX, y - prevDragY);
             prevDragX = x;
             prevDragY = y;
    
             repaint();      // redraw canvas to show shape in new position
    //         System.out.println("Mouse dragged to (" + x + ", " + y + ")");
          }  // end mouseDragged() method
    
          public void mouseReleased(MouseEvent evt) 
          {
             // User has released the mouse.  Move the dragged shape, and set
             // draggedShape to null to indicate that dragging is over.
             // If the shape lies completely outside the canvas, remove it
             // from the list of shapes (since there is no way to ever move
             // it back on screen).  However, if the event is a popup trigger
             // event, then show the popup menu instead.
             if (draggedShape == null) 
             {
                return;
             }
             int x = evt.getX();
             int y = evt.getY();
             
             if (evt.isPopupTrigger()) 
             {
                // Check whether the user is trying to pop up a menu.
                // (This should be checked in both the mousePressed() and
                // mouseReleased() methods.)
                popup.show(this,x-10,y-2);
             }
             else 
             {
                draggedShape.moveBy(x - prevDragX, y - prevDragY);
                if ( draggedShape.left >= getSize().width || draggedShape.top >= getSize().height ||
                        draggedShape.left + draggedShape.width < 0 ||
                        draggedShape.top + draggedShape.height < 0 ) 
                {  // shape is off-screen
                   shapes.remove(draggedShape);  // remove shape from list of shapes
                }
                repaint();
             }
             draggedShape = null;  // Dragging is finished.
             System.out.println("Mouse released at (" + x + ", " + y + ")"); 
          } // end mouseReleased() method
    
          public void mouseEntered(MouseEvent evt) { }  
          public void mouseExited(MouseEvent evt) { }    
          public void mouseMoved(MouseEvent evt) { }
          public void mouseClicked(MouseEvent evt) { }
    
       }  // end class ShapeCanvas
    
       abstract class Shape
       {
          int left, top;      
          int width, height;  // Size of the bounding rectangle.
          Color color = Color.white;  // Color of this shape.
          boolean drawOutline;  // If true, a black border is drawn on the shape
    
          void setBounds(int left, int top, int width, int height)
          {
             // Set the position and size of this shape.
             this.left = left;
             this.top = top;
             this.width = width;
             this.height = height;
          }
          
          void setSizeLgr() 
          {
             // Set the size of this shape
             this.width = width+15;
             this.height = height+15;
          }
          
          void setSizeSmlr() 
          {
                // Set the size of this shape
             this.width = width-15;
             this.height = height-15;
          }
    
          void moveBy(int dx, int dy) 
          {
             // Move the shape by dx pixels horizontally and dy pixels vertically
             // (by changing the position of the top-left corner of the shape).
             left += dx;
             top += dy;
          }
    
          void setColor(Color color) 
          {
             // Set the color of this shape
             this.color = color;
          }
          
          void setDrawOutline(boolean draw) 
          {
             // If true, a black outline is drawn around this shape.
             drawOutline = draw;
          }
    
          boolean containsPoint(int x, int y) 
          {
             // Check whether the shape contains the point (x,y).
             // By default, this just checks whether (x,y) is inside the
             // rectangle that bounds the shape.  This method should be
             // overridden by a subclass if the default behavior is not
             // appropriate for the subclass.
             if (x >= left && x < left+width && y >= top && y < top+height)
                return true;
             else
                return false;
          }
    
          abstract void draw(Graphics g);  
    
       }  // end of class Shape
    
       class RectShape extends Shape 
       {
          void draw(Graphics g) 
          {
             g.setColor(color);
             g.fillRect(left,top,width,height);
             if (drawOutline) 
             {
                g.setColor(Color.black);
                g.drawRect(left,top,width,height);
             }
          }
       } // end RectShape class
       
       class TriangleShape extends Shape 
       {
          Point p1 = new Point(top, height);
          Point p2 = new Point(width/2, top);
          Point p3 = new Point(width, height);
    
          int[] xs = { p1.x, p2.x, p3.x };
          int[] ys = { p1.y, p2.y, p3.y };
          Polygon triangle = new Polygon(xs, ys, xs.length);
          // This class represents Triangle shapes.
          void draw(Graphics g) 
          {
    //        Point p1 = new Point(top, height);
    //        Point p2 = new Point(width/2, top);
    //        Point p3 = new Point(width, height);
    //
    //        int[] xs = { p1.x, p2.x, p3.x };
    //        int[] ys = { p1.y, p2.y, p3.y };
    //        Polygon triangle = new Polygon(xs, ys, xs.length);
            g.setColor(color);
            g.fillPolygon(triangle);
            if (drawOutline) 
            {
              g.setColor(Color.black);
              g.drawPolygon(triangle);
            }
          }
          
          boolean containsPoint(int x, int y) 
          {       
            if (triangle.contains(x,y))
               return true;
            else
               return false;
          }
          
          void moveBy(int dx, int dy) 
          {
    //         Move the shape by dx pixels horizontally and dy pixels vertically
    //         (by changing the position of the top-left corner of the shape).
    //         left += dx;
    //         top += dy;
            triangle.translate(dx, dy);
          }
       } // end TriangleShape class  
    
    
       class OvalShape extends Shape 
       {
          void draw(Graphics g) 
          {
             g.setColor(color);
             g.fillOval(left,top,width,height);
             if (drawOutline) 
             {
                g.setColor(Color.black);
                g.drawOval(left,top,width,height);
             }
          }
          
          boolean containsPoint(int x, int y) 
          {
             double rx = width/2.0;   // horizontal radius of ellipse
             double ry = height/2.0;  // vertical radius of ellipse 
             double cx = left + rx;   // x-coord of center of ellipse
             double cy = top + ry;    // y-coord of center of ellipse
             // ellipse equation algorithm
             if ( (ry*(x-cx))*(ry*(x-cx)) + (rx*(y-cy))*(rx*(y-cy)) <= rx*rx*ry*ry )
                return true;
             else
               return false;
          }
       } // end OvalShape class
       
       class RoundRectShape extends Shape 
       {
          void draw(Graphics g) 
          {
             g.setColor(color);
             g.fillRoundRect(left,top,width,height,width/3,height/3);
             if (drawOutline) 
             {
                g.setColor(Color.black);
                g.drawRoundRect(left,top,width,height,width/3,height/3);
             }
          }
       } // end class RoundRectShape
    } // end class CompShapes
    -

  2. #2
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    fwiw i have whittled my issues down to just

    the triangle, the JList, and the JTextField validating configuration fields.

  3. #3
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    Still can't figure out how to get a triangle happening on the JPanel, I think it has something to do with passing the four int parameters to the Graphics drawPolygon (and/or fillPolygon) method(s).

    Triangle insight, anyone?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The simplest way to get something to draw on a JPanel, is to extend JComponent, and override the 'paintComponent' method to draw whatever you want. Use the 'getBounds' method of JComponent to figure out how big your component is, so you can adjust the size of your drawing appropriately.

    Then, add an instance of your customized component to the content pane. This approach is essentially trivial. The only thing I don't see obviously wrong with the triangle code, is that you have the shape definition commented out.

    (FYI, try to post only the relevant code. Not only will it help us help you, but often, you will figure out the problem yourself when you excise just the relevant parts of the problem).

  5. #5
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    thank you, toadaly, i do need all of the help i can get.

    I am familiar with the extending Jcomponent way of drawing/filling shapes, and I may end up going that route, but it's become something of an obsession to figure out how to draw on a JPanel in the manner I've currently got implemented. Mostly I want to figure out why it has eluded me so far; the rectangles and ovals were simple enough, why can't I get a triangle to display in the same manner?

    the commented out lines represent paths and approaches that have not yet panned out.

    thank you again, toadaly, i do appreciate yr feedback.

    I am currently traveling so my online time is fleeting at best, but I may try to whittle down the code to the essentials of my problem and figure out a way to get it working.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, you can always just call 'paint' directly if it's bothering you that much.

  7. #7
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    I did get the triangle working; the paint method really wanted all of the work to be done inside the class draw method, not outside as I was trying to do.

    now i've just got to get a working jlist as fed from the arraylist of shapes..

    tia for help and encouragement

  8. #8
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    Will you please stop to post your threads regarding swing and awt in New to java?
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 04:25 AM
  2. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 03:06 AM
  3. Exponential Shape Program
    By checkmylongboarding in forum New To Java
    Replies: 3
    Last Post: 09-23-2008, 04:31 AM
  4. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  5. How to change shape of JButton
    By FaRuK in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2007, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •