Results 1 to 18 of 18
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Why my Graphics2D isn't painting.

    Hello, i would love to see if anybody can spot error in my code. I have been developing this code piece by piece and am sure everything is fine but it only prints white screen. Im sure i am saving and painting the images correctly.

    Java Code:
    package com.danger87.shoot;
    Alien.java
    
    package com.danger87.shoot;
    
    import java.awt.Image;
    import java.awt.Rectangle;
    
    import javax.swing.ImageIcon;
    
    public class Alien {
    
        private String craft = "alien.jpeg";
    
        private int x;
        private int y;
        private int width;
        private int height;
        private boolean visible;
        private Image image;
    
        public Alien(int x, int y) {
        	System.out.println("Alien");
            ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
            image = ii.getImage();
            width = image.getWidth(null);
            height = image.getHeight(null);
            visible = true;
            this.x = x;
            this.y = y;
        }
    
    
        public void move() {
            if (x < 0) 
                x = 400;
            x -= 1;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public boolean isVisible() {
            return visible;
        }
    
        public void setVisible(Boolean visible) {
            this.visible = visible;
        }
    
        public Image getImage() {
            return image;
        }
    
        public Rectangle getBounds() {
            return new Rectangle(x, y, width, height);
        }
    }
    Java Code:
    Board.java
    package com.danger87.shoot;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.*;
    
    import java.util.ArrayList;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Board extends JPanel implements ActionListener{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Timer timer;
    	private Ufo ufo;
    	private ArrayList<Alien>aliens;
    	private boolean in_game;
    	private int B_WIDTH;
    	private int B_HEIGHT;
    	
    	private int[][] pos = {
    			{2380, 29}, {2500, 59}, {1380, 89},
    			{780, 109}, {580, 139}, {680, 239},
    			{790, 259}, {760, 50},  {790, 150}, 
    			{980, 209}, {560, 45},  {510,  70}, 
    			{930, 159}, {590, 80},  {530, 60}, 
    			{940, 59}, {990, 30},   {920, 200}, 
    			{900, 259}, {660, 50},  {540, 90},
    			{810, 220}, {860, 20},  {740, 180}, 
    			{820, 128}, {490, 170}, {700, 30}, 
    	};
    	
    	public Board() {
    		addKeyListener(new TAdapter());
    		setFocusable(true);
    		setBackground(Color.BLACK);
    		setDoubleBuffered(true);
    		in_game = true;
    		
    		setSize(400, 300);
    		
    		ufo = new Ufo();
    		
    		initAliens();
    		
    		timer = new Timer(5, this);
    		timer.start();
    	}
    	
    	public void addNotify() {
    		super.addNotify();
    		B_WIDTH = getWidth();
    		B_HEIGHT = getHeight();
    	}
    	
    	public void initAliens() {
    		aliens = new ArrayList<Alien>();
    		
    		for (int i=0; i <pos.length; i++) {
    			aliens.add(new Alien(pos[i][0], pos[i][1]));
    		}
    	}
    	
    	public void paint (Graphics2D g) {
    		super.paint(g);
    		
    		if (in_game) {
    			Graphics2D g2d = (Graphics2D)g;
    			
    			if (ufo.isVisible()) 
    				g2d.drawImage(ufo.getImage(), ufo.getX(), ufo.getY(), this);
    			
    			ArrayList<Missile> ms = ufo.getMissiles();
    			
    			for (int i = 0; i < ms.size(); i++) {
    				Missile m = (Missile)ms.get(i);
    				g2d.drawImage(m.getImage(),m.getX(),m.getY(),this);
    			}
    			
    			for (int i = 0; i < aliens.size(); i++) {
    				Alien a = (Alien)aliens.get(i);
    				if (a.isVisible())
    					g2d.drawImage(a.getImage(),a.getX(),a.getY(),this);
    				}
    			g2d.setColor(Color.WHITE);
    			g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
    			
    		} else {
    			String msg = "Game Over";
    			Font small = new Font("Helvetica", Font.BOLD, 14);
    			FontMetrics metr = this.getFontMetrics(small);
    			
    			g.setColor(Color.white);
    			g.setFont(small);
    			g.drawString(msg, (B_WIDTH - metr.stringWidth(msg))/2,B_HEIGHT /2);
    		}
    		
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		
    		if (aliens.size() == 0) {
    			in_game = false;
    		}
    		
    		ArrayList<Missile> ms = ufo.getMissiles();
    		
    		for (int i = 0; i < ms.size(); i++) {
    			Missile m = (Missile) ms.get(i);
    			if (m.isVisible())
    				m.move();
    			else ms.remove(i);
    		}
    		for (int i = 0; i < aliens.size(); i++) {
    			Alien a = (Alien) aliens.get(i);
    			if (a.isVisible())
    				a.move();
    			else aliens.remove(i);
    		}
    		ufo.move();
    		checkCollison();
    		repaint();
    		
    	}
    	
    	public void checkCollison() {
    		Rectangle r3 = ufo.getBounds();
    		
    		for (int j = 0; j <aliens.size(); j++) {
    			Alien a = (Alien) aliens.get(j);
    			Rectangle r2 = a.getBounds();
    			
    			if(r3.intersects(r2)) {
    				ufo.setVisible(false);
    				a.setVisible(false);
    				in_game = false;
    			}
    		}
    		
    		ArrayList<Missile> ms = ufo.getMissiles();
    		
    		for (int i = 0; i < ms.size(); i++) {
    			Missile m = (Missile) ms.get(i);
    			
    			Rectangle r1 = m.getBounds();
    			
    			for (int j = 0; j <aliens.size(); j++) {
    				Alien a = (Alien) aliens.get(j);
    				Rectangle r2 = a.getBounds();
    				
    				if (r1.intersects(r2)) {
    					m.setVisible(false);
    					a.setVisible(false);
    				}			
    			}
    		}
    	}
    	private class TAdapter extends KeyAdapter {
    		public void keyReleased(KeyEvent e) {
    			ufo.keyReleased(e);
    		}
    		public void keyPressed(KeyEvent e) {
    			ufo.keyPressed(e);
    		}
    	}
    }
    Java Code:
    Ufo.java
    package com.danger87.shoot;
    
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    
    public class Ufo {
    	
    	private String ufo = "ufo.png";
    	
    	private int dx,dy,x,y,width,height;
    	private boolean visible;
    	private Image image;
    	private ArrayList<Missile> missiles;
    	
    	public Ufo() {
        	System.out.println("UFO");
    		ImageIcon ii = new ImageIcon(this.getClass().getResource(ufo));
    		image = ii.getImage();
    		width = image.getWidth(null);
    		height = image.getHeight(null);
    		missiles = new ArrayList<Missile>();
    		visible = true;
    		x = 40;
    		y = 60;
    	}
    	
    	public void move() {
    		x += dx;
    		y += dy;
    		
    		if (x < 1) { 
    			x = 1;
    		}
    			
    		if (y < 1) {
    			y = 1;
    		}
    	}
    		
    	public int getX() {
    		return x;
    	}
    	
    	public int getY() {
    		return y;
    	}
    	
    	public Image getImage() {
    		return image;
    	}
    	
    	public ArrayList<Missile> getMissiles() {
    		return missiles;
    	}
    	
    	public void setVisible(boolean visible) {
    		this.visible = visible;
    	}
    	
    	public boolean isVisible() {
    		return visible;
    	}
    	
    	public Rectangle getBounds() {
    		return new Rectangle (x, y, width, height);
    	}
    	
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    		
    		if (key == KeyEvent.VK_SPACE) {
    			fire();
    		}
    		if (key == KeyEvent.VK_LEFT) {
    			dx = -1;
    		}
    		if (key == KeyEvent.VK_RIGHT) {
    			dx = 1;
    		}
    		if (key == KeyEvent.VK_UP) {
    			dy = -1;
    		}
    		if (key == KeyEvent.VK_DOWN) {
    			dy = 1;
    		}    		
    	}
    	
    	public void fire() {
    		missiles.add(new Missile(x + width, y + height/2));
    	}
    	
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
    		
    		if (key == KeyEvent.VK_LEFT) {
    			dx = 0;
    		}
    		if (key == KeyEvent.VK_RIGHT) {
    			dx = 0;
    		}
    		if (key == KeyEvent.VK_UP) {
    			dy = 0;
    		}
    		if (key == KeyEvent.VK_DOWN) {
    			dy = 0;
    		}    		
    	}
    }

    Java Code:
    Missile.java
    package com.danger87.shoot;
    
    import java.awt.Image;
    import java.awt.Rectangle;
    
    import javax.swing.ImageIcon;
    
    public class Missile {
    	
    	// missle can only move left and right.
    	private int x,y;
    	private Image image;
    	boolean visible;
    	private int width,height;
    	
    	private final int BOARD_WIDTH = 390;
    	private final int MISSILE_SPEED = 2;
    	private String missile = "missile.jpeg";
    	
    	public Missile(int x, int y) {
        	System.out.println("Missile");
    		ImageIcon ii = new ImageIcon(this.getClass().getResource(missile));
    		image = ii.getImage();
    		visible = true;
    		this.x = x;
    		this.y = y;
    	}
    	
    	public Image getImage() {
    		return image;
    	}
    	
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    	
    	public boolean isVisible() {
    		return visible;
    	}
    	public void setVisible(Boolean visible) {
    		this.visible = visible;
    	}
    	public Rectangle getBounds() {
    		return new Rectangle(x,y,width,height);
    	}
    	
    	public void move() {
    		// missile moves at constant speed if > than board length, disappear.
    		x += MISSILE_SPEED;
    		if (x > BOARD_WIDTH)
    			visible = false;
    	}
    }


    Java Code:
    Collision.java
    package com.danger87.shoot;
    
    
    import javax.swing.JFrame;
    
    public class Collision extends JFrame {
    
        /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public Collision() {
            add(new Board());
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setTitle("Collision");
            setResizable(false);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Collision();
        }
    }
    Last edited by Bgreen7887; 07-17-2012 at 09:31 PM.

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    There is no Missiles class.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    I forgot .. Now updated. Sorry about that , still showing blank

  4. #4
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    There is no Alien Class either

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    That should do it.

  6. #6
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    It looks like this.getClass().getResource(ufo) is giving out a null Pointer for me.

  7. #7
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    Thats probably b.c you don't have the images along with the code. I have them all.

  8. #8
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    I think its not displaying anything because its never going to the paint method, even if you call repaint on the board.

  9. #9
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    I have no idea why it isn't calling paint method when i add(new Board()); I through a print statement inside constructor and you are right. It is not being called. How did you figure this out also?

  10. #10
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    I just used a debug breakpoint inside the paint method. It never stopped and went to debug mode.

  11. #11
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    Ok im pissed .. still looking for the bug. Maybe i need to become a become debugger, how did you learn?

  12. #12
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Why my Graphics2D isn't painting.

    I have already looked just your 2 classes so far, class Alien and class Board.

    My suggestions (and questions) are:

    1. in class Alien instead of using:
    Java Code:
    ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
    I think it's better to use:
    Java Code:
    ImageIcon ii = new ImageIcon(craft);
    2. in class Board
    Java Code:
    timer = new Timer(5, this);
    Don't you think that this timer is too fast because number 5 in this constructor is equal to 0.005 second?

  13. #13
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    The 5ms should be fine. For shits and giggles i changed it and it still didn't affect anything. Also Your correct about the imageicon but that doesn't change anything either as i have written previous projects following that same syntax. The baffling part is that the paint() method isn't being called, NOT once ! I don't understand how this is possible when im adding the board to the JFrame in the Collision class.

  14. #14
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Why my Graphics2D isn't painting.

    In your class Board:

    1. Your code:
    Java Code:
    public void paint (Graphics2D g) {
    	super.paint(g);
    I think that it should be Graphics instead of Graphics2D in this part of code.

    Also Your correct about the imageicon but that doesn't change anything either as i have written previous projects following that same syntax
    I'm still looking your code sorry cause I'm slow.

  15. #15
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    Take your time and thanks. Maybe i should be a little slower when i code to avoid is mayheim !!

  16. #16
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    Btw YOU WERE CORRECT ABOUT THE (Graphic2D g) instead of Graphics2D ;). Everything is now appearing .. I have to edit the collision bc i can shoot the missiles but it doesn't remove the aliens as planned haha YOU ARE A GOOD MAN SIR!

  17. #17
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Why my Graphics2D isn't painting.

    ^That's great. Thanks for compliments.

  18. #18
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Re: Why my Graphics2D isn't painting.

    Your welcome much! Another question. Im thinking of having a restart option, once the game over is displayed. I kinda have an idea but it seems dirty. Copy the constructor or Collision to another method and then call that method once game is over. There has to be a better way to do this ...

Similar Threads

  1. Painting with graphics2d
    By Alkor in forum New To Java
    Replies: 3
    Last Post: 03-20-2012, 04:22 AM
  2. Need help in Graphics2D
    By Yakkut in forum New To Java
    Replies: 1
    Last Post: 03-30-2011, 09:05 PM
  3. Graphics2D repaint bug
    By NextEpisode1 in forum Java 2D
    Replies: 10
    Last Post: 02-10-2011, 01:50 PM
  4. graphics to graphics2d casting
    By rocklikeits99 in forum New To Java
    Replies: 2
    Last Post: 09-22-2010, 04:12 AM
  5. Graphics2D setRenderingHint not working
    By Crazy Caveman in forum Java 2D
    Replies: 2
    Last Post: 08-27-2010, 07:47 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
  •