• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a Tetris clone  RSS feed

 
Timothybruce Williams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code as I'm starting.

My question is, how do I get my Cube to visualize in the JPanel? I've tried a bunch of add methods but they don't compile. Is there a proper method I can use to add an object to a JPanel?

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your Cube object have methods to make itself visible?
One way to do it is to call that method from inside paintComponent:-Note:
  • 1: protected access, not public.
  • 2: super call as first line to remove previous drawing after repaint call.
  • 3: Have duplicated graphics object with create() method in case the Cube method does something to the graphics which cannot be undone. An affine transform is the most likely suspect, especially shear.
  • 4: The object g is actually an instance of Graphics2D and you may need to cast it to Graphics2D.
  • Check all my syntax carefully as you try to implement it.
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your timer code, etc should be in a different class from the display. Override the paintComponent method only and the Timer etc belong elsewhere.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!