Java Forum / GUI / May 2006
Adding two applets in an applet
Rohit Gupta - 11 May 2006 15:16 GMT Hi,
I am facing a problem in adding two JComponents in an applet. The two components are separately defined in different files, and I am instantiating them in a third file which is the applet. The problem is that I am able to see only the second component while the first one is not present. Both the Jcomponents have their paint methods separately defined in their file. Since there are 3 files involved I was not sure what shoudl I paste here as a code, if there are any questions regarding the clarity of the problem I shall answer them immediately. Please help!!
TIA
Rohit
Rohit Gupta - 11 May 2006 15:17 GMT The subject is wrong please read it as : Adding two jcomponents in an applet
Thomas Weidenfeller - 11 May 2006 15:51 GMT > I am facing a problem in adding two JComponents in an applet. > The two components are separately defined in different files, and I am > instantiating them in a third file which is the applet. The problem is > that I am able to see only the second component while the first one is > not present. a) Ensure they return proper sizes
b) Use layout managers.
c) If you already use layout managers, correct the code where you don't use them correctly.
d) There is an error in line 42 of your code.
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Rohit Gupta - 11 May 2006 17:42 GMT I am using BorderLayout, and I have assigned Center to one and East to other and the problem is that the East one is not appearing. If I interchange the position then also the East positioned component is not appearing. Please Help!
TIA
Rohit
Vova Reznik - 11 May 2006 18:02 GMT > I am using BorderLayout, and I have assigned Center to one and East to > other and the problem is that the East one is not appearing. If I [quoted text clipped - 5 lines] > > Rohit http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html
check preferred size for the EAST component.
Thomas A. Russ - 11 May 2006 18:09 GMT > I am using BorderLayout, and I have assigned Center to one and East to > other and the problem is that the East one is not appearing. If I > interchange the position then also the East positioned component is not > appearing. > Please Help! Sounds like the JComponents do not have minimum or preferred sizes set. In BorderLayout, the NSEW positions usually get their preferred or minimum size, and all extra size goes to the center.
 Signature Thomas A. Russ, USC/Information Sciences Institute
