aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem displaying panel in an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem displaying panel in an applet" Watch "Problem displaying panel in an applet" New topic
Author

Problem displaying panel in an applet

Rohan Pujari
Greenhorn

Joined: Feb 27, 2007
Posts: 24
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;


public class RectanglePanel extends JPanel
{
int rect_ypos = 50 ;
int rect_width = 50 ;
int rect_height = 50 ;
Rectangle2D[] rect ;
Graphics2D g2 ;

public RectanglePanel()
{
rect = new Rectangle2D.Double[50] ;
System.out.println("Called") ;
}

public void paintComponent(Graphics g)
{
super.paintComponent(g) ;

Graphics2D g2 = (Graphics2D)g ;

for(int i=0,start_pos = 50,increment = rect_width+10 ;i<3; i++,start_pos+=increment)
{
rect(i) = new Rectangle2D.Double(start_pos,rect_ypos,rect_width,rect_height) ;
g2.draw(rect(i)) ;
}
}
}

This is a panel when i add it to an applet i dont see the panel but when i add it to a frame den it is displayed please let me know why??

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DrawRectangle extends JApplet
{
RectanglePanel drawpanel ;
JPanel jp ;
JTextField tf ;
JButton b ;

public void init()
{
Container contentpane = getContentPane();

jp = new JPanel() ;

drawpanel = new RectanglePanel() ;

tf = new JTextField(10) ;

b = new JButton("Click") ;

jp.add(tf) ;

jp.add(b) ;

contentpane.add(jp,BorderLayout.SOUTH) ;

contentpane.add(drawpanel,BorderLayout.CENTER) ;
}

public void paint(Graphics g)
{

}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Does the applet extend "Applet" or "JApplet"? I don't think "Applet" would know enough about Swing components to draw a JPanel properly.

Please use the "CODE" button to surround code of any length with code tags to preserve its formatting.

I'm moving the thread to the AWT/Swing/GUI forum, since there seems to be nothing applet-specific about the question.
[ March 07, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The only obvious difficulty is this:
When you override the paint method be sure to call super.paint so the JApplet can draw
itself and its child components.

A suggestion for graphic components:
The JApplet (top–level container) has no way of knowing that the graphic component
(RectanglePanel) has custom drawing/graphics on it. It asks RectanglePanel for its display
size which is computed by the layout manager (of RectanglePanel) as it lays out the child
components. Since it has no children RectanglePanel reports its default (JPanel) size of
(10, 10). Being mounted in the center section of a BorderLayout will expand it to fill the
available space but if it needs more room it will need to provide a size hint for its
parent. You can do this by calling setPreferredSize(Dimension d) inside the class
(constructor) or on an instance of the class, ie, RectanglePanel drawpanel in the applet.
Or you can override the getPreferredSize method in the class and return the desired
Dimension for display. You could move your rects initialization into the
constructor and compute the Dimension width and height at the same time.
Rohan Pujari
Greenhorn

Joined: Feb 27, 2007
Posts: 24
Thanks for the suggestions.

I have made the layout in the applet as border layout as you can see that i have an important line of code missing



when i added this line to the applet the panel is now being displayed so its working fine thanks for all your help.

Rohan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem displaying panel in an applet