Results 1 to 3 of 3
Thread: Help with JFrame
- 07-04-2007, 04:35 AM #1
Senior Member
- Join Date
- Jun 2007
- Posts
- 114
- Rep Power
- 0
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?
ThanksJava 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
Albert:rolleyes:
- 07-04-2007, 04:38 AM #2
Member
- Join Date
- Jun 2007
- Posts
- 95
- Rep Power
- 0
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:p
- 07-04-2007, 04:44 AM #3
Senior Member
- Join Date
- Jun 2007
- Posts
- 111
- Rep Power
- 0
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:
Java 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); } }GreetingsJava 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 }
Eric
Similar Threads
-
Add an image to JFrame
By Eranga in forum AWT / SwingReplies: 4Last Post: 02-01-2010, 03:09 PM -
JFrame problem
By saytri in forum New To JavaReplies: 6Last Post: 01-11-2008, 05:12 PM -
JFrame Question ?
By Mindhunter74 in forum New To JavaReplies: 2Last Post: 12-21-2007, 10:45 PM -
help me with JFrame and JLabel
By michcio in forum New To JavaReplies: 5Last Post: 11-20-2007, 07:44 AM -
JFrame question
By Try2Live4God in forum New To JavaReplies: 2Last Post: 10-15-2007, 01:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks