Results 1 to 20 of 27
- 03-11-2012, 12:08 PM #1
hi, how to add a graphic component , say a line2D, to a swing?
hi, how to add a graphic component , say a line2D, to a swing?
my graphic line method is ..
and I have initialized Graphics using "import java.awt.Graphics2D" and geometry using "import java.awt.geom.*"Java Code:void paintComponent () { Graphics2D g1 = new Graphics2D (g); g1.drawLine(1,1,10,10); }
but where to call the method "paintComponent()" is it in the method "public static void main(String args[])" or in another method, and Does my codings show error in graphics initialization(Graphics2D g1 = new Graphics (g);) ?
and I always get the error "your graphics2D object "g" has NULL value
thx
regards
dhilip
Last edited by noobplus; 03-11-2012 at 01:54 PM.
- 03-11-2012, 01:23 PM #2
Re: hi, how to add a graphic component , say a line2D, to a swing?
What is the code you posted supposed to do? It does not make sense.
What is the datatype of the paintComponent arg that is passed on line 1?
What is the method's name?
- 03-11-2012, 01:46 PM #3
Re: hi, how to add a graphic component , say a line2D, to a swing?
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 01:49 PM #4
- 03-11-2012, 01:55 PM #5
- 03-11-2012, 02:04 PM #6
- 03-11-2012, 11:21 PM #7
Re: hi, how to add a graphic component , say a line2D, to a swing?
Get in the habit of using standard Java naming conventions!
- 03-12-2012, 01:28 PM #8
Re: hi, how to add a graphic component , say a line2D, to a swing?
thx, and how do we initialize graphics into the method "paintComponent()" ? that would help out my project. my project submission date is coming soon next week.
upto my knowledge,
// and one more doubt.. can the class that extends JComponent be a public class? (JComponent can work alone? or needs to be attached to a JFrame? If so, should it be initialized into the class extending JFrame? )Java Code:public class abc extends JComponent { void paintComponent(Graphics g) { Graphics output = new Graphics (g); output.drawLine(1,1,10,10); } }
regards
dhilipLast edited by noobplus; 03-12-2012 at 01:39 PM. Reason: adding my signature
- 03-12-2012, 02:10 PM #9
Re: hi, how to add a graphic component , say a line2D, to a swing?
Why try to create an unusable Graphics object (line 5)when there is good one passed to the method?
- 03-12-2012, 02:24 PM #10
Re: hi, how to add a graphic component , say a line2D, to a swing?
Better go through the whole Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-12-2012, 02:24 PM #11
Re: hi, how to add a graphic component , say a line2D, to a swing?
Last edited by noobplus; 03-12-2012 at 02:26 PM.
- 03-12-2012, 02:26 PM #12
Re: hi, how to add a graphic component , say a line2D, to a swing?
Have you compiled and executed it? What happens?
- 03-12-2012, 02:34 PM #13
Re: hi, how to add a graphic component , say a line2D, to a swing?
hi,
I added a main method to it in eclipse. (like given below)
ive mentioned the comment in the main method.Java Code:import javax.swing.*; import java.awt.*; public class framecomp extends JComponent { protected void paintComponent(Graphics g) { //Graphics output = new Graphics (g); g.drawLine(1,1,10,10); //output.drawLine changed to g.drawLine } public static void main(String args[]) { framecomp f = new framecomp(); f.paintComponent(null)// this line asks for a proper argument. and what arguments should i give? } }
regards
dhilip
- 03-12-2012, 02:53 PM #14
Re: hi, how to add a graphic component , say a line2D, to a swing?
What happens when you execute that code?
Did you read the link posted in post#10? Especially this part:
http://docs.oracle.com/javase/tutori...ing/index.html
It looks like you are making code up instead of following the recommendations shown in the tutorial
- 03-12-2012, 03:02 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: hi, how to add a graphic component , say a line2D, to a swing?
As Darryl suggests, I think the OP needs to sit down and go through the whole Swing tutorial.
Currently they're just making stuff up and hoping it works, with no idea at all how Swing fits together.Please do not ask for code as refusal often offends.
- 03-12-2012, 03:05 PM #16
Re: hi, how to add a graphic component , say a line2D, to a swing?
there was no class demonstrations.. and yet I found the title "Painting in Swing " paint() and repaint() are called in the class extending the Frame..
Painting in AWT and Swing
paint is in package "container" think so,..
thx for the link,
so should we initialize graphics in main() method?
regards
dhilip
- 03-12-2012, 03:08 PM #17
Re: hi, how to add a graphic component , say a line2D, to a swing?
- 03-12-2012, 03:18 PM #18
Re: hi, how to add a graphic component , say a line2D, to a swing?
In the tutorial there is code: SwingPaintDemo2 that draws a String:
g.drawString("This is my custom Panel!",10,20);
replace that with:
g.drawLine(1,1,100,100);
- 03-12-2012, 03:21 PM #19
Re: hi, how to add a graphic component , say a line2D, to a swing?
- 03-12-2012, 03:34 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: hi, how to add a graphic component , say a line2D, to a swing?
Please do not ask for code as refusal often offends.
Similar Threads
-
embed Flex component in Java Swing
By monireh in forum AWT / SwingReplies: 0Last Post: 02-24-2011, 02:28 PM -
Which swing component is suitable for TextEditor...?
By Dinesh_rockz in forum AWT / SwingReplies: 1Last Post: 02-23-2011, 05:41 AM -
swing component
By cahyadiakbar in forum AWT / SwingReplies: 0Last Post: 09-22-2010, 10:40 AM -
Require Links for free swing component
By Gajesh Tripathi in forum Advanced JavaReplies: 2Last Post: 08-11-2007, 10:24 PM -
Swing Graphic interface
By Daniel in forum AWT / SwingReplies: 2Last Post: 06-28-2007, 04:39 PM


6Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks