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 8 of 8

Thread: "Simple" Shape drawing!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Simple" Shape drawing!

    Hei currently working on an program that shall draw different shapes selected from a JComboBox. I cant seem to get the MouseListener/ComboBox to understand which shapes i selecet. Here is what i got so far in the GUI window..all the other files seems to be correct..Shapes.java, Circle.java , section.java , segment. java....and so on..

    class DrawingWindow extends JFrame implements MouseListener, MouseMotionListener,ActionListener,ItemListener {
     
     private String[] shapes= { "section", "segment",  "square", "Rect" , "Circle", "Ellipse" }; 
     
      private drawingPanel drawingPad;
      private JPanel ButtonPanel;
      private JComboBox box = new JComboBox(figurer);
      private JButton knapp = new JButton("Tegn");
      private Shape shapes;
     
      public TegneVindu(String title) {
        this.setTitle(tittel);  
        this.setSize( 600, 600 );
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
        JPanel np = new JPanel ();
        add(np, BorderLayout.NORTH);
        JLabel textfield= new JLabel("Choose shape of the figure: ");
        np.add(textfield);  
        np.add(box);
        box.addItemListener(this);
     
        JPanel sp = new JPanel(new BorderLayout());
        add(sp, BorderLayout.CENTER);
        drawingPad = new drawingPanel( );	
        drawingPad.addMouseListener(this);
        drawingPad.addMouseMotionListener(this);
     
        sp.add(drawingPad);
        sp.add(knapp, BorderLayout.NORTH);
        knapp.addActionListener(this);
      }    
     
        @Override
        public void mouseClicked(MouseEvent e) {
     
        }
     
        @Override
        public void mousePressed(MouseEvent e) {
     
          shapes= new segment(Color.black, e.getX(), e.getY(),
                                                 e.getX(), e.getY() );
     
          drawingPad.setFigur(figuren);
     
          drawingPad.repaint();
        }   
        @Override
        public void itemStateChanged(ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.SELECTED) {
     
     ???????????????????????? code here ???????????++
        }
        }
        @Override
        public void mouseReleased(MouseEvent e) {
     
          shapes.changeSize(e.getX(), e.getY() );
     
          drawingPad.repaint();
        }
        @Override
        public void mouseDragged(MouseEvent e) {
     
          shapes.changeSize(e.getX(), e.getY() );
     
          drawingPad.repaint();
        }
        @Override
        public void mouseEntered(MouseEvent e) {
     
        }
     
        @Override
        public void mouseExited(MouseEvent e) {
     
        }
        @Override
        public void mouseMoved(MouseEvent e) {
     
        }
     
        @Override
        public void actionPerformed(ActionEvent e) {
     
        }
     
     
     
    }


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Simple" Shape drawing!

    solved

    ---------- Post added at 05:38 PM ---------- Previous post was at 05:36 PM ----------

    my next question is ? how do i "clear" the JPanel with the different shapes in it?? ive made a button which is called "reset" when i click this the whole panel shoud be blank..trying with repaint..but dosent seem to work..yet ;P

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: "Simple" Shape drawing!

    This all depends on how you're drawing things to the JPanel. I assume that you're either drawing to a BufferedImage or within a paintComponent method override. If a BufferedImage, simply draw a rectangle of the background color over the entire image. If in paintComponent, simply undo the logic that you used to draw with.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Simple" Shape drawing!

    I'm brand new to coding so I'm not exactly sure what I'll say is right.

    All I know is that c.clear(); clears the page and makes it blank.

    Not sure if you need to import anything or not, I just started with java.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: "Simple" Shape drawing!

    ive made a button
    Always post the code with the question and word for word errors so we are all looking at the same thing as we talk. Besides how are we supposed to know what the button code does or troubleshoot it without seeing the code.

    but dosent seem to work..yet
    "dosent seem to work" does not say much about what it does, what it should do, or where it seems to go wrong.

    If you still need help, post the code, explain what it should do, explain what it does do, and try to make an educated guess where things are going wrong. Your words help us locate the problem and get you back on track faster.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: "Simple" Shape drawing!

    Quote Originally Posted by DarkPrince View Post
    I'm brand new to coding so I'm not exactly sure what I'll say is right.

    All I know is that c.clear(); clears the page and makes it blank.

    Not sure if you need to import anything or not, I just started with java.
    I think that you'll want to delete this answer as it is not a solution for the original poster's problem and may be misleading to him.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Simple" Shape drawing!

    ok thanks im new to both java and this forum, sorry.



    yep im drawing shapes in an JPanel..and i want to reset/clear the JPanel when the button Clear is clicked..


    public void actionPerformed(ActionEvent e) {


    Object p = box.getSelectedItem();

    if ("Section".equals(p) && e.getSource() == Draw) {
    Shape = new Section(ChooseColor, 18, 18);
    }

    if ( "segment".equals(p) && e.getSource() == Draw) {
    Shape = new segment(ChooseColor, 25, 25, 40, 40, 20);

    }

    if ("Square".equals(p) && e.getSource() == Draw) {
    Shape = new Square(ChooseColor, 15, 30, 15);

    }

    the code for the "Clear/reset" button ;
    if (e.getSource() == Clear) {
    drawingPad.repaint();

    everything works now..its just the clear/reset button im not familiar with..and dont know where to put the code or what to code here..but im guessing it has something to do with the panel?

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: "Simple" Shape drawing!

    You'll want to wrap your posted code in [code=java] [/code] tags so that it retains its formatting and is easier to read.

    Is your shape drawn in a JPanel's paintComponent method? If so, perhaps have a line:

    // in paintComponent method
     
    // Shape should begin with a lower case letter, shape:
    if (shape != null) {
       // draw your shape however you normally draw it
    }

    then in your clear method, you could set shape = null, and call repaint().

Similar Threads

  1. I need to create a certain SHAPE with FOR LOOP.
    By Rage1337 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 4th, 2012, 09:15 AM
  2. [SOLVED] Problem with a simple drawing program
    By Saiimon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 24th, 2012, 03:45 PM
  3. Moving a shape across a panel
    By havinFun in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 17th, 2012, 05:56 PM
  4. Shape error?
    By xTommy24 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:46 AM
  5. Shape Calculation Program
    By TH1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 03:54 PM