Results 1 to 7 of 7
  1. #1
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default First time, valiant effort, but having drawing problem

    Hello All!

    I am just getting back into java. And i read through my old college text in order to see if i could finally grasp this. I am using Netbeans 7.0 as a programing environment.

    The goal for this is to generate an applet which will perform a weighted average. While developing, i am using a JPanel inside of a JFrame. The program i have, below, is not complete as i am tackling the GUI first. I wanted to place a rounded rectangle as a background, behind the text strings and GUI elements.

    This rounded rectangle will not appear and I am not sure why. I can fill the background with a color. But, I cannot seem to place a rounded rectangle as the background. Can you guys see what i am doing wrong? I highlighted the portions of the code where i am trying to add the rounded rectangle.

    *note* The rounded rectangle is not sized correctly at this time. I was going to size it after i finally got it to appear in the JPanel.

    Java Code:
    package javaapplication2;
    
    //importing 
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class FFXIVCraftingCalculator {
      
    
    [COLOR="orange"]//for making rectangles with curved corners and a custom color
          private static class Rect extends JPanel {
          public void paintComponent(Graphics g) {
          super.paintComponent(g);
          
          g.fillRoundRect(10, 10, 200, 100, 2, 2);
          setVisible(true);
          
       }[/COLOR]
       }  
    //making listener for Close button, and assigning action to it
       private static class ButtonHandler implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             System.exit(0);
          }
       }
       
     //beginning main  
       public static void main(String[] args) {
           
     //create rectangle object, will not show up in the Jpanel
          
    [COLOR="orange"]      Rect roundedRectangle = new Rect();
          roundedRectangle.setForeground(new Color(36, 34, 101));[/COLOR]
    
          
    //create text fields, positions them
          JTextField rank1 = new JTextField("0", 3);
          rank1.setBounds(130, 40, 60, 18); 
          
          JTextField rank2 = new JTextField("0", 3);
          rank2.setBounds(130, 60, 60, 18); 
          
          JTextField rank3 = new JTextField("0", 3);
          rank3.setBounds(130, 80, 60, 18); 
          
          JTextField rank4 = new JTextField("0", 3);
          rank4.setBounds(130, 100, 60, 18); 
          
          JTextField rank5 = new JTextField("0", 3);
          rank5.setBounds(130, 120, 60, 18); 
          
          JTextField rank6 = new JTextField("0", 3);
          rank6.setBounds(130, 140, 60, 18); 
          
          JTextField rank7 = new JTextField("0", 3);
          rank7.setBounds(130, 160, 60, 18); 
          
          JTextField rank8 = new JTextField("0", 3);
          rank8.setBounds(130, 180, 60, 18); 
          
          
     //create close button object, assign listener, and set position/size
          JButton okButton = new JButton("Close");
          ButtonHandler listener = new ButtonHandler();
          okButton.addActionListener(listener);
          okButton.setBounds(200, 300, 100, 30);
          
     //text labels created, sized, positioned, color
          JLabel item1 = new JLabel("First Item: ");
          item1.setBounds(20, 40, 80, 18);
          item1.setForeground(new Color(236, 232, 133));
          
          JLabel item2 = new JLabel("Second Item: ");
          item2.setBounds(20, 60, 80, 18);
          item2.setForeground(new Color(236, 232, 133));
          
          JLabel item3 = new JLabel("Third Item: ");
          item3.setBounds(20, 80, 80, 18);
          item3.setForeground(new Color(236, 232, 133));
          
          JLabel item4 = new JLabel("Fourth Item: ");
          item4.setBounds(20, 100, 80, 18);
          item4.setForeground(new Color(236, 232, 133));
          
          JLabel item5 = new JLabel("Fifth Item: ");
          item5.setBounds(20, 120, 80, 18);
          item5.setForeground(new Color(236, 232, 133));
          
          JLabel item6 = new JLabel("Sixth Item: ");
          item6.setBounds(20, 140, 80, 18);
          item6.setForeground(new Color(236, 232, 133));
          
          JLabel item7 = new JLabel("Seventh Item: ");
          item7.setBounds(20, 160, 80, 18);
          item7.setForeground(new Color(236, 232, 133));
          
          JLabel item8 = new JLabel("Eighth Item: ");
          item8.setBounds(20, 180, 80, 18);
          item8.setForeground(new Color(236, 232, 133));
          
          JLabel itemRank = new JLabel("Item Rank");
          itemRank.setBounds(130, 20, 80, 18);
          itemRank.setForeground(new Color(236, 232, 133));
          
          JLabel Quality = new JLabel("Item Quality");
          Quality.setBounds(198, 20, 80, 18);
          Quality.setForeground(new Color(236, 232, 133));
          
     //drop boxes created, granted values, positioned, sized
          JComboBox itemQuality1 = new JComboBox();
          itemQuality1.addItem("NQ");
          itemQuality1.addItem("HQ");
          itemQuality1.addItem("HQ2");
          itemQuality1.addItem("HQ3");
          itemQuality1.setBounds(200, 40, 60, 18);
          
          JComboBox itemQuality2 = new JComboBox();
          itemQuality2.addItem("NQ");
          itemQuality2.addItem("HQ");
          itemQuality2.addItem("HQ2");
          itemQuality2.addItem("HQ3");
          itemQuality2.setBounds(200, 60, 60, 18);
          
          JComboBox itemQuality3 = new JComboBox();
          itemQuality3.addItem("NQ");
          itemQuality3.addItem("HQ");
          itemQuality3.addItem("HQ2");
          itemQuality3.addItem("HQ3");
          itemQuality3.setBounds(200, 80, 60, 18);
          
          JComboBox itemQuality4 = new JComboBox();
          itemQuality4.addItem("NQ");
          itemQuality4.addItem("HQ");
          itemQuality4.addItem("HQ2");
          itemQuality4.addItem("HQ3");
          itemQuality4.setBounds(200, 100, 60, 18);
          
          JComboBox itemQuality5 = new JComboBox();
          itemQuality5.addItem("NQ");
          itemQuality5.addItem("HQ");
          itemQuality5.addItem("HQ2");
          itemQuality5.addItem("HQ3");
          itemQuality5.setBounds(200, 120, 60, 18);
          
          JComboBox itemQuality6 = new JComboBox();
          itemQuality6.addItem("NQ");
          itemQuality6.addItem("HQ");
          itemQuality6.addItem("HQ2");
          itemQuality6.addItem("HQ3");
          itemQuality6.setBounds(200, 140, 60, 18);
          
          JComboBox itemQuality7 = new JComboBox();
          itemQuality7.addItem("NQ");
          itemQuality7.addItem("HQ");
          itemQuality7.addItem("HQ2");
          itemQuality7.addItem("HQ3");
          itemQuality7.setBounds(200, 160, 60, 18);
          
          JComboBox itemQuality8 = new JComboBox();
          itemQuality8.addItem("NQ");
          itemQuality8.addItem("HQ");
          itemQuality8.addItem("HQ2");
          itemQuality8.addItem("HQ3");
          itemQuality8.setBounds(200, 180, 60, 18);
          
    
          
     //create jpanel and populate it with containers(objects)
          JPanel content = new JPanel();
    [COLOR="orange"]      content.add(roundedRectangle);[/COLOR]
          content.setLayout(null); 
          //content.setBackground(new Color(36, 34, 101));
          content.add(okButton);
          content.add(rank1);
          content.add(rank2);
          content.add(rank3);
          content.add(rank4);
          content.add(rank5);
          content.add(rank6);
          content.add(rank7);
          content.add(rank8);
          content.add(itemQuality1);
          content.add(itemQuality2);
          content.add(itemQuality3);
          content.add(itemQuality4);
          content.add(itemQuality5);
          content.add(itemQuality6);
          content.add(itemQuality7);
          content.add(itemQuality8);
          content.add(item1);
          content.add(item2);
          content.add(item3);
          content.add(item4);
          content.add(item5);
          content.add(item6);
          content.add(item7);
          content.add(item8);
          content.add(itemRank);
          content.add(Quality);
          
    //create jframe, sized, positioned, and populated it with the jpannel object
          JFrame window = new JFrame("FFXIV Crafting Calculator");
          window.setContentPane(content);
          window.setSize(500,400);
          window.setLocation(100,100);
          window.setVisible(true);
          
       }
       
    }
    Last edited by Willriker; 08-10-2011 at 06:12 AM. Reason: quote tags changed to code tags

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

    Default

    Welcome to the forum. I have edited your post and changed your quote tags to code tags so your code will be more readable.


    Quote Originally Posted by Willriker View Post
    The goal for this is to generate an applet which will perform a weighted average. While developing, i am using a JPanel inside of a JFrame. The program i have, below, is not complete as i am tackling the GUI first. I wanted to place a rounded rectangle as a background, behind the text strings and GUI elements.
    Then this is something different than what your code is doing. Your code is creating a bunch of components and adding them all to the contentPane. If you want your rounded rectangle to hold a bunch of components, then you must add the components to the rounded rectangle, not the contentPane. True, eventually, you'll need to add the rounded rectangle to a container that eventually leads to the contentPane.

    This rounded rectangle will not appear and I am not sure why. I can fill the background with a color. But, I cannot seem to place a rounded rectangle as the background. Can you guys see what i am doing wrong? I highlighted the portions of the code where i am trying to add the rounded rectangle.

    *note* The rounded rectangle is not sized correctly at this time. I was going to size it after i finally got it to appear in the JPanel.
    If you're going to use a null layout, you'd better know it's restrictions. When using this layout, if you don't set a components size and position, it won't show. I strongly advise you to not use null layouts, ever if possible. Instead use the user-friendly layout managers.

  3. #3
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Oh thank you for the quick reply! I had no idea someone would reply so quickly.

    Yeah, i learned that about the null layout the hard way with the other GUI elements. But i thought this embeded class gave the position and size for the rounded rectangle.

    Java Code:
    //for making rectangles with curved corners and a custom color
          private static class Rect extends JPanel {
          public void paintComponent(Graphics g) {
          super.paintComponent(g);
          
          g.fillRoundRect[COLOR="orange"](10, 10, 200, 100, 2, 2);[/COLOR]
          setVisible(true);
          
       }
    g.fillRoundRect(x int, y int, width, hight, x radius, y radius);

    about the rounded rectangle holding the GUI elements. I had not thought of it that way. I was just thinking i was making a rounded rectangle and placing the GUI elements on top of it. Is there a way to do it with how i was thinking of this?
    Last edited by Willriker; 08-10-2011 at 06:14 AM.

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

    Default

    Quote Originally Posted by Willriker View Post
    Yeah, i learned that about the null layout the hard way with the GUI elements. But i thought this embeded class gave the position and size for the rounded rectangle.

    Java Code:
    //for making rectangles with curved corners and a custom color
          private static class Rect extends JPanel {
          public void paintComponent(Graphics g) {
          super.paintComponent(g);
          
          g.fillRoundRect[COLOR="orange"](10, 10, 200, 100, 2, 2);[/COLOR]
          setVisible(true);
          
       }
    g.fillRoundRect(x int, y int, width, hight, x radius, y radius);
    All that does is tell Java to draw something in that JPanel at that location. It does nothing to set the position or size of the JPanel, and if the JPanel is of size 0 , 0 (as yours indeed will be), the drawing of course will be invisible.

  5. #5
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    I think I see, so I have the rounded rectangle going to a Jpanel, but the JPanel's dimensions are undefined?

    That explains the extra window that was popping up at one point while trying to get this to work. How would I alter my code so that the rounded rectangle appears in the content pane, with the other GUI elements, instead of the JPanel?

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

    Default

    Quote Originally Posted by Willriker View Post
    I think I see, so I have the rounded rectangle going to a Jpanel, but the JPanel's dimensions are undefined?
    No, the dimensions are not undefined but rather they are the default values of [0, 0].

    That explains the extra window that was popping up at one point while trying to get this to work. How would I alter my code so that the rounded rectangle appears in the content pane, with the other GUI elements, instead of the JPanel?
    • If you want your rounded JPanel as the background panel, make it so -- add it to the top-level container's contentPane in the BorderLayout.CENTER position.
    • Give it a decent layout as well, and start adding components to it. In other words use it as a container.
    • If you add other JPanels to your image-background JPanel, be sure that you set their opaque property to false so the background image can be seen through them. This is done easily by calling setOpaque(false) on them.
    • Again, read up on and learn to use the layout managers.
    • Don't hard-code the rectangle borders in the paintComponent method, but rather get the JPanel's width and height (getWidth() and getHeight()), and use those values to tell paintComponent where to draw the rectangle.
    Last edited by Fubarable; 08-10-2011 at 06:28 AM.

  7. #7
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    Thank you for your help, this got the rounded rectangle to appear. Thank you for pointing me in this direction, or at least getting me to look at it differently!

    Java Code:
     //create jpanel and populate it with containers(objects)
         [COLOR="orange"] JPanel content = new JPanel();        changed to Rect content = new Rect();[/COLOR]
          content.add(roundedRectangle);
          content.setLayout(null); 
          //content.setBackground(new Color(36, 34, 101));
          content.add(okButton);
          content.add(rank1);
          content.add(rank2);
          content.add(rank3);
          content.add(rank4);
          content.add(rank5);
          content.add(rank6);
          content.add(rank7);
          content.add(rank8);
          content.add(itemQuality1);
          content.add(itemQuality2);
          content.add(itemQuality3);
          content.add(itemQuality4);
          content.add(itemQuality5);
          content.add(itemQuality6);
          content.add(itemQuality7);
          content.add(itemQuality8);
          content.add(item1);
          content.add(item2);
          content.add(item3);
          content.add(item4);
          content.add(item5);
          content.add(item6);
          content.add(item7);
          content.add(item8);
          content.add(itemRank);
          content.add(Quality);
    I realized that i extended Jpanel with the Rect class. But i added JPanel instead of Rect.
    Last edited by Willriker; 08-10-2011 at 07:02 AM.

Similar Threads

  1. Problem with drawing program
    By gameaddict123 in forum New To Java
    Replies: 2
    Last Post: 07-20-2011, 04:55 AM
  2. First Time problem!
    By Jcbconway in forum New To Java
    Replies: 7
    Last Post: 11-16-2010, 07:24 PM
  3. Double drawing problem with 2D
    By leapinlizard in forum Java 2D
    Replies: 4
    Last Post: 02-12-2010, 02:02 AM
  4. Problem with 3D drawing.
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 08-31-2008, 01:39 PM
  5. [SOLVED] Problem in Drawing(Repaint)
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 08:28 AM

Posting Permissions

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