Results 1 to 15 of 15
- 08-22-2013, 07:50 PM #1
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
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(); } } } }
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() } } } }
EDIT: does in fact work! Just not wen called from main using objectname.paintPixel(2,2)Last edited by Xyexs; 08-22-2013 at 10:57 PM.
- 08-22-2013, 08:04 PM #2
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-22-2013, 08:06 PM #3
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: Draw a rectangle when you press a button?
Okay, thanks :D
- 08-22-2013, 08:12 PM #4
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-22-2013, 08:22 PM #5
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
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
- 08-22-2013, 08:29 PM #6
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!
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-22-2013, 08:29 PM #7
Re: Draw a rectangle when you press a button?
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-22-2013, 09:10 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Draw a rectangle when you press a button?
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-22-2013, 10:01 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 08-22-2013, 10:46 PM #10
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: Draw a rectangle when you press a button?
Stole the topic- se OP
Edit: thats a bad idea, making a new topicLast edited by Xyexs; 08-22-2013 at 11:10 PM.
- 08-22-2013, 11:25 PM #11
- 08-23-2013, 08:22 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 08-23-2013, 08:41 AM #13
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 impairedIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-23-2013, 04:04 PM #14
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 08-23-2013, 04:08 PM #15
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Draw a rectangle when you press a button?
Deja Vu. I see DB's dupe detector needs new batteries.
http://www.java-forums.org/new-java/...g-weirdly.html"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
Rectangle/Ellipse won't draw
By Danieldcc in forum SWT / JFaceReplies: 1Last Post: 03-07-2012, 08:51 AM -
Draw rectangle in a frame
By Mothrat in forum Java 2DReplies: 0Last Post: 12-13-2010, 11:52 AM -
Draw Shape later of Press Button, other implementation., where is mistake?
By joseluisbz in forum Java 2DReplies: 0Last Post: 05-20-2010, 09:18 PM -
Draw String in Rectangle
By DavidG24 in forum AWT / SwingReplies: 3Last Post: 05-20-2009, 08:05 AM -
How to Draw a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 12:09 AM
Bookmarks