Rohit Gupta - 12 May 2006 09:06 GMT Ok, I understand the problem now, my Center component can expand upon mouse clicks to fill in the complete screen so maybe this is the reason the East component isn't becoming visible even though I have assigned a very small size to it.
Should I use any other layout, I read about some other but none of them seem to fit into this. Any advice would be highly appreciated.
Rohit
Rohit Gupta - 12 May 2006 09:11 GMT What I need is that the Center component fills in the complete screen but the East component remains static there.
Any suggestions on what should I do?
Rohit
Rogan Dawes - 12 May 2006 11:36 GMT > Ok, I understand the problem now, my Center component can expand upon > mouse clicks to fill in the complete screen so maybe this is the reason [quoted text clipped - 6 lines] > > Rohit As a previous poster suggested, look into
JComponent.setPreferredSize()
JComponent.setMinimumSize()
JComponent.setMaximumSize()
for your CENTRE and EAST components.
Rogan
Rohit Gupta - 12 May 2006 15:50 GMT I have set the appropriate values in the 3 functions for the components, there is some improvement as now the center doesn't occupy the whole screen and there is a grey bar left in the right side. However the east component is still not visible, I am drawing a rectangle in the EAST component whose sizes I have taken appropriately from the preferred size values. Here is a snippet of the code where I initialize the 3 functions for the components :
dc.setPreferredSize(new Dimension(this.getWidth()-50, this.getHeight())); dc.setMaximumSize(new Dimension(this.getWidth()-30, this.getHeight())); tck.setMinimumSize(new Dimension(30,this.getHeight()-80)); tck.setPreferredSize(new Dimension(50,this.getHeight()-70)); tck.setMaximumSize(new Dimension(50, this.getHeight()-60)); add(tck, BorderLayout.EAST); add(dc, BorderLayout.CENTER);
Is there anything which I am missing or not understanding, please help as its really important.
TIA
Rohit
andrewthommo@gmail.com - 17 May 2006 04:03 GMT > dc.setPreferredSize(new Dimension(this.getWidth()-50, this.getHeight()));
If 'this' is the applet and this code is performed prior to packing/appearing, the sizes you set might end up 0.
But then - a lot of things 'might be' wrong with the incomplete code above.
A couple of points.
Applets are considerably harder to debug than layouts, you should fix this code in a frame first.
People have hinted that you should work through the GUI tutorial, I will be more explicit. You will almost certainly complete this task faster if you do the GUI tutorial -first-!
You will get clearer, faster answers if you can supply a (short!) self contained piece of code that breaks (an SSCCE).
HTH
Andrew T.
Rohit Gupta - 18 May 2006 10:06 GMT I am trying to write a short code which should cover all the necessary details :
Class A extends Japplet, MouseListener.... {
public void init() { loadInterface(); } private void loadInterface() { addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(this); setLayout(new BorderLayout()); tck = new Thick(...); dc = new DrawingCanvas(...); dc.setPreferredSize(new Dimension(this.getWidth()-50, this.getHeight())); dc.setMaximumSize(new Dimension(this.getWidth()-30, this.getHeight())); tck.setMinimumSize(new Dimension(30,this.getHeight()-80)); tck.setPreferredSize(new Dimension(50,this.getHeight()-70)); tck.setMaximumSize(new Dimension(50, this.getHeight()-60)); add(tck, BorderLayout.EAST); add(dc, BorderLayout.CENTER);
} }
//different file public class DrawingCanvas extends JComponent { /* In this class I draw a large number of polygons and lines */ }
//different file
public class Thick extends JComponent { /* In this I draw a rectangle of a given size*/ }
It won,t be possible for me to give a self compilable code showing the same features and which would fail, because its really hard to replicate some of the stuff. If you could show me some way to get out of this problem it would be of great help!!
Rohit
Rohit Gupta - 18 May 2006 10:07 GMT and Yes, I have gone through the GUI tutorials.
Rohit
Rohit Gupta - 18 May 2006 10:40 GMT Ok, I have written the code which you can compile and please tell me why it isn't working :
/*Main.java*/
package javaapplication1;
import java.awt.BorderLayout; import javax.swing.JApplet; import java.awt.BorderLayout; import java.awt.Dimension;
/** * * @author gupta */ public class Main extends JApplet{
/** Creates a new instance of Main */ public void init() { loadInterface(); } private void loadInterface() {
DrawingCanvas dc = new DrawingCanvas(); Thick tck = new Thick(); setLayout(new BorderLayout()); dc.setPreferredSize(new Dimension(this.getWidth()-50, this.getHeight())); dc.setMinimumSize(new Dimension(this.getWidth()-70, this.getHeight())); dc.setMaximumSize(new Dimension(this.getWidth()-30, this.getHeight())); tck.setMinimumSize(new Dimension(30,this.getHeight()-80)); tck.setPreferredSize(new Dimension(50,this.getHeight()-70)); tck.setMaximumSize(new Dimension(50, this.getHeight()-60)); add(tck, BorderLayout.EAST); add(dc, BorderLayout.CENTER); }
/** * @param args the command line arguments */
}
/*DrawingCanvas.java*/
package javaapplication1;
import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color;
public class DrawingCanvas extends JComponent {
private Graphics2D g2d; public DrawingCanvas() { repaint(); }
public void paint (Graphics g) { g2d = (Graphics2D) g; g2d.setColor(Color.blue); g2d.fillRect(0,0, getWidth(), getHeight()); }
}
/*Thick.java*/
package javaapplication1;
import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import java.awt.GradientPaint;
public class Thick extends JComponent {
private Graphics2D g2d;
public Thick() { repaint(); }
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; //GradientPaint gp = new GradientPaint (0, 10, new Color((float)1,(float)0,(float)0), 0, 50, new Color((float)0,(float)0,(float)1)); //g2d.setPaint(gp); g2d.setColor(Color.red); g2d.drawRect( getWidth(),50, 20, 20); } }
Please help!!!
Rohit
Rohit Gupta - 18 May 2006 10:46 GMT I made a mistake in the last line of Thick.java. Read it as :
g2d.drawRect(getWidth()-30,50,20,50)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|