Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By jim829

Thread: Draw a rectangle when you press a button?

  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Things acting weirdly?

    I stole this topic, original is found at bottom of post
    Hi!
    In order to learn i wrote this.
    Java Code:
    package xyexs.name.src;
    
    
    
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JPanel;
    
    
    
    
    public class Panel extends JPanel {
    
    
    
    
    	public int x = 0;
    	public int y = 0;
    
    
    
    
    	public void paintComponent(Graphics g) {
    		super.paintComponents(g);
    		g.setColor(Reference.BACKGROUND_COLOR);
    		g.fillRect(0, 0, Reference.WINDOW_X, Reference.WINDOW_Y);
    		g.setColor(Reference.SNAKE_COLOR);
    		g.fillRect(y, x, Reference.PIXEL_SIZE, Reference.PIXEL_SIZE);
    	}
    
    
    
    
    	public Panel() {
    		keyHandler handlerObject = new keyHandler();
    		setFocusable(true);
    		addKeyListener(handlerObject);
    	}
    
    
    
    
    	public Dimension getPreferredSize() {
    		return new Dimension(Reference.WINDOW_X, Reference.WINDOW_Y);
    	}
    
    
    
    
    	private class keyHandler extends KeyAdapter {
    		@Override
    		public void keyPressed(KeyEvent key) {
    			if (key.getKeyCode() >= 37 && key.getKeyCode() <= 40) {
    
    
    
    
    				switch (key.getKeyCode()) {
    
    
    
    
    					case KeyEvent.VK_LEFT: {
    						// left
    						x = x - 1;
    					}
    					case KeyEvent.VK_UP: {
    						// up
    						x = x + 1;
    					}
    					case KeyEvent.VK_RIGHT: {
    						// right
    						x = x + 1;
    					}
    					case KeyEvent.VK_DOWN: {
    						// down
    						y = y + 1;
    					}
    				}
    				repaint();
    			}
    		}
    	}
    }
    It's supposed to move the box in the direction of the arrow you press.




    The weird thing is that if i press up it ... see below...




    If i press... It moves...
    Up Down
    Right Right/down
    Down Right
    Left Nowhere








    Original post





    Java Code:
    package xyexs.name.src;
    
    import java.awt.Graphics;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JPanel;
    
    public class Panel extends JPanel {
    	public void paintPixel(int pixelX, int pixelY) {
    		Graphics g = getGraphics();
    		super.paintComponents(g);
    		g.setColor(Reference.SNAKE_COLOR);
    		g.fillRect(pixelX * Reference.PIXEL_SIZE,
    				pixelY * Reference.PIXEL_SIZE, pixelX * Reference.PIXEL_SIZE
    						+ Reference.PIXEL_SIZE, pixelY * Reference.PIXEL_SIZE
    						+ Reference.PIXEL_SIZE);
    	}
    
    	public Panel() {
    		keyHandler handlerObject = new keyHandler();
    		setFocusable(true);
    		setBackground(Reference.BACKGROUND_COLOR);
    		addKeyListener(handlerObject);
    	}
    
    	private class keyHandler extends KeyAdapter {
    		@Override
    		public void keyReleased(KeyEvent key) {
    			if (key.getKeyCode() >= 37 && key.getKeyCode() <= 40) {
    				paintPixel()
    			}
    		}
    	}
    }
    ^ the above code (as i expected) won't work, i want it to draw a "pixel" (which is a 50 X 50 rectangle), it just does NOTHING... help?

    EDIT: does in fact work! Just not wen called from main using objectname.paintPixel(2,2)
    Last edited by Xyexs; 08-22-2013 at 09:57 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Draw a rectangle when you press a button?

    That is not how you do custom painting. Read this instead: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    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. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Draw a rectangle when you press a button?

    Okay, thanks :D

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: Draw a rectangle when you press a button?

    And in general: never use getGraphics() of a Component. At least, not unless you have a very thorough understanding of the painting mechanism.

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

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Draw a rectangle when you press a button?

    After reading for a while... doesn't that run automatically?(the paintComponent method)
    That is why i did this.. which failed totally

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

    Default Re: Draw a rectangle when you press a button?

    Wow, you went through the custom painting tutorial in 18 minutes? The first time I read through it, it took me almost two hours!

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

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by Xyexs View Post
    After reading for a while... doesn't that run automatically?(the paintComponent method)
    That is why i did this.. which failed totally
    You either read very fast, or didn't try any of the examples, which should explain anything you're confused about.

    The paintComponent() method is called when the component needs to be painted, which can either be triggered by the OS or by the programmer by calling repaint().
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by DarrylBurke View Post
    Wow, you went through the custom painting tutorial in 18 minutes? The first time I read through it, it took me almost two hours!

    db
    Nor did I miss the "first time" reference. I have lost track of how many times I have read and re-read articles or white papers (and then read them again).

    Regards,
    Jim
    KevinWorkman likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by DarrylBurke View Post
    Wow, you went through the custom painting tutorial in 18 minutes?
    16 minutes according to my data.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Draw a rectangle when you press a button?

    Stole the topic- se OP

    Edit: thats a bad idea, making a new topic
    Last edited by Xyexs; 08-22-2013 at 10:10 PM.

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

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by JosAH View Post
    16 minutes according to my data.

    kind regards,

    Jos
    #2 at 10:34
    #5 at 10:52

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

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by DarrylBurke View Post
    #2 at 10:34
    #5 at 10:52
    #2 is Kevin; #3 should be 10.36 your time, so it's 16 minutes ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: Draw a rectangle when you press a button?

    I computed it from the time the link was supplied.

    Your approach is more correct.

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

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Draw a rectangle when you press a button?

    OP: your current problem is caused by your case statements falling through. Recommended reading: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Draw a rectangle when you press a button?

    Quote Originally Posted by KevinWorkman View Post
    OP: your current problem is caused by your case statements falling through. Recommended reading: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Deja Vu. I see DB's dupe detector needs new batteries.

    Things acting weirdly?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Rectangle/Ellipse won't draw
    By Danieldcc in forum SWT / JFace
    Replies: 1
    Last Post: 03-07-2012, 07:51 AM
  2. Draw rectangle in a frame
    By Mothrat in forum Java 2D
    Replies: 0
    Last Post: 12-13-2010, 10:52 AM
  3. Replies: 0
    Last Post: 05-20-2010, 08:18 PM
  4. Draw String in Rectangle
    By DavidG24 in forum AWT / Swing
    Replies: 3
    Last Post: 05-20-2009, 07:05 AM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 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
  •