Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Use JFrame and a canvas

Thread view: 
Ben - 09 Feb 2006 19:41 GMT
Hi,
   Could anyone please advise me on how to achieve this.  At the
moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
Is it possible to draw using awt.graphics in to a sub window inside it?
If so what do I need to look at in order to do this?  If not then how
can something similar to this be achieved?

Thanks.

Ben.
Vova Reznik - 09 Feb 2006 19:48 GMT
> Hi,
>     Could anyone please advise me on how to achieve this.  At the
[quoted text clipped - 6 lines]
>
> Ben.

What is sub window?

You may use JPanel and its super JComponent method

public void paintComponent(Graphics g){
   // you may want to call super.paintComponent(g)
   // because painting in Swing is different than painting in awt
}
Ben - 09 Feb 2006 19:58 GMT
Sorry, what I mean is that I am tring to create a  program.  That has a
main window and sub windows inside it.  One of which can be drawn on.
Sort of like having multiple documents open is sub windows in microsoft
word.

Thanks.  

Ben.
Chris Lamb - 09 Feb 2006 22:05 GMT
> Sorry, what I mean is that I am tring to create a  program.  That has a
> main window and sub windows inside it.  One of which can be drawn on.
> Sort of like having multiple documents open is sub windows in microsoft
> word.

Do you mean with a JDesktopFrame and a JInternalFrame?

http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html

Chris
Rhino - 09 Feb 2006 19:53 GMT
> Hi,
>    Could anyone please advise me on how to achieve this.  At the
> moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
> Is it possible to draw using awt.graphics in to a sub window inside it?
> If so what do I need to look at in order to do this?  If not then how
> can something similar to this be achieved?

Are you sure you really want a java.awt.Canvas in the midst of your Swing
controls? It is much more common to see people draw on a JPanel, which is
the Swing equivalent of a Canvas.

That's exactly what I do in one of my applications. It was originally
written for AWT before Swing came along; then I converted the Canvas to a
JPanel. I don't think I changed the drawing code (g.drawLine(),
g.drawOval(), etc. etc.) very much if at all.

If you mix AWT controls with Swing controls in the same GUI you get a lot of
ugliness due to interactions between the heavyweight AWT controls and the
lightweight Swing controls.

--
Rhino
Ben - 09 Feb 2006 21:04 GMT
Thanks for your replies.

After looking around on the net, I found this.

/** Panel on which to draw two shapes. **/
class ShapesPanel extends JPanel
{
 public void paintComponent (Graphics g) {
   // First paint background
   super.paintComponent (g);

   // The context that is passed is actually
   // the Graphics2D sub-class of Graphics. So
   // we can cast it to Graphics2D and take advantage
   // of the additional tools that it has.
   Graphics2D g2 = (Graphics2D)g;

   // Options on the rendering, i.e. the actual pixel settings
   // of the drawing, can be chosen via RenderingHints
   g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

   // Create the two shapes, a circle and a square.
   Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
   Shape shape2 = new Rectangle2D.Double (65.0, 35.0, 70.0, 70.0);

   // Now draw the shapes.
   g2.draw (shape1);
   g2.draw (shape2);
 } // paintComponent

} // class ShapesPanel

After the frame and panel have been created and made visible.  Is it
possible to make the panel created using the above function redraw?
For example if the shapes specified in changed every second so.  Could
I have another function that kept telling  the panel to redraw.  Is
this possible? If so how?

Thanks.

Ben.
Vova Reznik - 09 Feb 2006 21:27 GMT
> Thanks for your replies.
>
[quoted text clipped - 38 lines]
>
> Ben.

repaint() will call paintComponent(g).
So replace your numbers
    Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
with variables
    Shape shape1 = new Ellipse2D.Double (?, ?, ?, ?);

update variables and call yourPanel.update()
Vova Reznik - 09 Feb 2006 21:29 GMT
> repaint() will call paintComponent(g).
> So replace your numbers
[quoted text clipped - 3 lines]
>
> update variables and call yourPanel.update()

Ups, repaint(), not update().
Sorry (
Roedy Green - 10 Feb 2006 04:23 GMT
>    Could anyone please advise me on how to achieve this.  At the
>moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
>Is it possible to draw using awt.graphics in to a sub window inside it?
> If so what do I need to look at in order to do this?  If not then how
>can something similar to this be achieved?

To custom paint for JFrames, paint on a JPanel and put your code in
paintComponent instead of paint.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ben - 10 Feb 2006 15:49 GMT
Hi,

I have now created a class that extends JPanel and I have my own code
in the paintcomponent that paints to a panel (similar layout of code to
above).  The class that initialises the JFrame and JPanel runs
repaint() every second and updates the panel just fine.
        However when I try to use a layout manager on the frame.
Everything appears but the panel doesn't get drawn. After playing
around I used the following code to add the panel and a button (cp is
the content pane of the frame).

cp.add(button1, BorderLayout.SOUTH);
cp.add(panel1, BorderLayout.NORTH);

When it runs I noticed that you can just see the top of one of the
circles that gets drawn on the frame. It is at the top of the panel.
This makes me think something is getting painted over the top of my
frame.

Does anyone have any suggestion of what I might be doing wrong?

Thanks.

Ben.
Thomas Weidenfeller - 10 Feb 2006 15:55 GMT
> Does anyone have any suggestion of what I might be doing wrong?

You code in paintComponent() (the code you didn't show), makes certain
assumptions which are not true.

Or, the JPanel can't be layed out properly, because it doesn't return
useful size values.

Or ... whatever, we can only guess.

/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/

Vova Reznik - 10 Feb 2006 16:02 GMT
> Hi,
>
[quoted text clipped - 20 lines]
>
> Ben.

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

You may try to put your panel into CENTER or set preferred size


Free Magazines

Get 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 ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.