Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By RogerWilco

Thread: Drawing on wrong JPanel.

  1. #1
    RogerWilco is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Drawing on wrong JPanel.

    I am writing a Tetris program and for some reason the panel I am using to display the next piece is either drawing what is supposed to go in the score panel or the playing area while also displaying the next piece. It is seemingly randomly drawing the extra stuff. Does it maybe have something to do with me using the observer package?

    Java Code:
    private static final int MY_COLS = 10;
    	
    	private static final int MY_ROWS = 20;
    	
    	private static final int MY_MULT = 30;
    	
    	private static final int MY_TIMER_DELAY = 500;
    	
    	private final Board my_board = new Board(MY_COLS, MY_ROWS);
    	
    	private final JPanel my_playing_area = new PlayingArea(MY_COLS, MY_ROWS, MY_MULT);
    	
    	private final JPanel my_score_area = new ScoreArea(MY_COLS, MY_ROWS, 10);
    	
    	private final Timer my_timer = new Timer(MY_TIMER_DELAY, new TimerListener());
    	
    	private final JPanel my_next_area = new NextPieceArea(my_board.getNextPiece(), MY_COLS * 10, MY_COLS * 10);
    	
    	public Frame()
    	{
    		super();
    		addKeyListener(new KeyListener());
    		my_board.addObserver((Observer) my_playing_area);
    		my_board.addObserver((Observer) my_next_area);
    		my_board.addObserver((Observer) my_score_area);
    		my_board.addObserver(this);
    	}
    	
    	public void start()
    	{
    		makeMenuBar();
    		add(my_playing_area, BorderLayout.CENTER);
    		final JPanel panel = new JPanel(new GridLayout(2,1));
    		panel.add(my_next_area);
    		panel.add(my_score_area);
    		add(panel, BorderLayout.EAST);
    		my_timer.start();
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	}
    Last edited by Fubarable; 01-09-2012 at 12:04 AM. Reason: code tags added

  2. #2
    RogerWilco is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Drawing on wrong JPanel.

    Thanks. I just read the code tags.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Drawing on wrong JPanel.

    Hello and welcome to the Java-Forums!

    I have edited your post above to add [code][/code] which can make your posted code more readable.

    As to your actual question, I'm not sure about the others, but I'm not sure how to advise you given the information you've presented. I think that you'll need to tell us and show us (code) more. How much? It's hard to say, and that's really the art to asking a good question -- enough so that we'll be able to solve it, but not too much so that we're drowning in code. Best would be if you could create and post an SSCCE (see the link below).

    Again, welcome!

  4. #4
    RogerWilco is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Drawing on wrong JPanel.

    Thanks for the advice! It's hard for me to cut much away from the code without breaking it and it is about 9 classes so I wont post all that code. Hopefully this is enough for someone to see a problem.
    This is the class Im using to draw the next piece preview. When update is called the next piece is gotten from the Observable and painted. This is the pane that either (seemingly) randomly paints the upper left corner of the playing area or the score. In debug it always paints the correct number of Bricks but I cant figure out why it is adding the other info. Here is what it looks like when it paints the corner from the playing area. And thanks again for the advice Fubar!

    Drawing on wrong JPanel.-tetris.png
    Java Code:
    package view;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    import java.util.Observable;
    import java.util.Observer;
    
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    
    import model.Board;
    import model.Brick;
    import model.Piece;
    
    @SuppressWarnings("serial")
    public class NextPieceArea extends JPanel implements Observer
    {
    	private Piece my_next_piece;
    	
    	public NextPieceArea(final Piece the_piece, final int the_width, final int the_height)
    	{
    		super();
    		my_next_piece = the_piece;
    		setBorder(BorderFactory.createRaisedBevelBorder());
    		setPreferredSize(new Dimension(the_width, the_height));
    	}
    	
    	public void paintComponent(final Graphics the_graphics)
    	{
    		if (my_next_piece != null)
    	    {
    	      final Graphics2D g2d = (Graphics2D) the_graphics;
    	      final int multiplier = getSize().width / 4;
    	      for (Brick brick : my_next_piece.getBricks())
    	      { 
    	    	  g2d.setPaint(brick.color());
    	    	  g2d.fill(new Rectangle2D.Double((brick.location().x() + 1) * multiplier, (brick.location().y() + 1) * 
    	                                        multiplier, multiplier, multiplier));
    	      }
    	    }
    	}
    	
    	@Override
    	public void update(final Observable the_observable, final Object the_object)
    	{
    		final Board board = (Board) the_observable;
    		my_next_piece = board.getNextPiece();
    		repaint();
    	}
    
    }
    Last edited by RogerWilco; 01-09-2012 at 01:34 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default Re: Drawing on wrong JPanel.

    randomly paints the upper left corner of the playing area or the score
    How can the posted code draw the score? The only drawing method is a fill().
    Where is the code that draws the score?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Drawing on wrong JPanel.

    I'm with Norm. I still see nothing that explains your problem. You're going to have to do some sleuthing with a debugger and/or debug statements to try to tease out the cause of your problem first.

  7. #7
    RogerWilco is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Drawing on wrong JPanel.

    Sorry about that. I don't want to get any rebukes for posting too much code. I'll do some more detective work with the debugger. Thanks for looking guys!

  8. #8
    RogerWilco is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Drawing on wrong JPanel.

    Update. I figured out what I was doing. I didn't call super.paintComponent(the_graphics). I dont know why this would cause the problem I had but it fixed it.
    Fubarable likes this.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Drawing on wrong JPanel.

    Glad you've got things fixed, and many thanks for thinking of us and letting us know.

Similar Threads

  1. Possible to do JPanel drawing without extending?
    By SM2010 in forum AWT / Swing
    Replies: 7
    Last Post: 07-02-2010, 11:30 PM
  2. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 04:46 AM
  3. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 08:12 PM
  4. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 07:42 AM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •