Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to pass data to the paintComponent method?

  1. #1
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default 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);
    	  }
    	
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: How to pass data to the paintComponent method?

    Override paintComponent, rather than paint.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default 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.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to pass data to the paintComponent method?

    Quote Originally Posted by DarrylBurke View Post
    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.
    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!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to pass data to the paintComponent method?

    Quote Originally Posted by Tohron View Post
    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.
    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.

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

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

    Much luck.
    DarrylBurke likes this.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to pass data to the paintComponent method?

    Quote Originally Posted by Tohron View Post
    Really, what I need it sample code
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default 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!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  2. Replies: 3
    Last Post: 02-04-2011, 06:30 AM
  3. paintComponent() Method straitjacket
    By oldalistair in forum New To Java
    Replies: 5
    Last Post: 09-11-2010, 12:06 AM
  4. Replies: 10
    Last Post: 02-28-2010, 01:52 PM
  5. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •