Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Phathead

Java...

This topic is 5736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
that''s pretty sick dude! someday, i will reach that level... someday...... hehehe

-- 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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!