Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 05:35 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
Help with JFrame
In the Paint method of a JFrame I can call a method like drawRect( ) and it will work. But when I try to call a class method for drawing, it won't work. It creates the window but doesn't draw anything in it. Here is a simple example. It uses a class, Design, that just uses drawRect and drawOval. An applet can use the Design class and its methods just fine. Why can't the JFrame do the same?

Code:
import java.awt.*; import javax.swing.*; public class DrawTest extends JFrame { Design d; public DrawTest( ) { super( "Drawing Test" ); setSize(100, 100 ); setVisible( true ); } public void init( ) { d = new Design( ); } public void paint( Graphics g ) { super.paint( g ); d.draw( g ); } public static void main( String args[] ) { DrawTest application = new DrawTest( ); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end of main } // end of DrawTest
Thanks

Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 05:38 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
I am not sure if you could draw straight into a JFrame instance....though, I might be wrong.

I think most Java programmers use an instance of the Canvas class to draw on frames.

here is an example

Greetings

Felissa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 05:44 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
What you want to paint on, is not the JFrame, but on its contentPane.

This is more or less the way I always do it, by using JPanels:
Code:
import javax.swing.JFrame; class{ main{ //create JFrame JFrame frame = new JFrame ("A JFrame"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //create instance of drawing class //i guess you could do it on init().. Design d = new Design(); //add contentPane to frame //add drawings to contentPane frame.getContentPane().add(d); frame.pack(); frame.setVisible(true); } }
Code:
import javax.swing.JPanel; Design class extends JPanel{ public Design(){ //constructor //add initial values ... //set panel color and size setBackground (Color.gray); setPreferredSize (new Dimension(600,600)); } ...stuff public void paint(Graphics g) { //drawings go here super.paint(g); ....stuff } ...stuff }
Greetings

Eric
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add an image to JFrame Eranga AWT / Swing 2 03-14-2008 09:24 AM
JFrame problem saytri New To Java 6 01-11-2008 06:12 PM
JFrame Question ? Mindhunter74 New To Java 2 12-21-2007 11:45 PM
help me with JFrame and JLabel michcio New To Java 5 11-20-2007 08:44 AM
JFrame question Try2Live4God New To Java 2 10-15-2007 02:52 AM


All times are GMT +3. The time now is 06:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org