Java...
Hi everyone, it''s been a while.
I wanted to pick someone''s brain about Java drawing in a JFrame. In particular, I''m trying to make a Tetris clone (starting off small). I have three classes so far, though that will likely change:
1. class Block which is extends JComponent
2. class Board which also extends JComponent
3. class Main which extends JFrame (and contains the main method)
My question is the following. How do I *move* the component once it''s been placed inside the frame? I can get everything to draw out like it should be drawn (using paintComponent(Graphics g) inside of class Block), but I have no idea how to move it. As you may have assumed, every block is a JComponent. If this was VB, I would just say JComponent.left = x; Is there something equivalent in Java? I looked at the javadocs and I can find nothing... Help would be appreciated,
Thanks all.
-mk
P.S. Is anyone else finding it very frustrating to program java''s gui? Of course the alternatives aren''t much better... sigh...
Gui layouts in Swing are almost always relative positions (hence BorderLayout, FlowLayout etc.) and so moving controls around with precision is difficult. However, for ''normal'' apps this has the advantage that Gui''s will resize automajically.
There are two ways around this: Either set a null layout on your JFrame.setLayout(null). This means that you can place controls by an absolute position and move them around easily. Something like JComponent.setPosition() I think.
Or use the Canvas or Window class to get a blank drawing surface. Then you can simply paint directly onto the components (something like Component.getGraphics() then use the methods of Graphics2D like drawImage).
There are two ways around this: Either set a null layout on your JFrame.setLayout(null). This means that you can place controls by an absolute position and move them around easily. Something like JComponent.setPosition() I think.
Or use the Canvas or Window class to get a blank drawing surface. Then you can simply paint directly onto the components (something like Component.getGraphics() then use the methods of Graphics2D like drawImage).
Sweet dude. I was in the process of figuring out how this works with the LayoutManager set to null. The methods to move around I think are setBounds(x, y, width, height). I think your Window or Canvas solutions are slightly more elegant though so I will try that next. In any case, thank you!!
Just outta curiosity, have you (or anyone else) made games in Java? Are my attempts futile? It''s still cool to learn, I guess, but I''m frustrated by c++ -- well, more frustrated that my college made the switch to java a few years ago and am now forced to use java. arghhhh
life goes on.
Thanks again,
-mk
-- WANTED: Infinite amount of monkeys willing to work for an infinite amount of time on infinitely many computers. Goal: write the best video game ever. Contact planetOfAPIs@imadork.com
Just outta curiosity, have you (or anyone else) made games in Java? Are my attempts futile? It''s still cool to learn, I guess, but I''m frustrated by c++ -- well, more frustrated that my college made the switch to java a few years ago and am now forced to use java. arghhhh
life goes on.
Thanks again,
-mk
-- WANTED: Infinite amount of monkeys willing to work for an infinite amount of time on infinitely many computers. Goal: write the best video game ever. Contact planetOfAPIs@imadork.com
quote:Original post by Phathead
Just outta curiosity, have you (or anyone else) made games in Java?
Hell yes, I prefer Java to C++. Most recently CounterSheep and still working on the Prism Engine:
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement