Results 1 to 3 of 3
  1. #1
    Greedychris is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Having issues drawing Shapes into Jpanel

    Hi guys, I'm pretty new here as you can see, I wouldn't normally do this but I really am stuck, I've manged to get the Program to compile but I have no idea whats wrong with it.

    I personally think its something to do with the ActionPerfomed part but I'm not sure, Could someone please see where I have gone wrong? I'm finding it difficult to draw the shapes into the new JPanel.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class shape3 extends JFrame implements ActionListener
    {
        boolean Rectangle = false;
        boolean Circle = false;
        int x;
        int y;
        int width;
        int height;
        JTextField input1;
        JTextField input2;
        JTextField input3;
        JTextField input4;
        JTextField input5;
        JTextField input6;
        JButton addRect;
        JButton addOval;
        double area;
        double perimeter;
        JLabel label, label1, label2, label3, label4, label5, label6, label7; 
        MyPanel myPanel;
        FlowLayout layout;
      
        
      public shape3()
      {
          setSize (420,510);
          getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
          setVisible(true);
          
         
          
          JPanel myPanel =  new JPanel();
          myPanel.setPreferredSize(new Dimension(400,400) );
          myPanel.setBackground(Color.yellow);
          setVisible(true);
           
          addRect = new JButton(" Draw Rectangle");
          addOval = new JButton("Draw Oval");
          addRect.setVisible(true);
          addRect.setBackground(Color.blue);
          addRect.setForeground(Color.white);
          addRect.addActionListener(this);  
          addOval.setVisible(true);
          addOval.setBackground(Color.red);
          addOval.setForeground(Color.white);
          addOval.addActionListener(this);
         
          JLabel label = new JLabel("Enter X coordinates");
          JLabel label1 = new JLabel("Enter Y coordinates");
          JLabel label2 = new JLabel("Enter Width");
          JLabel label3 = new JLabel("Enter Height");
          JLabel label4 = new JLabel("Parimeter of shape");
          JLabel label5 = new JLabel("Area of shape");
          
    
        
          input1 = new JTextField(3);
          input2 = new JTextField(3);
          input3 = new JTextField(3);
          input4 = new JTextField(3);
          input5 = new JTextField(3);
          input6 = new JTextField(3);
          input5.setEditable(false);
          input6.setEditable(false);
          
          
          add(addRect);
          add(addOval);
          add(label);
          add(input1);
          add(label1);
          add(input2);
          add(label2);
          add(input3);
          add(label3);
          add(input4);
          add(label4);
          add(input5);
          add(label5);
          add(input6);
          add(myPanel, BorderLayout.CENTER);
                
      }
      
     
      public void actionPerformed(ActionEvent ev)
      {
             x = Integer.parseInt(input1.getText());
             y = Integer.parseInt(input2.getText());
             width = Integer.parseInt(input3.getText());
             height = Integer.parseInt(input4.getText());
             
            
             {
                   
                   if(ev.getSource() == addRect)
                   {
                     
                    Rectangle = true;
                    repaint();
                  
                    
                   }
                   
                   if (ev.getSource() == addOval)
                   {
                     
                     Circle = true;
                     repaint();
         
                     
                   }
                   
      
                  
             }
      }
      
      
    
                  
       public static void main(String args[])
      {
            new shape3();
      }
    
    class MyPanel extends JPanel 
    {
       
       public void paint(Graphics graf)
       {
         super.paint(graf);
         
         if (Circle )
         {
            Circle = false;
            graf.setColor(Color.blue);
            graf.drawRect(x, y, width, height);
            repaint();
            
         }
         if (Rectangle ) 
         {
            Rectangle = false;
            graf.setColor(Color.blue);
            graf.drawOval(x, y, width, height);
            repaint();
          }
      
         
    
    }
    }
    }
    Could really do with your help, I know its messy at the moment but I will sort it out when Ive done this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Having issues drawing Shapes into Jpanel

    You'll want to look up some tutorials on how to do painting as you can't guess at this stuff and it's different from other types of coding you've done before. A few suggestions:
    • draw in the JPanel's paintComponent method, not its paint method.
    • For gosh's sake don't call repaint() from inside of a paint or paintComponent method. That makes no sense at all and is potentially dangerous.
    • Don't have your paint or paintComponent method doing any program logic such as setting class fields (like your boolean fields). You do not have absolute control over when or if these methods will be called, so this is not a wise thing to do.
    • Test out painting in a simpler program first, then when you've got it working, move it over to your larger program. Or if you get stuck, then you can post a smaller simpler program.


    Much luck!

  3. #3
    Greedychris is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Having issues drawing Shapes into Jpanel

    Thank you for such a quick response, Yeah the code is a but messy because I've been trying ideas out from other examples online. I do have two simpler versions of this program.

    shape

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class shape extends JFrame implements ActionListener
    {
        
        int x;
        int y;
        int width;
        int height;
        JTextField input1;
        JTextField input2;
        JTextField input3;
        JTextField input4;
        JTextField input5;
        Button button1, button2;
        double area;
        double perimeter;
       
      
        
      public shape()
      {
          setSize (600,600);
          setLayout(new FlowLayout());
          setVisible(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
            
          button1 = new Button(" Draw Rectangle");
          button2 = new Button("Draw Oval");
        
          button1.setVisible(true);
          button1.setBackground(Color.blue);
          button1.setForeground(Color.white);
          button1.addActionListener(this);
          
          button2.setVisible(true);
          button2.setBackground(Color.red);
          button2.setForeground(Color.white);
          button2.addActionListener(this);
         
          JLabel label = new JLabel("Enter X coordinates");
          JLabel label1 = new JLabel("Enter Y coordinates");
          JLabel label2 = new JLabel("Enter Width");
          JLabel label3 = new JLabel("Enter Height");
    
        
          input1 = new JTextField(3);
          input2 = new JTextField(3);
          input3 = new JTextField(3);
          input4 = new JTextField(3);
          input5 = new JTextField(3);
          input5.setEditable(false);
          
          add(button1);
          add(button2);
          add(input1);
          add(input2);
          add(input3);
          add(input4);
          add(input5);
                
      }
    
      public void actionPerformed(ActionEvent ev)
      {
             x = Integer.parseInt(input1.getText());
             y = Integer.parseInt(input2.getText());
             width = Integer.parseInt(input3.getText());
             height = Integer.parseInt(input4.getText());
             Graphics graf = this.getGraphics();
             {
                   System.out.println(ev);
                   if(ev.getSource() == button1)
                   {
                         graf.drawRect(x, y, width, height);
                         validate();
                   }
                   
                   if (ev.getSource() == button2)
                   {
                     graf.drawOval(x, y, width, height);
                     validate();
                   }
             }
      }
      
      
      public void drawRect(Graphics graf)
      {
            graf.setColor(Color.blue);
            graf.drawRect(x, y, width, height);
            
            graf.setColor(Color.blue);
            graf.fillRect(x, y ,width ,height);
      }
      
      public void drawOval(Graphics graf)
      {
            graf.setColor(Color.blue);
            graf.drawOval(x, y, width, height);
            
            graf.setColor(Color.blue);
            graf.fillOval(x, y ,width ,height);
      }
      
                  
       public static void main(String args[])
      {
            new shape();
      }
      
    }
    shape2

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class shape2 extends JFrame implements ActionListener
    {
        
         int x;
         int y;
         int width;
         int height;
         JTextField input1;
         JTextField input2;
         JTextField input3;
         JTextField input4;
         JTextField input5;
         JTextField input6;
         JLabel label, label1, label2, label3, label4, label5, label6, label7 ; 
         Button button1, button2;
         double area;
         double perimeter;
         MyPanel mypanel;
       
      
        
      public shape2()
      {
          JFrame frame = new JFrame("lol");
          frame.pack();
          frame.setSize (400,400);
          frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
          frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
          frame.setVisible(true);
                                           
          JPanel mainPanel =  new JPanel();
          JLabel label6 = new JLabel ("ToolBox");
          mainPanel.setPreferredSize( new Dimension(400, 100) );
          
          
          JPanel subPanel2 = new JPanel();
          JLabel label7 = new JLabel ("SketchPad");
          subPanel2.setPreferredSize(new Dimension(400,400) );
          subPanel2.add(label6);
          frame.add(mainPanel);
          mainPanel.add(label6);
          frame.add(subPanel2);
          subPanel2.add(label7);
                                           
                                           
            
          button1 = new Button(" Draw Rectangle");
          button2 = new Button("Draw Oval");
        
          button1.setVisible(true);
          button1.setBackground(Color.blue);
          button1.setForeground(Color.white);
          button1.addActionListener(this);
          
          button2.setVisible(true);
          button2.setBackground(Color.red);
          button2.setForeground(Color.white);
          button2.addActionListener(this);
          
          
         
          JLabel label = new JLabel("Enter X coordinates");
          JLabel label1 = new JLabel("Enter Y coordinates");
          JLabel label2 = new JLabel("Enter Width");
          JLabel label3 = new JLabel("Enter Height");
          JLabel label4 = new JLabel("Parimeter of shape");
          JLabel label5 = new JLabel("Area of shape");
                                           
    
        
          input1 = new JTextField(3);
          input2 = new JTextField(3);
          input3 = new JTextField(3);
          input4 = new JTextField(3);
          input5 = new JTextField(3);
          input6 = new JTextField(3);
          input5.setEditable(false);
          input6.setEditable(false);
          
         
          mainPanel.add(button1);
          mainPanel.add(button2);
          mainPanel.add(label);
          mainPanel.add(input1);
          mainPanel.add(label1);
          mainPanel.add(input2);
          mainPanel.add(label2);
          mainPanel.add(input3);
          mainPanel.add(label3);
          mainPanel.add(input4);
          mainPanel.add(label4);
          mainPanel.add(input5);
          mainPanel.add(label5);
          mainPanel.add(input6);
          
       
          mainPanel.setVisible(true);
          subPanel2.setVisible(true);
                
      }
      
      public void actionPerformed(ActionEvent ev)
      {
             x = Integer.parseInt(input1.getText());
             y = Integer.parseInt(input2.getText());
             width = Integer.parseInt(input3.getText());
             height = Integer.parseInt(input4.getText());
             Graphics graf = this.getGraphics();
             {
                   System.out.println(ev);
                   if(ev.getSource() == button1)
                   {
                         graf.drawRect(x, y, width, height);
                        
                         validate();
                   }
                   
                   if (ev.getSource() == button2)
                   {
                     graf.drawOval(x, y, width, height);
                     validate();
                   }
             }
      }    
    
        public static void main(String args[])
      {
            new shape2();
      }  
    
    class MyPanel extends JPanel
    {
      
    
    }
    }

    please excuse how messy it is but the only thing stopping me from using the first example is the fact that I people can draw shapes which overlap my buttons, I Only need help with trying to draw shapes on the Jpanel. Ive tried lots of methods which I have found haven't worked.

    Really sorry for the noobyness , but I would class myself as one of those :)
    Last edited by Greedychris; 03-06-2012 at 06:34 AM.

Similar Threads

  1. Drawing different shapes with stars.
    By Amazineous in forum New To Java
    Replies: 23
    Last Post: 11-14-2011, 04:55 PM
  2. Drawing simple shapes using loops
    By Quinn in forum New To Java
    Replies: 2
    Last Post: 05-29-2011, 10:05 AM
  3. Drawing shapes with for loops using characters
    By Strawlion in forum New To Java
    Replies: 3
    Last Post: 02-16-2011, 09:32 PM
  4. help! shapes output and array issues!
    By japgoth in forum New To Java
    Replies: 2
    Last Post: 01-25-2011, 10:49 PM
  5. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 07:35 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
  •