Results 1 to 11 of 11
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Build project - paint component in Panel not visible.

    When i run my game in netbeans the playground is beeing drawn with a paintcomponent and i can play the game.

    When i build the poject and i start the Jar file. My paintComponent is the only thing thats not beeing drawn. Everything else works.

    What can this be?

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Any code that might help us help you?
    Without it we're merely guessing.

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi Tolls,

    I got two panels in one frame and this is the playgroundpanel.
    In the IDE it works perfrect, but outside of the IDE (JAR) the paintcomponent is not beeing drawn.

    Java Code:
    package view;
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    import controller.PlaygroundController;
    import controller.SpelHandler;
    import model.*;
    
    class PlaygroundPanel extends JPanel implements KeyListener
    {
        private Playground playground;
        private PlaygroundController control;
        private boolean useGrid;
        private Spel spel;
        private Image speler;
    
        /*
         * @type: constructor
         * @param: spel, itemHeight, itemWidth, rows, columns, useGrid
         */
        public PlaygroundPanel(Spel spel, int itemHeight, int itemWidth, int rows, int columns, boolean useGrid)
        {
            // zet de playground
            this.playground = spel.getPlayground();
    
            // op een nieuwe controler voor de playground
            this.control = new PlaygroundController(this.playground, itemHeight, itemWidth, rows, columns);
            this.useGrid = useGrid;
            //Defineer spel en frame voor de next playground
            this.spel = spel;
            this.speler = Toolkit.getDefaultToolkit().getImage("src/images/soko.png");
    
            // voeg keylistener toe aan dit panel
            addKeyListener(this);
        }
    
        /*
         * @type: override
         * @param: g
         */
        @Override
        public void paintComponent(Graphics g)
        {
            // zet achtergrond kleur en teken panel
            g.setColor(new Color(83, 83, 83));
            g.clearRect(0, 0, this.control.getPanelWidth(), this.control.getPanelHeight());
            g.fillRect(0, 0, this.control.getPanelWidth(), this.control.getPanelHeight());
    
            g.setColor(new Color(225,225,225));
            g.drawString("Aantal stappen: " + this.playground.speler.getAantalStappen(), 10, 20);
    
            // teken eindpunten (statisch)
            Image eindpunt = Toolkit.getDefaultToolkit().getImage("src/images/endpoint.png");
            for(Eindpunt item : this.playground.eindpuntenLijst)
                g.drawImage(eindpunt, item.getXIndex()*this.control.getItemWidth(), item.getYIndex()*this.control.getItemHeight(), this);
    
            // teken muren (statisch)
            Image muur = Toolkit.getDefaultToolkit().getImage("src/images/wall.png");
            for(Muur item : this.playground.murenLijst)
                g.drawImage(muur, item.getXIndex()*this.control.getItemWidth(), item.getYIndex()*this.control.getItemHeight(), this);
    
            // teken kisten (dynamisch)
            for(Kist item : this.playground.kistenLijst)
            {
                Image kist = Toolkit.getDefaultToolkit().getImage("src/images/chest.png");
    
                if(item.getOpEindpunt())
                    kist = Toolkit.getDefaultToolkit().getImage("src/images/endpoint_chest.png");
    
                g.drawImage(kist, item.getXIndex()*this.control.getItemWidth(), item.getYIndex()*this.control.getItemHeight(), this);
            }
    
            // teken speler (dynamisch)
            g.drawImage(this.speler, this.playground.speler.getXIndex()*this.control.getItemWidth(), this.playground.speler.getYIndex()*this.control.getItemHeight(), this);
    
            // controleer of grid wordt gebruikt
            if(this.useGrid)
            {
                g.setColor(Color.BLACK);
    
                // teken grid rijen
                for(int i = 0; i <= this.control.getRows(); i++)
                    g.drawLine(0, i*this.control.getItemHeight(), this.control.getPanelWidth(), i*this.control.getItemHeight());
    
                // teken grid kolommen
                for(int i = 0; i <= this.control.getColumns(); i++)
                    g.drawLine(i*this.control.getItemWidth(), 0, i*this.control.getItemWidth(), this.control.getPanelHeight());
            }
        }
    
    
        /*
         * @type: method
         * @param: key
         */
        public void keyPressed(KeyEvent key)
        {
            // definieer aantal stappen
            int aantalStappenOud = this.playground.speler.getAantalStappen();
    
            switch(key.getKeyCode())
            {
                // direction 1 (omlaag)
                case KeyEvent.VK_DOWN:
                    this.control.replaceVertical(1, 1);
                    this.speler = Toolkit.getDefaultToolkit().getImage("src/images/soko_down.png");
                    break;
    
                // direction 2 (omhoog)
                case KeyEvent.VK_UP:
                    this.control.replaceVertical(-1, 2);
                    this.speler = Toolkit.getDefaultToolkit().getImage("src/images/soko_up.png");
                    break;
    
                // direction 3 (links)
                case KeyEvent.VK_LEFT:
                   this.control.replaceHorizontal(-1, 3);
                   this.speler = Toolkit.getDefaultToolkit().getImage("src/images/soko_left.png");
                   break;
    
                // direction 4 (rechts)
                case KeyEvent.VK_RIGHT:
                    this.control.replaceHorizontal(1, 4);
                    this.speler = Toolkit.getDefaultToolkit().getImage("src/images/soko_right.png");
                    break;
    
                // verwijder muren...
                case KeyEvent.VK_Y:
                    this.playground.getMurenCoordinaten().clear();
                    break;
    
                // terug naar startpositie
                case KeyEvent.VK_HOME:
                    if(this.control.gotoStartPosition(this.playground.speler))
                        repaint();
                    break;
    
                // keer één stap terug
                case KeyEvent.VK_B:
                    if(this.control.doUndo(this.playground.speler))
                        repaint();
                    break;
            }
    
            // controleer of het aantal stappen is verhoogt
            if(aantalStappenOud < this.playground.speler.getAantalStappen())
            {
                System.out.println(this.playground.speler.getAantalStappen() + ": y " + this.playground.speler.getYIndex() + ", x" + this.playground.speler.getXIndex());
                repaint();
            }
    
            // controleer of alle kisten op eindpunten staan
            if(this.control.checkAlleKistenOpEindpunt())
            {
                SpelHandler spelHandler = new SpelHandler();
                spelHandler.gotoNextPlayground(spel);
            }
         }
    
        /*
         * @type: method
         * @param: key
         */
        public void keyTyped(KeyEvent key){}
    
        /*
         * @type: method
         * @param: key
         */
        public void keyReleased(KeyEvent key){}
    }
    Thanks for looking at it.

    Kind regards,

    André

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Write some debug code (System.out.println) in the paintComponent() and see whether it's finding the images.

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Tolls,

    Well than we have the answer already because he does show me the scorestring (drawString).
    So it looks like he doesn't show the images.
    How can i solve this?

    The directory of the images is placed in the src directory and in the IDE he has no problem displaying them.

    When i extract the *.jar i have them to in the same dir.

    Kind regards,

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Jars work differently.
    What you really need to do is getResourceAsStream, and use ImageIO rather than Toolkit. Also ensure that the images are in the jar file.

  7. #7
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Can you give an example with drawimage and my code.

    I've tried this and it gives a nullpointer
    Java Code:
      Image image=java.awt.Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("src/images/endpoint.png"));
            for(Eindpunt item : this.playground.eindpuntenLijst)
                g.drawImage(image, item.getXIndex()*this.control.getItemWidth(), item.getYIndex()*this.control.getItemHeight(), this);
    error:

    Uncaught error fetching image:
    java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:99)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:113)
    at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:240)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 36)
    Uncaught error fetching image:
    java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:99)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:113)
    at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:240)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 36)
    Last edited by aborgeld; 04-11-2011 at 06:06 PM.

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Solved the nullpointer. Let's see and its displaying the image.
    Lets see how it is outside of NB.

  9. #9
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    I have changed this:
    1. Path
    Java Code:
     
    BASEPATH = ".\\src\\images\\";
    2. getting the image:
    Java Code:
     
            if(eindpunt  == null) {
                    eindpunt  = readImage(BASEPATH + "endpoint.png");
            }
            else
            {
                System.out.println("Kan het bestand niet vinden - endpoint.png");
            }
    3. reading the image
    Java Code:
     static public Image readImage( String imgLocation ) {
            try {
                BufferedImage image = ImageIO.read(new File(imgLocation));
                return image;
            } catch (IOException ex) {
                throw new RuntimeException(ex.getMessage());
            }
        }
    The new solution works once again in the IDE but not when i clean in build, outside the IDE.

    Any ideas?

  10. #10
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Solved:
    URL url = getClass().getResource("/images/soko.png");
    Image image =Toolkit.getDefaultToolkit().getImage(url);
    this.speler = image;

  11. #11
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

Similar Threads

  1. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  2. Label not visible on panel
    By warchieflll in forum Advanced Java
    Replies: 4
    Last Post: 01-29-2011, 01:29 PM
  3. when i build my project it give following error
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-18-2010, 11:02 AM
  4. Custom component and paint outside of bounds
    By happy_hippie in forum AWT / Swing
    Replies: 2
    Last Post: 06-30-2010, 05:41 PM
  5. how to remove component from panel
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2009, 12:50 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
  •