Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default [SOLVED] paint() method question

    Alright, so upon running the program, the paint method is automatically called, right?

    Now, that creates problems for me because inside my paint method I have a drawImage function and the image drawn is a variable found within another class. As a result, when I run the program, it doesn't work. However, if I replace the other classe's variable with a variable inside the same class as the paint function, everything works fine. I'm guessing this is because the paint method is ran BEFORE the variables from the other class are initialized. How can I go about fixing this? I have tried calling the method that initializes variables from the main(String[] args) method but to no avail.
    Last edited by supremegrandruler; 05-04-2014 at 05:26 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    That's the solution, have the other class do its initialization before its data is needed.

    How did you code it?
    What problem did you have?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: paint() method question

    Line #3 for testing purposes.

    Java Code:
    public static void main(String[] args) {
    		JFrame frame = new JFrame("Kings of War");
    		AssetLoader.initVariables();
    		frame.add(new Main());
    		frame.setSize(screenResolutionWidth, screenResolutionHeight);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setFocusable(true);
    	}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    What does that show? Does that code work or not?
    It looks like it calls a static method. Are the fields all static?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: paint() method question

    Well it didn't give errors when I compiled it but actually, I made a mistake in the code. It should be:

    Java Code:
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Kings of War");
    		CardData.initCardData();
    		frame.add(new Main());
    		frame.setSize(screenResolutionWidth, screenResolutionHeight);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setFocusable(true);
    	}
    Now though, it gives me this error (even though it didn't before):

    Exception in thread "main" java.lang.NullPointerException
    at com.kingsofwar.main.Field.<init>(Field.java:32)
    at com.kingsofwar.main.Main.<init>(Main.java:28)
    at com.kingsofwar.main.Main.main(Main.java:89)
    Everything is static.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    Exception in thread "main" java.lang.NullPointerException
    at com.kingsofwar.main.Field.<init>(Field.java:32)
    what is null on line 32?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: paint() method question

    Nothing. :(
    I'll post the code. Maybe it'll be easier.
    Java Code:
    package com.kingsofwar.main;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Main extends JPanel {
    
    	public static int screenResolutionWidth = 1366;
    	public static int screenResolutionHeight = 768;
    
    	public static boolean isPlayer1 = true;
    	public static int maxTileSize = 148; // TODO - Calculate maxTileSize based on game resolution.
    	public ArrayList<Card> cards = new ArrayList<Card>();
    	public static Image cardArt = new ImageIcon("lister.gif").getImage();
    	Field field;
    
    	public Main() {
    		field = new Field(this);
    		
    		for (int i = 0; i < 3; i++) {
    			Card c = new Card(i * 100, i * 100, i, true);
    			cards.add(c);
    		}
    	}
    
    	public ArrayList getCards() {
    		return cards;
    	}
    
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		// START PAINT BACKGROUND
    		setBackground(Color.GREEN);
    		// END PAINT BACKGROUND
    
    		// START PAINT FIELD.
    		int a = 0;
    		int b = 0;
    		while (a < 9) {
    			while (b < 5) {
    				if (a != 0 && a != 8) {
    					g.drawImage(
    							AssetLoader.TILE_ART[AssetLoader.tileValue],
    							a
    									* Main.maxTileSize
    									+ ((Main.screenResolutionWidth - Main.maxTileSize * 9) / 2),
    							(b * Main.maxTileSize)
    									+ ((getHeight() - Main.maxTileSize * 5) / 2),
    							Main.maxTileSize, Main.maxTileSize, null);
    				} else {
    					g.drawImage(
    							AssetLoader.playerArea,
    							a
    									* Main.maxTileSize
    									+ ((Main.screenResolutionWidth - Main.maxTileSize * 9) / 2),
    							(b * Main.maxTileSize)
    									+ ((getHeight() - Main.maxTileSize * 5) / 2),
    							Main.maxTileSize, Main.maxTileSize, null);
    				}
    				b += 1;
    			}
    			b = 0;
    			a += 1;
    		}
    		// END PAINT FIELD
    		ArrayList cards = getCards();
    		for (int i = 0; i < cards.size(); i++) {
    			Card c = (Card) cards.get(i);
    			// g.drawImage(cardArt, c.getX(), c.getY(), 100 , 100,null);
    			g.drawImage(CardData.CARD_ART[0], c.getX(), c.getY(), 100, 100,
    					null);
    			// g.setColor(Color.YELLOW);
    			// g.fillRect(c.getX(), c.getY(), 10, 5);
    		}
    	}
    
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Kings of War");
    		CardData.initCardData();
    		frame.add(new Main());
    		frame.setSize(screenResolutionWidth, screenResolutionHeight);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setFocusable(true);
    	}
    }
    (Field) I don't think the problem is in there.
    Java Code:
    package com.kingsofwar.main;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Field {
    
    	public Main game = null;
    	public int tileValue = 0;
    
    	public Field(Main game) {
    		this.game = game;
    
    		int a = 0;
    		int b = 0;
    		while (a < 9) {
    			while (b < 5) {
    				AssetLoader.TILE[a][b] = new Rectangle(
    						a
    								* Main.maxTileSize
    								+ ((Main.screenResolutionWidth - Main.maxTileSize * 9) / 2),
    						(b * Main.maxTileSize)
    								+ ((game.getHeight() - Main.maxTileSize * 5) / 2),
    						Main.maxTileSize, Main.maxTileSize);
    				b += 1;
    			}
    			b = 0;
    			a += 1;
    		}
    	}
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    What was null on line 32?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: paint() method question

    Nothing I haven't changed anything since posting the error.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    If nothing was null on line 32 there won't have been a NullPointerException.
    You need to find what was null and then add code to make sure it is not null when the code is executed.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: paint() method question

    Thanks I fixed it. Basically the error was I forgot to call the method to initialize the variables and then when I noticed the error, I deleted another method instead... Thanks a lot for the help though. :)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: paint() method question

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Paint method
    By subbus in forum Java 2D
    Replies: 5
    Last Post: 06-20-2012, 04:38 AM
  2. Paint method, need help
    By PeterPan in forum AWT / Swing
    Replies: 2
    Last Post: 01-15-2012, 01:35 AM
  3. paint method
    By larry_d1990 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 06:45 PM
  4. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 08:07 AM
  5. Java Paint Method?
    By leapinlizard in forum Java 2D
    Replies: 2
    Last Post: 02-11-2010, 08:01 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
  •