Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree1Likes

Thread: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

  1. #21
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by KevinWorkman View Post
    Don't call them from inside paintComponent(). Why would you have to call them in the middle of painting?
    Where can I put it instead?

  2. #22
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by iKeirNez View Post
    Where can I put it instead?
    I don't really know how to answer this, since the only code you've provided is your painting code. You want to call setBackground() when you want to change the background color. You want to call repaint() when you want to trigger a repaint. I don't know how else to explain it.

    Do you see why it doesn't make sense to call those things while you're painting? Say you're painting a fence, right.. But every time you get halfway through painting the fence, somebody tells you to change the fence's color or repaint the fence, so you start over. Then you get halfway through, and the same thing happens. Will you ever finish painting the fence?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #23
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by KevinWorkman View Post
    I don't really know how to answer this, since the only code you've provided is your painting code. You want to call setBackground() when you want to change the background color. You want to call repaint() when you want to trigger a repaint. I don't know how else to explain it.

    Do you see why it doesn't make sense to call those things while you're painting? Say you're painting a fence, right.. But every time you get halfway through painting the fence, somebody tells you to change the fence's color or repaint the fence, so you start over. Then you get halfway through, and the same thing happens. Will you ever finish painting the fence?
    I see what you mean, I have tried putting the repaint() method in numerous places but it doesn't work. Here is all the code I have in that class.

    Java Code:
    package me.iKeirNez.FirstGame;
    
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    public class Keying extends JPanel {
    	
    	public Rectangle character;
    	
    	Font font = new Font(Options.font, Font.TRUETYPE_FONT,Options.fontSize);
    	
    	public int charW = Options.CharacterWidth;
    	public int charH = Options.CharacterHeight;
    	
    	public int spawnX = Options.spawnX;
    	public int spawnY = Options.spawnY;
    	
    	public boolean right = false;
    	public boolean left = false;
    	public boolean forward = false;
    	public boolean backward = false;
    	public boolean respawn = false;
    	public boolean invertColors = false;
    	
    	public Keying(Display f, Images i){
    		character = new Rectangle(spawnX, spawnY, charW, charH);
    		
    		f.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e){
    				
    				if (e.getKeyCode() == KeyEvent.VK_D){
    					right = true;
    				}
    				
    				if (e.getKeyCode() == KeyEvent.VK_A){
    					left = true;
    				}
    				
    				if (e.getKeyCode() == KeyEvent.VK_W){
    					forward = true;
    				}
    				
    				if (e.getKeyCode() == KeyEvent.VK_S){
    					backward = true;
    				}
    				
    				if (e.getKeyCode() == KeyEvent.VK_ENTER){
    					respawn = true;
    				}
    				
    				if (e.getKeyCode() == KeyEvent.VK_I){
    					if (invertColors){
    						invertColors = false;
    					} else {
    						invertColors = true;
    					}
    				}
    				
    			}
    			
    			public void keyReleased(KeyEvent e){
    				if (e.getKeyCode() == Options.rightControl){
    					right = false;
    				}
    				
    				if (e.getKeyCode() == Options.leftControl){
    					left = false;
    				}
    				
    				if (e.getKeyCode() == Options.forwardControl){
    					forward = false;
    				}
    				
    				if (e.getKeyCode() == Options.backwardControl){
    					backward = false;
    				}
    				
    				if (e.getKeyCode() == Options.respawnControl){
    					respawn = false;
    				}
    			}
    			
    		});
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		if (invertColors){
    			this.setBackground(Options.rectColor);
    			g.setColor(Options.background);
    		} else {
    			this.setBackground(Options.background);
    			g.setColor(Options.rectColor);
    		}
    		g.fillRect(character.x, character.y, character.width, character.height);
    		
    		if(right){
    			character.x += 1;
    		}
    		
    		if (left){
    			character.x -=1;
    		}
    		
    		if (forward){
    			character.y -= 1;
    		}
    		
    		if (backward){
    			character.y += 1;
    		}
    		
    		if(respawn){
    			character.x = spawnX;
    			character.y = spawnY;
    		}
    		
    		g.setColor(Options.fontColor);
    		g.setFont(font);
    		g.drawString((Options.text + Main.version) , Options.textX, Options.textY);
    	}
    	
    }
    It's getting a lot of the settings from the config file,

    Java Code:
    package me.iKeirNez.FirstGame;
    
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    
    public class Options {
    	
    	// Version of the "game"
    	public static double version = 0.3;
    	
    	// Width and Height of the Rectangle
    	public static int CharacterWidth = 24;
    	public static int CharacterHeight = 36;
    	
    	// Where should the Rectangle (re)spawn
    	public static int spawnX = 180;
    	public static int spawnY = 180;
    	
    	// Size of the window
    	public static int windowWidth = 600;
    	public static int windowHeight = 400;
    	
    	// The title of the window, the version number will be added to the end.
    	public static String windowTitle = "iKeirNez's First Java Game - version ";
    	
    	// Controls
    	public static int rightControl = KeyEvent.VK_D;
    	public static int leftControl = KeyEvent.VK_A;
    	public static int forwardControl = KeyEvent.VK_W;
    	public static int backwardControl = KeyEvent.VK_S;
    	public static int respawnControl = KeyEvent.VK_ENTER;
    	
    	// Allows the user to resize the screen
    	public static boolean resizable = true;
    	
    	// About at the Bottom Right of Screen
    	public static String text = "iKeirNez's First Game - Version ";
    	public static String font = "Comic Sans MS";
    	public static int fontSize = 20;
    	public static Color fontColor = Color.WHITE;
    	public static int textX = 266;
    	public static int textY = 360;
    	
    	// Background and Rectangle Color Settings
    	public static Color background = Color.BLACK;
    	public static Color rectColor = Color.RED;
    
    }
    Thanks
    Keir

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    it doesn't work
    Where did you try putting the call to repaint()?
    After what event or processing do you want the paint method to be called? That would be the place to call repaint().
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by Norm View Post
    Where did you try putting the call to repaint()?
    After what event or processing do you want the paint method to be called? That would be the place to call repaint().

    I just tried this, I thought it would work but it didn't, I can't seem to call the repaint() method outside of the paintComponent section.

    Java Code:
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		if (invertColors){
    			this.setBackground(Options.rectColor);
    			g.setColor(Options.background);
    		} else {
    			this.setBackground(Options.background);
    			g.setColor(Options.rectColor);
    		}
    		g.fillRect(character.x, character.y, character.width, character.height);
    		
    		if(right){
    			character.x += 1;
    			repaint();
    		}
    		
    		if (left){
    			character.x -=1;
    			repaint();
    		}
    		
    		if (forward){
    			character.y -= 1;
    			repaint();
    		}
    		
    		if (backward){
    			character.y += 1;
    			repaint();
    		}
    		
    		if(respawn){
    			character.x = spawnX;
    			character.y = spawnY;
    			repaint();
    		}
    		
    		g.setColor(Options.fontColor);
    		g.setFont(font);
    		g.drawString((Options.text + Main.version) , Options.textX, Options.textY);
    	}

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    You should NOT call repaint() from inside of a paint method. That has been explained several times already.

    I can't seem to call the repaint() method outside of the paintComponent section.
    Please explain.
    Show the code and the error message.
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by Norm View Post
    You should NOT call repaint() from inside of a paint method. That has been explained several times already.


    Please explain.
    Show the code and the error message.
    I finally managed to figure this out, I had to move the paint method near the top of the class and then call the repaint methods inside the keyPressed and keyRelease methods.

    Thanks
    Keir

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

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by iKeirNez View Post
    I had to move the paint method near the top of the class
    The location of a method is source code is irrelevant.

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

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 11:16 AM
  2. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 06:54 AM
  3. Changing text color in SWT
    By ourimaler in forum SWT / JFace
    Replies: 1
    Last Post: 06-02-2010, 02:08 PM
  4. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 08:29 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 10:51 AM

Tags for this Thread

Posting Permissions

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