Results 1 to 3 of 3
  1. #1
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Exclamation problem resizing rectangles in Jpanels

    Dear all
    i am new to java and i wanted to create a frame having two jpanels and in one jpanel i want to create two
    rectangles. for this purpose i have created a myRect() class that contain two constructor one default constructor and the other constructor takes four variables : (x, y, w, h). the code for the rectangle can be found below.

    package my.graphics;

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Color;

    public class myRect extends JPanel{

    private int x,y,w,h;
    public myRect()
    {//this.setBackground(Color.white);
    //this.setPreferredSize(new Dimension(300,300));


    //public myRect(){
    x=1;
    y=1;
    h=50;
    w=80;
    }
    public myRect(int xx, int yy, int hh, int ww)
    {//this.setBackground(Color.white);
    //this.setPreferredSize(new Dimension(300,300));
    x = xx;
    y = yy;
    h = hh;
    w = ww;
    }
    public void paintComponent(Graphics g)
    {//super.paintComponent(g);
    g.setColor(Color.green);
    g.fillRect(x,y,w,h);
    }


    ///// to add the myRect() objects i am using the following syntax:
    JPanel Panel2 = new JPanel();
    myRect car = new myRect(10,10,40,40);
    myRect canon = new myRect(1,1,100,100);
    Panel2.setBackground(Color.white);
    frame1.getContentPane().add(Panel2,BorderLayout.CE NTER);

    Panel2.setBorder(BorderFactory.createLineBorder (Color.blue,2));
    Panel2.add(car);
    Panel2.add(canon);
    Panel2.setPreferredSize(new Dimension(300,300));
    frame1.pack();

    the problem that i am having with this code is the following:
    when i try to add two myRect() objects to the panel without including this.setPreferredSize(new Dimension(300,300)); line in my Rect() class i get in the panel2 a small panel that display a portion of the rectangle that i have created. no matter how much i increase the size of the rectangle the same portion of the rectangle is always displayed.
    if i include the this.SetPreferredSize(new Dimension(300,300) line and try to create two rectangles then only the first rectangle is created while the other is not displayed.


    can anyone specify who can i solve this problem. or is there another way to add two rectangles of different size at different positions (remember i am new to java and maybe there exist a very simpler way to do this the right way)
    thanks in advance.

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

    Default Re: problem resizing rectangles in Jpanels

    Your problem is one of how layout managers handle component size. For example if you add a border to your JPanel, you'll see that it's being rendered quite small:

    Java Code:
       public myRect(int xx, int yy, int hh, int ww) {// this.setBackground(Color.white);
       // this.setPreferredSize(new Dimension(300,300));
          x = xx;
          y = yy;
          h = hh;
          w = ww;
          
          setBorder(BorderFactory.createLineBorder(Color.black));
       }
    One solution is to have the JPanel that holds to two rectangle making JPanel use a GridLayout (look up the tutorial to see how to do this). Another solution is to have the newRect class (please change the name to NewRect since it's a class) a getPreferredSize() method override, one that uses the class data to set its best size.

    Most important though is to read up and understand the layout managers.

  3. #3
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: problem resizing rectangles in Jpanels

    Dear Fubarable,
    thanks for your reply,
    i want to ask you if i should set the jpanel to GridLayout if i want to use this jpanel as a drawing area, which will contain in my final version two rectangle with two circles that will move horizontally.
    dont you think that the gridlayout will constrain the positions in my Jpanel where i can place the rectangle and the gridlayout will prevent me from moving the objects from on grid to another

Similar Threads

  1. JTextArea resizing problem
    By me.anchit in forum AWT / Swing
    Replies: 5
    Last Post: 07-24-2011, 02:01 PM
  2. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  3. JFrame resizing problem
    By Bluefox815 in forum AWT / Swing
    Replies: 7
    Last Post: 02-24-2011, 07:56 PM
  4. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 03:13 AM
  5. Problem in resizing JTable in netbeans
    By shahid0627 in forum NetBeans
    Replies: 1
    Last Post: 09-07-2009, 06:33 PM

Tags for this Thread

Posting Permissions

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