Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree1Likes

Thread: Troubles with rendering

  1. #1
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Question Troubles with rendering

    In this game I want to drawString based on keyPressed and only render that string when some key is pressed(to save memory).
    But whatever i try to fix this it ends with a error or it does not work like intended.I sucessfully get typed keys, I can print them but when it comes to rendering it doesnt work. Trying really hard to fix this, searched for the solution online but I cant get it to work. Please help
    Java Code:
    public class Game extends JPanel{
    	static String word;
    	private static Graphics g;
    	private static boolean running = true;
    	 static MyPanel mp = new MyPanel();
    	static boolean next= true;
    
    	public static void Display(String title) {
    
    		JFrame frame;
    		frame = new JFrame(title);
    		frame.setSize(640, 480);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.add(new MyPanel());
    		frame.add(mp);
    		frame.addKeyListener(new keyManager());
    
    	}
    
    	public static void main(String[] args) {
    		Display("Type Racer v1.0");
    		
    	}
    	public static class MyPanel extends JPanel{
    	public void MyPanel() {
    		setBackground(Color.gray);
    	//	getWord();
    		
    		
    	}	
    	
    @Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		g.setColor(Color.RED);
    		g.setFont(new Font("tahoma", Font.BOLD, 30));
    		g.drawString("TYPE: " + getWord(), 200, 200);
    		
    		g.drawString("HEY: " + keyManager.getTyped(), 200, 400);
    		
    	}
    public static void painttext(Graphics g){
    	g.drawString("HEY: " + keyManager.getTyped(), 200, 400);
    }
    
    	public String getWord() {
    		int maxlength = 5;
    		
    		final String chars = "abcdefghijklmnopqrstuvwxyz";
    		word = "";
    		
    		while (word.length() < maxlength) {
    			Random rand = new Random();
    			int x = rand.nextInt(26);
    			word = word + chars.charAt(x);
    		}
    		System.out.println("TYPE: " + word);
    
    	
    		return word;
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    it ends with a error or it does not work like intended
    Please provide the details of errors or explain what it does and what you want it to do differently.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by Norm View Post
    Please provide the details of errors or explain what it does and what you want it to do differently.
    My random word gets refreshed every update or tick, or it doesnt show anything at all.Not sure how to explain it. Tried multiple things but it doesnt work.
    I want it to render the string getWord() only once, and again if i type the word correctly.
    But i want to render the string getTyped() every time i press some key or atleast make a loop that locks the ticks/frame.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    My random word gets refreshed every update or tick, or it doesnt show anything at all.Not sure how to explain it. Tried multiple things but it doesnt work.
    Please explain what you want the program to do.
    I want it to render the string getWord() only once,
    Is the "random word" the same as the String from getWord()? The paintComponent method will need to draw it every time the paintComponent method is called.

    want to render the string getTyped() every time i press some key
    A call to the repaint() method will cause the paintComponent() method to be called.

    Note the code should override paintComponent not paint.

    If the KeyListener was implemented as an inner class, it would have access to the GUI components to be able to call their repaint method.

    Get rid of all the static attributes. Only main() should be static.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Here is the keyManager class witch contains the getTyped
    Java Code:
    public  class keyManager implements KeyListener {
    	int keys = 0;
    	static String userWord = "";
    
    	public void keyPressed(KeyEvent e) {
    		String key;
    		Game.next = true;
    		int keyCode = e.getKeyCode();
    		key = KeyEvent.getKeyText(keyCode);
    		// System.out.println(key);
    		userWord = userWord + key;
    		System.out.println(userWord);
    		keys++;
    		if(keys == 5)
    		{
    			userWord = "";
    			keys = 0;
    		
    		}
    		
    	
    	}
    
    	public static String getTyped() {
    		return userWord;
    	}
    I want the code to display 2 things: 1 string witch contains the word the player needs to type, and 1 string that displays what keys did player press.
    I can display the first string correctly, but the second one doesnt not refresh, so I want to repaint that string again.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    Ok, before going any further, the fixes I recommended need to be done:
    Note the code should override paintComponent not paint.

    If the KeyListener was implemented as an inner class, it would have access to the GUI components to be able to call their repaint method.

    Get rid of all the static attributes. Only main() should be static.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by Norm View Post
    Ok, before going any further, the fixes I recommended need to be done:
    I changed paint to paintComponent.
    Not sure what do you mean with that. KeyListener is implemented in another class file, keyManager, then I add the keyManager to the main frame.
    But from what I know I need to have it static if I want to call a method from somewhere else.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    I need to have it static
    No. You can use a reference to an instance of the class. For example from your code:
    Java Code:
            int keyCode = e.getKeyCode();  // call the getKeyCode method using the class reference e
    If you make the KeyListener an inner class, it can call repaint to have the new String drawn
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by Norm View Post
    No. You can use a reference to an instance of the class. For example from your code:
    Java Code:
            int keyCode = e.getKeyCode();  // call the getKeyCode method using the class reference e
    If you make the KeyListener an inner class, it can call repaint to have the new String drawn
    This is how my code looks now, when I implemented keylistener to MyPanel class:
    Java Code:
    public class Game extends JPanel {
    	static String word;
    	private static Graphics g;
    	private static boolean running = true;
    
    	public static void Display(String title) {
    
    		JFrame frame;
    		frame = new JFrame(title);
    		frame.setSize(640, 480);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.add(new MyPanel());
    		frame.addKeyListener(new keyManager());
    
    	}
    
    	public static void main(String[] args) {
    		Display("Type Racer v1.0");
    
    	}
    
    	public static class MyPanel extends JPanel implements KeyListener {
    		public void MyPanel() {
    			setBackground(Color.gray);
    			// getWord();
    
    		}
    
    		@Override
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.setFont(new Font("tahoma", Font.BOLD, 30));
    			g.drawString("TYPE: " + getWord(), 200, 200);
    			g.drawString("HEY: " + getTyped(), 200, 400);
    
    		}
    
    		int keys = 0;
    		String userWord = "";
    
    		public void keyPressed(KeyEvent e) {
    			String key;
    			int keyCode = e.getKeyCode();
    			key = KeyEvent.getKeyText(keyCode);
    			// System.out.println(key);
    			userWord = userWord + key;
    			System.out.println(userWord);
    			repaint();
    			keys++;
    			if (keys == 5) {
    				userWord = "";
    				keys = 0;
    
    			}
    
    		}
    
    		public String getTyped() {
    			return userWord;
    		}
    
    		public void keyReleased(KeyEvent e) {
    		}
    
    		public void keyTyped(KeyEvent e) {
    
    		}
    	}
    But it still doesnt repaint when I press some keys..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Troubles with rendering

    Why does Game extend JPanel?

    Normally you would add any components to the JFrame and any inside panels before calling pack() and then calling setVisible().
    This ensures that everything has been sized correctly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    Get rid of the use of static. Only main needs to be static.

    Change the Display method to be the constructor for the class. Call the constructor from the main() method.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by Norm View Post
    Get rid of the use of static. Only main needs to be static.

    Change the Display method to be the constructor for the class. Call the constructor from the main() method.
    Ok, I did it, my code looks like this now:
    Java Code:
    public class Game extends JPanel {
    	
    	private Graphics g;
    	
    	
    	public Game(String title) {
    
    		JFrame frame;
    		frame = new JFrame(title);
    		frame.setSize(640, 480);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.add(new MyPanel());
    		frame.addKeyListener(new keyManager());
    
    	}
    
    	public static void main(String[] args) {
    		new Game("Type Racer v1.0");
    
    	}
    
    	public class MyPanel extends JPanel implements KeyListener {
    		public void MyPanel() {
    			setBackground(Color.gray);
    			// getWord();
    
    		}
    
    		@Override
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.setColor(Color.RED);
    			g.setFont(new Font("tahoma", Font.BOLD, 30));
    			g.drawString("TYPE: " + getWord(), 200, 200);
    			g.drawString("HEY: " + getTyped(), 200, 400);
    			//System.out.println("REPAINT");
    		}
    
    		int keys = 0;
    		String userWord = "";
    
    		public void keyPressed(KeyEvent e) {
    			String key;
    			int keyCode = e.getKeyCode();
    			key = KeyEvent.getKeyText(keyCode);
    			// System.out.println(key);
    			userWord = userWord + key;
    			System.out.println(userWord);
    			repaint();
    			keys++;
    			if (keys == 5) {
    				userWord = "";
    				keys = 0;
    
    			}
    
    		}
    
    		public String getTyped() {
    			return userWord;
    		}
    
    		public void keyReleased(KeyEvent e) {
    			repaint();
    		}
    
    		public void keyTyped(KeyEvent e) {
    
    		}
    	}
    
    	public  String getWord() {
    		int maxlength = 5;
    		 String word;
    		final String chars = "abcdefghijklmnopqrstuvwxyz";
    		word = "";
    
    		while (word.length() < maxlength) {
    			Random rand = new Random();
    			int x = rand.nextInt(26);
    			word = word + chars.charAt(x);
    		}
    		System.out.println("TYPE: " + word);
    
    		return word;
    	}
    
    }
    But it still does not solve my issue.

  13. #13
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by Tolls View Post
    Why does Game extend JPanel?

    Normally you would add any components to the JFrame and any inside panels before calling pack() and then calling setVisible().
    This ensures that everything has been sized correctly.
    That is a part that I forgot to delete since MyPanel extends it.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Troubles with rendering

    Perhaps it is because you are adding an instance of your old keyManager class to JFrame. Your key management routines are now in MyPanel.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by jim829 View Post
    Perhaps it is because you are adding an instance of your old keyManager class to JFrame. Your key management routines are now in MyPanel.

    Regards,
    Jim
    Tried adding a new keylistener ( MyPanel) to JFrame, it's still the same.

  16. #16
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Tried adding a repaint to getTyped
    Java Code:
    public String getTyped() {
    			repaint();
    			return userWord;
    			
    		}
    Now it properly repaints but it my random word gets refreshed too...

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Troubles with rendering

    Your MyPanel "constructor" is a method (get rid of void).
    You need to save a copy of the current word and only display that in paintComponent. Only
    get another word when some special event happens.

    Regards,
    Jim
    Last edited by jim829; 08-19-2016 at 05:51 PM. Reason: grammar
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Troubles with rendering

    Quote Originally Posted by jim829 View Post
    Your MyPanel "constructor" is a method (get rid of void).
    You need to save an copy of the current word and only display that in paintComponent. Only
    get another word when some special event happens.

    Regards,
    Jim
    I changed MyPanel to constructor, changed getWord to makeWord and made getter and setter for it, getWord() and setWord().
    But when i press keys they dont show up on the gui, but they get printed in the console.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Troubles with rendering

    Are you building the word properly and displaying it in paintComponent?
    Are you repainting properly? When you change the code you may be
    making subtle changes which affect other areas.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Troubles with rendering

    Tried adding a repaint to getTyped
    Not a good place if getTyped is called from the paintComponent() method. The effect would be an endless loop: repaint causes call to paintComponent which calls repaint and on and on

    Call repaint when a change has been made that requires a new drawing of the GUI. For example when the users presses a key
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. HashMap Troubles
    By Cod in forum New To Java
    Replies: 5
    Last Post: 02-09-2014, 10:52 AM
  2. String to int troubles
    By Chuckling_atom in forum New To Java
    Replies: 13
    Last Post: 11-27-2011, 06:38 PM
  3. Clock Troubles
    By brycepower1 in forum New To Java
    Replies: 55
    Last Post: 05-12-2011, 12:33 PM
  4. Char troubles
    By diggdude in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 03:55 PM
  5. Integer troubles again...
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 09-20-2009, 02:32 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
  •