Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: SWING:- GUI And Image Problems.

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Exclamation SWING:- GUI And Image Problems.

    So I'm using NetBeans 8.0 IDE, and I've compiled my main class (YuGiOh Game I'm working on):-

    /*
     * Copyright (c) 2014 Jack Andrew Loudon, All Rights Reserved.
     * Author: Jack Andrew Loudon, 2014
     */
     
    package com.github.bewd.yugioh.main;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.math.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    @SuppressWarnings("serial")
    public class Main extends JPanel{
     
        public static final String imgPathCard = "img\\cards\\";
        public static final String imgPathLogo = "img\\logos\\";
        public static final String fontPath = "fonts\\";
        private static final int appVersionMajor = 0;
        private static final double appVersionMinor = 1.3;
        private static final String appName = "Yu-Gi-Oh!: Trading Card Game";
        private static final String appStage = "Alpha";
        private static final String versionString = appName + " (" + appVersionMajor + "." + appVersionMinor + " " + appStage + ")";
        private BufferedImage sdk1, sdk2, sdk3, sdk4, sdk5, sdk6, sdk7, sdk8, sdk9, sdk10, bc;
     
        public Main() {
     
            try {   
                sdk1 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk001.png"));
                sdk2 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk002.png"));
                sdk3 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk003.png"));
                sdk4 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk004.png"));
                sdk5 = ImageIO.read(getClass().getResourceAsStream(imgPathCard + "sdk005.png"));
            } 
            catch (IOException e) {
                Logger LogErr = Logger.getLogger(Main.class.getName());
                System.err.println(appName + " " + "Caught IOException: " + e.getMessage());
            }
     
        }
     
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(sdk1, 0, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk2, Card.cardlong, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk3, Card.cardlong * 2, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk4, Card.cardlong * 3, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk5, Card.cardlong * 4, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
        }
     
    	public static void main(String[] args) {
                JFrame frame = new JFrame(versionString);
                frame.setSize(1020, 680);
                frame.setVisible(true);
                frame.setResizable(false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
     
                ArrayList<MonsterCard> deck1 = new ArrayList(50);
                ArrayList<MonsterCard> deck2 = new ArrayList(50);
                deck1.add(CardListKaiba.BlueEyesWhiteDragon);
                deck1.add(CardListKaiba.HitotsuMeGiant);
                deck1.add(CardListKaiba.RyuKishin);
                deck1.add(CardListKaiba.TheWickedWormBeast);
                deck1.add(CardListKaiba.BattleOx);
                deck1.add(CardListKaiba.KoumoriDragon);
                deck1.add(CardListKaiba.JudgeMan);
                deck1.add(CardListKaiba.RogueDoll);
                deck1.add(CardListKaiba.Kojikocy);
                deck1.add(CardListKaiba.Uraby);
                deck2.add(CardListYugi.MysticalElf); //Other Player
                deck2.add(CardListYugi.FeralImp);
                deck2.add(CardListYugi.WingedDragon1);
                deck2.add(CardListYugi.SummonedSkull);
                deck2.add(CardListYugi.BeaverWarrior);
                deck2.add(CardListYugi.DarkMagician);
                deck2.add(CardListYugi.Gaia);
                deck2.add(CardListYugi.CurseOfDragon);
                deck2.add(CardListYugi.CelticGuardian);
                deck2.add(CardListYugi.MammothGraveyard); //End Of Other Player     
                ArrayList<MonsterCard> monsterfield1 = new ArrayList(5);
                ArrayList<MonsterCard> monsterfield2 = new ArrayList(5);
                ArrayList<SpellCard> magicfield1 = new ArrayList(5);
                ArrayList<SpellCard> magicfield2 = new ArrayList(5);
                ArrayList<MonsterCard> hand1 = new ArrayList(5);
                ArrayList<MonsterCard> hand2 = new ArrayList(5);
                int lifepoints1 = 2000;
                int lifepoints2 = 2000;
                Random r = new Random();
                int randomPos1 = r.nextInt(deck1.size());
                int randomPos2 = r.nextInt(deck2.size());
                int drawHandSize = 3;
     
                System.out.println("Drawing Hand from Deck...");
                for(int playerOneDraw = -1; playerOneDraw < drawHandSize; playerOneDraw++){
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                }
                for(int playerTwoDraw = -1; playerTwoDraw < drawHandSize; playerTwoDraw++){
                    hand2.add(deck2.get(randomPos2));
                    deck2.remove(randomPos2);
                }
     
                while(lifepoints1 > 0|| lifepoints2 > 0){
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                    System.out.println("Initiating Draw Phase:");
                    System.out.println("Your hand consists of:");
                    for(int i = 0; i < hand1.size(); i++){
                        System.out.println(hand1.get(i).toString() + "");
                    }
     
                System.out.println("Initiating Main Phase 1:");
                System.out.println("Which monster would you like to summon?");
                Scanner monSummon = new Scanner(System.in);
                int whichMonsterPOS1 = monSummon.nextInt();
                monsterfield1.add(hand1.get(whichMonsterPOS1));
                hand1.remove(whichMonsterPOS1);
                for(int i = 0; i<monsterfield1.size(); i++){
                    System.out.println(monsterfield1.get(i).toString() + " has been summoned");
                }   
     
                frame.setVisible(true);
                System.out.println("Your side of the Field contains: " + monsterfield1);
                break;
            }
        }
    }

    But when I run the jar, it doesn't show the images on the JFrame, I removed
    frame.add(new Main());
    from my main class; as it caused errors, I wonder if this is affecting it?

    Also I'm getting occasional following Array IndexOutofBounds Exceptions (At different instances):

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    	at com.github.bewd.yugioh.main.Main.main(Main.java:105)
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    	at com.github.bewd.yugioh.main.Main.main(Main.java:110)

    Thanks in advance.

    UPDATE: When I removed the
    frame.add(new Main());
    I noticed that the image does now not show in NetBeans anymore either; but when I add this code back in, it works fine in NetBeans but when I try to run it from a "java -jar" command from a batch file it does not work; Also I've set the Batch File to collect info what's happening in a text file, here's what it says:

    Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at com.github.bewd.yugioh.main.Main.<init>(Main.java:37)
    	at com.github.bewd.yugioh.main.Main.main(Main.java:62)

    All the Image links are fine, I believe, and are relative to the Jar, not my system.

    Screenshot of Classes attached: Classes.jpg
    Last edited by BlueEyesWhiteDragon; June 5th, 2014 at 07:38 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: SWING:- GUI And Image Problems.

    You leave out many details that would be helpful:

    Are the images shown when you run from NetBeans?

    How did you build the .jar file?

    Copy and post EXACTLY and in their ENTIRETY the error messages you want help with.

  3. #3
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: SWING:- GUI And Image Problems.

    When I compiled the program, this is what I saw first as error message:-

    Cannot find symbol - variable Card;

    - in this code:

     g.drawImage(sdk1, 0, 0, Card.cardwidth / 5, Card.cardheight / 5, null);

    Check your program again. Maybe you forgot to declare it.

  4. #4
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SWING:- GUI And Image Problems.

    Sorry about the lack of information, the thread has been updated now. Thanks again.

    Quote Originally Posted by Abhilash View Post
    When I compiled the program, this is what I saw first as error message:-

    Cannot find symbol - variable Card;

    - in this code:

     g.drawImage(sdk1, 0, 0, Card.cardwidth / 5, Card.cardheight / 5, null);

    Check your program again. Maybe you forgot to declare it.
    No, my Card Class has the int: cardwidth set, and the int: cardheight set also.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: SWING:- GUI And Image Problems.

    For better help sooner, post an MCVE instead of a snippet or your whole project.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member HappySad-'s Avatar
    Join Date
    Jun 2014
    Location
    United Kingdom of Tea Lovers
    Posts
    6
    My Mood
    Cynical
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SWING:- GUI And Image Problems.

    Try adding this at the end of your rendering code? Or at the end of the class file...

    JFrame.validate()
    //or
    frame.validate()

    I dont know if this will help, but I used to get the components in the JFrame not show up due to this.

    Also you're getting the arrayoutofbounds exception due to having the for loops going outside the bounds.

    for(int i = 0; i<monsterfield1.size(); i++){
        System.out.println(monsterfield1.get(i).toString() + " has been summoned");
    }

    Try to change things like these to

    for(int i = 0; i < monsterfield1.size() - 1; i++)
    {
        System.out.println(monsterfield1.get(i).toString() + " has been summoned");
    }

    Notice how I minus one from the size of the array. This is due to the method returning the exact amount of items in the list +1 due to it not including the position 0.

    I am quite bad at explaining things but that might clear it up for you. Hope it helps..

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: SWING:- GUI And Image Problems.

    Quote Originally Posted by HappySad- View Post
    Try adding this at the end of your rendering code? Or at the end of the class file...

    JFrame.validate()
    //or
    frame.validate()

    I dont know if this will help, but I used to get the components in the JFrame not show up due to this.
    Perhaps what you want is to call repaint() instead of validate. But I did not see the entire code so it is very hard for me to make anything but a guess..

    Quote Originally Posted by HappySad- View Post
    Also you're getting the arrayoutofbounds exception due to having the for loops going outside the bounds.

    for(int i = 0; i<monsterfield1.size(); i++){
        System.out.println(monsterfield1.get(i).toString() + " has been summoned");
    }

    Try to change things like these to

    for(int i = 0; i < monsterfield1.size() - 1; i++)
    {
        System.out.println(monsterfield1.get(i).toString() + " has been summoned");
    }

    Notice how I minus one from the size of the array. This is due to the method returning the exact amount of items in the list +1 due to it not including the position 0.

    I am quite bad at explaining things but that might clear it up for you. Hope it helps..
    If the OP is using a regular data structure then this is wrong.
    For any regular array or list the code of the OP should work just fine without any exceptions. If you have 10 elements then you have to iterate over the elements from 0, 1, 2, ..., 9. That is, all indices below 10.

  8. #8
    Junior Member HappySad-'s Avatar
    Join Date
    Jun 2014
    Location
    United Kingdom of Tea Lovers
    Posts
    6
    My Mood
    Cynical
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SWING:- GUI And Image Problems.

    Quote Originally Posted by Cornix View Post
    Perhaps what you want is to call repaint() instead of validate. But I did not see the entire code so it is very hard for me to make anything but a guess..


    If the OP is using a regular data structure then this is wrong.
    For any regular array or list the code of the OP should work just fine without any exceptions. If you have 10 elements then you have to iterate over the elements from 0, 1, 2, ..., 9. That is, all indices below 10.
    Yes, yes you are right. .-. I thought he used something else.. ignore that

  9. #9
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SWING:- GUI And Image Problems.

    If you're interested in trying to fix this, well here's the zip file: Zippyshare.com

    Thanks if you do fix this, I am quite confused by this; it seems to escape my logic.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: SWING:- GUI And Image Problems.

    Nobody is going to download a shady zip file onto their computer and just do your work for you. Try asking a specific technical question and posting an MCVE, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. swing layout problems
    By xchan in forum AWT / Java Swing
    Replies: 6
    Last Post: April 21st, 2013, 05:29 PM
  2. swing applet for name problems
    By my21 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2013, 07:05 PM
  3. Swing (Problems)
    By LoganC in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 22nd, 2012, 03:48 PM
  4. [SOLVED] Problem with image in swing gui
    By Pusillus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 14th, 2012, 11:13 AM
  5. Help with swing GUI
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 03:54 AM

Tags for this Thread