Results 1 to 6 of 6
  1. #1
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default JPanel and graphics

    * stupid question inc* Hi all, please find below two classes below i could really do with some help with, the drawingApplication sets the panel up including sizes etc while the picture class controls graphic in this case a retangle and all im trying to get to happen currently is to display the graphic(g) and fall from the top of the panel to the bottom using the move while loop in there, the graphic doesnt even draw and im tired and at a loss as to why, please help :(
    Java Code:
    /*
     * DrawingApplication.java
     *
     */
    package tma02q3;
    
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    
    /**
     *
     * 
     */
    public class DrawingApplication extends JFrame
    {
        // instance variables provided
        private final int FRAME_WIDTH = 400;
        private final int FRAME_HEIGHT = 400;
        private Picture myPic = new Picture(FRAME_WIDTH, FRAME_HEIGHT);
    
        // TODO add further instance variables as required
    
        /**
         * Creates a new instance of DrawingApplication
         */
        public DrawingApplication(String title)
        {
            super(title);
            setSize(FRAME_WIDTH,FRAME_HEIGHT);
            this.setResizable(false);
            setLayout(new BorderLayout());
             //exiting on close
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            //TODO add registering of any event handlers here
    
        }
    
        //TODO complete update method
        //this method drives the application
        public void update()
        {
            //the idea here is to loop
            //and change the picture
    
            //this involves updating the content of the drawingPanel
            // by calling the panel's updatePictureState method
            while (true)
            {
                myPic.updatePictureState();
                repaint();
                try
                {
                    Thread.sleep(100);
                }
                catch(Exception e)
                {
                    System.exit(0);
                }
            }
    
        }
    
        // inner class on which to draw everything - you can add
        // to this class as much as you like
        private class DrawingPanel extends JPanel // provided
        {
            private Picture myPicture;
    
            // add further instance variables if required
    
            public DrawingPanel() // given
            {
                myPicture = new Picture(FRAME_WIDTH, FRAME_HEIGHT);
            }
    
            //this method is invoked automatically when repaint occurs in
            //the outer container
            public void paintComponent(Graphics g) // given
            {
                super.paintComponent(g);
    
                myPicture.draw(g); //This does the redrawing based on current state
            }
    
            // TODO complete updatePictureState method
            //this is about updating the state of elements in the picture
            //not about redrawing
            public void updatePictureState()
            {
                //TODO update state of myPicture
                myPicture.updatePictureState();
                
    
            }
    
            //If you want to use these methods, they
            //should only be called after the frame is visible
            //They tell you about the available width and height of the
            //drawing panel
            private int getAvailableWidth()
            {
                return getWidth() - getInsets().left - getInsets().right;
            }
    
            private int getAvailableHeight()
            {
                return getHeight() - getInsets().top - getInsets().bottom;
            }
    
            //TODO add further methods as required
        }
    
        //TODO add further (inner) classes as required
    }
    Java Code:
    /*
     * Picture.java
     *
     * This class includes a constructor that can be used to set up a Picture object
     * to allow drawing on an area of known size
     * The method draw receives a graphics context (could this be in the constructor?)
     *
     */
    
    package tma02q3;
    
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Color;
    
    
    
    /**
     *
     * @author M257 CT
     */
    public class Picture
    {
        //TODO add instance variables
        private int height,width;
        private final int moveMent = 4;
        private final int size = 10;
        private Point pos;
        private Color paintColour = Color.black;
    
        //TODO complete constructor
        public  Picture  (int  frameWidth,  int  frameHeight)
        {
            //TODO complete
            width=frameWidth;
            height=frameHeight;
            pos = new Point();
            pos.x = (int)(Math.random() * (width-size)) + size;
            pos.y = (int)(Math.random() * (height/2 -size)) + size;
            
        }
    
        //TODO add updatePictureState method
        // parameters can be removed/changed if required
        //This method does not do any drawing - it changes the state of the
        //picture object(s). The draw method does the drawing
        public void updatePictureState()
        {
            //TODO complete
            if (pos.y < height - size)
            {
                pos.translate(0, moveMent);
            }
        }
    
        //TODO complete draw method
        public void draw(Graphics  g)
        {
            //TODO complete
            g.setColor(paintColour);
            g.fillRect(pos.x-size, pos.y-size, 2 * size, 2 * size);
            
        }
    
    }
    Last edited by Fubarable; 03-02-2011 at 09:53 PM. Reason: Moderator Edit: quote tags changed to code tags

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

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

    Default

    Here:

    Java Code:
        //this method drives the application
        public void update()
        {
            //the idea here is to loop
            //and change the picture
    
            //this involves updating the content of the drawingPanel
            // by calling the panel's updatePictureState method
            while (true)
            {
                myPic.updatePictureState();
                repaint();
                try
                {
                    Thread.sleep(100);
                }
                catch(Exception e)
                {
                    System.exit(0);
                }
            }
    
        }
    The comment states that the code "drives" the application, but since you're doing a while (true) loop and a Thread.sleep on the main Swing thread, the EDT, you're effectively putting this thread to sleep. Since the EDT is responsible for Swing graphics and user interactions, your application is dead in the water.

    The solution, 1) don't do this, but instead use a Swing Timer -- the tutorials will show you how, and 2) read up on the just what the EDT is and why it's important: Concurrency in Swing

  4. #4
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    cheers for your reply mate, i did about of reading like you said and im suggesting something like the below which is probably wrong, this is still not initialising any graphic thou so im not sure if the fault doesnt lie somewhere else :( again thanks for your input
    public void update()
    {
    //the idea here is to loop
    //and change the picture

    //this involves updating the content of the drawingPanel
    // by calling the panel's updatePictureState method
    timer = new Timer(100, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    while (true)
    {
    myPic.updatePictureState();
    repaint();
    try
    {
    timer.start();
    }
    catch(Exception e2)
    {
    System.exit(0);
    }
    }
    }

    }
    );
    }

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

    Default

    Again, read the Timer tutorial before trying this. Again get rid of the while (true) stuff. If you read the timer tutorial, you'll see that you just don't need this. Also, please don't use quote tags where code tags belong when posting code.

  6. #6
    nevermiind is offline Member
    Join Date
    Apr 2010
    Posts
    15
    Rep Power
    0

    Default

    ok i have edited the method as below, still nothing at all, im sure something else is wrong and im being a muppet... any suggestions?

    public void update()
    {
    ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
    myPic.updatePictureState();
    repaint();
    }
    };
    Timer timer = new Timer(10, actionListener);
    timer.start();
    }

Similar Threads

  1. JPanel and Graphics zoom.
    By AcousticBruce in forum New To Java
    Replies: 3
    Last Post: 02-19-2011, 03:06 AM
  2. Can't display my graphics in a JPanel
    By handro1104 in forum AWT / Swing
    Replies: 2
    Last Post: 02-14-2011, 03:40 PM
  3. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 08:40 PM
  4. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 01:14 AM
  5. Newbie need help on JPanel graphics
    By junpogi in forum AWT / Swing
    Replies: 7
    Last Post: 10-21-2008, 07:44 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
  •