Results 1 to 3 of 3
- 01-28-2012, 05:43 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 15
- Rep Power
- 0
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.
-
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:
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.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)); }
Most important though is to read up and understand the layout managers.
- 01-28-2012, 10:42 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 15
- Rep Power
- 0
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
-
JTextArea resizing problem
By me.anchit in forum AWT / SwingReplies: 5Last Post: 07-24-2011, 02:01 PM -
Switching JPanels with a button inside one of the JPanels
By Beastly in forum AWT / SwingReplies: 2Last Post: 04-26-2011, 02:50 PM -
JFrame resizing problem
By Bluefox815 in forum AWT / SwingReplies: 7Last Post: 02-24-2011, 07:56 PM -
Problem resizing JPanel on window resize
By Nyet in forum AWT / SwingReplies: 4Last Post: 11-27-2009, 03:13 AM -
Problem in resizing JTable in netbeans
By shahid0627 in forum NetBeansReplies: 1Last Post: 09-07-2009, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks