Results 1 to 6 of 6
  1. #1
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default Trying to use Graphics draw methods

    I'm having trouble drawing simple graphics.

    This is in my Shuffleboard class
    Java Code:
    Board board = new Board("Shuffleboard");
    board.setLocation(100, 100);
    board.setResizable(false);
    board.setVisible(true);
    This is in my Board class
    Java Code:
    boardCanvas = new BoardCanvas();
    this.add(boardCanvas);
    This is in my BoardCanvas class
    Java Code:
    super.paintComponent(g);
    		
    g.setColor(Color.black);
    for (Weight w : weights){
    	g.fillOval(200, 200, 50, 50);
    	//g.drawString("Test", 100, 100);
    	System.out.println("Bleh");
    }
    I know its executing because it is printing Bleh to the console. But no oval is being drawn. There appears to be no error.

    My BoardCanvas class extends JPanel - and my Shuffleboard class extends JFrame. Would this be an issue?

    Any help is appreciated,
    Lang

  2. #2
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Ok - I fixed the graphics issues.

    However, I can't get the graphics to clear with dispose()

    Am I doing something wrong? Is it a different function?

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class ShuffleBoard extends JFrame {
        // If Board is a model class and not part
        // of the componeent hierarchy.
        Board board = new Board("Shuffleboard");
        BoardCanvas boardCanvas;
    
        ShuffleTest() {
            boardCanvas = new BoardCanvas();
            this.add(boardCanvas);
            board.seLocation(100, 100);
            board.setResizable(false);
            board.setVisible(true);
        }
    }
    
    class Board {
        // This could be some kind of model class.
        // If so you may not need a reference to
        // it in the ShuffleBoard class.
        // If it is designed as part of the component
        // hierarchy see below.
    }
    
    class BoardCanvas extends JPanel {
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.black);
            for (Weight w : weights) {
                g.fillOval(200, 200, 50, 50);
    //            g.drawString("Test", 100, 100);
                System.out.println("Bleh");
            }
        }
    }
    If Board is part of the component hierarchy:
    Java Code:
    public class ShuffleTest extends JFrame {
        Board board = new Board("Shuffleboard");
    
        ShuffleTest() {
            this.add(board);
            board.seLocation(100, 100);
            board.setResizable(false);
            board.setVisible(true);
        }
    }
    
    class Board extends Some_JComponent {
        BoardCanvas boardCanvas;
    
        Board() {
            boardCanvas = new BoardCanvas();
            this.add(boardCanvas);
        }   
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    can't get the graphics to clear with dispose()
    Don't use dispose to clear graphics.
    Set up your painting methods to draw the current state of its enclosing class (the graphic component). Control the state of this class from your event code in this and/or other classes.

  5. #5
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    I'm having trouble understanding what you said, hardwired.

    When the mouse is released I'd like all the graphics to disappear.

    So I tried this:

    Java Code:
    public void mouseReleased(MouseEvent e){		
    	this.remove(boardCanvas);
    				
    	startShot = null;
    	endShot = null;
    }
    startShot and endShot are two irrelevant Point variables.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Then try adding a call to revalidate
    Java Code:
    this.remove(boardCanvas);
    revalidate()
    which tells the parent component to do a new layout.

Similar Threads

  1. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 08:11 AM
  2. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 12:22 PM
  3. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 AM
  4. how to draw in Java
    By Heather in forum AWT / Swing
    Replies: 2
    Last Post: 07-12-2007, 11:01 AM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

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