Results 1 to 8 of 8
- 07-28-2012, 06:33 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
How to pass data to the paintComponent method?
Please move if wrong forum.
Anyway, I'm trying to write a program to display a sinusoidal wave, which will employ user input (like the number of peaks and troughs). However, this runs into a problem since the paintComponent method only accepts a graphics object, and any attempt to modify the graphics object outside of paint component results in a crash. Any sample code showing how to make a simple variable (say, an int) available to the paintComponent method would be much appreciated. If this is not an option, then sample code showing an alternative way to display objects dependent on user-entered values would also be much appreciated.
Here's my code so far, if it helps:
Java Code:public class WaveDisplayMain extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("DrawRect"); frame.setSize(300, 200); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = frame.getContentPane(); contentPane.add(new WaveDisplayMain()); //frame.getGraphics().fillRect(30, 30, 40, 40); //g.setColor(Color.blue); // Cannot even set variables //g.fillRect(30, 30, 40, 40); //frame.paint(g); frame.show(); //frame.getGraphics().fillRect(30, 50, 80, 80); //frame.paint(frame.getGraphics()); } public void paint(Graphics g){ //int n; super.paintComponent(g); super.paint(g); g.setColor(Color.black); g.fillRect(10, 10, 30, 30); } }
- 07-28-2012, 07:19 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: How to pass data to the paintComponent method?
Override paintComponent, rather than paint.
- 07-28-2012, 08:26 AM #3
Re: How to pass data to the paintComponent method?
As Tolls said, override paintComponent(...) -- and invoke the super implementation only for the method you override. Not for any other painting or other method.
Maintain the needed variables as instance fields and call repaint() any time any of them are changed by user interaction.
db
edit: show() is deprecated. See the API for the method that is recommended to be used instead.
edit2: Adding a WindowListener to exit the program is old AWT code. Read up on the setDefaultCloseOperation(...) method. And the title can be passed as a parameter to the constructor of a JFrame.Last edited by DarrylBurke; 07-28-2012 at 08:29 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-09-2012, 01:14 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: How to pass data to the paintComponent method?
While I appreciate the help, I have only a year or so of (limited) experience with java - I've covered things like array lists, overriding functions & inheritance, and some basic coverage of graphics, but am unfamiliar with things such as instance fields.
Really, what I need it sample code, as I said in the OP. The task doesn't appear overly complicated - the example need only use an int(declared in main, and then employed in the overridden paint function) - and once I've got the means to do this I'll have all I need. I know it's asking a bit for someone to put in time for an anonymous stranger, but this really would be a great bit of assistance for me. Thanks!
-
Re: How to pass data to the paintComponent method?
An instance field is simply a non-static variable that has been declared in the class (not declared in a method or constructor), that's it.
And there's plenty of sample code to be found in this and other similar forum. Please feel free to search the forum for it and experiment with it.Really, what I need it sample code, as I said in the OP. The task doesn't appear overly complicated - the example need only use an int(declared in main, and then employed in the overridden paint function) - and once I've got the means to do this I'll have all I need.
Since the problem and question is yours, the anonymous stranger who should put in the most of the time and effort should be you. Please try to create your own code and if it doesn't work, post it here and ask questions about it.I know it's asking a bit for someone to put in time for an anonymous stranger, but this really would be a great bit of assistance for me. Thanks!
Much luck.
- 08-09-2012, 05:54 AM #6
Re: How to pass data to the paintComponent method?
Then search the net for tutorials. This forum can help you to understand stuff until you're capable of writing your own code. Here's one to start with: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
An instance field is a variable declared at the class level, outside of any methods or constructors, and is visible to code in any method or constructor.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-10-2012, 02:38 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: How to pass data to the paintComponent method?
Thanks for the help! I'm familiar with the concept of class variables, just not used to them being called instance fields. At any rate - things are now working - I can call a setter in the main, then use a getter in the paint function to access the variable. Now I can finally get moving on implementing the actual project. Thanks again for the assistance!
- 08-10-2012, 03:58 AM #8
Re: How to pass data to the paintComponent method?
For addressing instance fields of the same class, you don't need to go through getters and setters, unless they either have side effects (yuck!) or perform a validity check. You can assign or access the variable by its name.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Can i Pass a variable as a parameter from one method to other method
By Anagha in forum New To JavaReplies: 18Last Post: 04-18-2011, 05:39 AM -
repaint() does not recall an override method paintComponent(), why is that?
By ilovevcoffe in forum AWT / SwingReplies: 3Last Post: 02-04-2011, 06:30 AM -
paintComponent() Method straitjacket
By oldalistair in forum New To JavaReplies: 5Last Post: 09-11-2010, 12:06 AM -
JOptionPane when called from inside the paintComponent() method
By Y. Progammer in forum New To JavaReplies: 10Last Post: 02-28-2010, 01:52 PM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks