Results 1 to 4 of 4
  1. #1
    Monster is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default How to get this paint?

    I want to draw the character when i call the charWriter method but it doesn't work. What am i doing wrong?
    And, when i call the repaint() method it only calls the paintComponent method right?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class HangmanOutput extends JPanel{
    	private Graphics f;
    	private int charNo;
    	private int startX=10;
    	private int startY=30;
    	private int lineLength=5;
    	private int distance=10;
    	private int charHeight=10;
    	
    	public HangmanOutput(int length){
    		JFrame f = new JFrame();
    		f.setTitle("Hangman");
    		f.getContentPane().add(this);
    		f.setSize(200, 150);
    		f.setVisible(true);
    		charNo=length;
    	}
    	
    	public void paintComponent(Graphics g){
    		f=g;
    		f.setColor(Color.black);
    		for(int i=0;i<charNo;i++){
    			f.drawLine(startX + i*distance, startY, startX+lineLength+i*distance, startY);
    		}
    	}
    	
    	public void charWriter(char c, int index){
    		f.setColor(Color.black);
    		f.drawString(c+"", startX+index*distance, startY-charHeight);
    	}
    	
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to get this paint?

    Java Code:
    public void paintComponent(Graphics g){
        f=g;
        // etc
    This doesn't look right. Basically you can't really (and usefully) save the Graphics instance g for use later.

    Methods like paintComponent() are designed to be called from outside the methods you write. They get called by the Swing subsystem whenever it decides that your application needs to repaint itself. This makes sense because your application may need to repaint because of reasons outside its control or knowledge (like other application windows being closed and, thereby, making your application window visible). In effect you delegate the decision to repaint to the Swing subsystem, and your application repaints when it's told to.

    The Graphics instance g is only "good" for the method to which it's passed as part of this process. In particular calling methods on it outside the Swing painting mechanism will not result in anything getting painted - as you've found.

    If your program itself wants to have some painting done it (1) sets aspects of its state (like the character and the index) then (2) calls repaint() which tells the Swing painting system that it wants a repaint. Then it waits. Later the painting system will result in a call to paintComponent() and this method use the values of character and index that were stored previously to paint in the appropriate way.

    ---

    I hope the foregoing makes some sense as a "high level" description of what happens (and why what you wrote won't result in anything happening). But it isn't really intended to provide detail of approach or code. For that I suggest you find a decent Swing tutorial.

  3. #3
    Monster is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to get this paint?

    Thank you for the reply and the explanation, i found it very useful. I have one more question, how to "save" what i painted, i mean, if i add another character the last one will be deleted and i don't want that. Here is the modified code:

    Java Code:
    public class HangmanOutput extends JPanel{
    	private HangmanModel model;
    	private int charNo;
    	private int startX=10;
    	private int startY=30;
    	private int lineLength=5;
    	private int distance=10;
    	private int charHeight=10;
    	private char c=' ';
    	private int index;
    	
    	public HangmanOutput(HangmanModel c){
    		model =c;
    		JFrame f = new JFrame();
    		f.setTitle("Hangman");
    		f.getContentPane().add(this);
    		f.setSize(200, 150);
    		f.setVisible(true);
    		charNo = model.getLength();
    	}
    	
    	public void paintComponent(Graphics f){
    		f.setColor(Color.black);
    		for(int i=0;i<charNo;i++){
    			f.drawLine(startX + i*distance, startY, startX+lineLength+i*distance, startY);
    		}
    		f.drawString(c+"", startX+index*distance, startY-charHeight);
    	}
    	
    	public void charWriter(char d, int position){
    		index=position;
    		c=d;
    		repaint();

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to get this paint?

    You could have an array (or List of some sort) of char. The charWriter() just appends the char. I don't know how the index works: if it just increments then the paint method can figure it out, otherwise charWriter() will have to save the value (as part of an array of int).

Similar Threads

  1. Paint everything at once?
    By Lionlev in forum AWT / Swing
    Replies: 11
    Last Post: 10-15-2012, 04:01 AM
  2. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 06:52 AM
  3. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 05:15 AM
  4. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 09:00 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

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