Results 1 to 6 of 6
  1. #1
    Thebigchalupa is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default need help with JPanel Images

    I am working on creating a pacman game but i am having trouble putting an Image into JPanel....
    any help is appreciated


    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    
    public class gameBoard extends JPanel 
    {
        SquareRx[][] squares;
        final int PAD = 20; 	
        
       
        public gameBoard() 
        {
        	
            int ROWS = 10;
            int COLS = 10;
            squares = new SquareRx[ROWS][COLS];
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j] = new SquareRx(i, j);
                }
            }
        }
    
        protected void paintComponent(Graphics g) 
        {
        	
        	
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/squares[0].length;
            double yInc = (double)(h - 2*PAD)/squares.length;
            
            g2.setPaint(Color.black);
            for(int i = 0; i <= squares[0].length; i++) 
            {
                double x = PAD + i*xInc;
                //g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            
            for(int i = 0; i <= squares.length; i++) 
            {
                double y = PAD + i*yInc;
                //g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
                //left column
                g.fillRect(20,20 ,35,35);
                g.fillRect(20,55 ,35,35);
                g.fillRect(20,90 ,35,35);
                g.fillRect(20,120 ,35,35);
                g.fillRect(20,155 ,35,35);
                g.fillRect(20,190 ,35,35);
                g.fillRect(20,220 ,35,35);
                g.fillRect(20,255 ,35,35);
                g.fillRect(20,290 ,35,35);
                g.fillRect(20,320 ,35,35);
                g.fillRect(20,355 ,35,35);
                g.fillRect(20,390 ,35,35);
                g.fillRect(20,420 ,35,35);
                g.fillRect(20,455 ,35,35);
                g.fillRect(20,490 ,35,35);
                //top row
                g.fillRect(55,20 ,35,35);
                g.fillRect(90,20 ,35,35);
                g.fillRect(120,20 ,35,35);
                g.fillRect(155,20 ,35,35);
                g.fillRect(190,20 ,35,35);
                g.fillRect(220,20 ,35,35);
                g.fillRect(255,20 ,35,35);
                g.fillRect(290,20 ,35,35);
                g.fillRect(320,20 ,35,35);
                g.fillRect(355,20 ,35,35);
                g.fillRect(390,20 ,35,35);
                g.fillRect(420,20 ,35,35);
                g.fillRect(455,20 ,35,35);
                g.fillRect(490,20 ,35,35);
                //bottom row
                g.fillRect(55,490 ,35,35);
                g.fillRect(90,490 ,35,35);
                g.fillRect(120,490 ,35,35);
                g.fillRect(155,490 ,35,35);
                g.fillRect(190,490 ,35,35);
                g.fillRect(220,490 ,35,35);
                g.fillRect(255,490 ,35,35);
                g.fillRect(290,490 ,35,35);
                g.fillRect(320,490 ,35,35);
                g.fillRect(355,490 ,35,35);
                g.fillRect(390,490 ,35,35);
                g.fillRect(420,490 ,35,35);
                g.fillRect(455,490 ,35,35);
                g.fillRect(490,490 ,35,35);
                //right column
                g.fillRect(490,55 ,35,35);
                g.fillRect(490,90 ,35,35);
                g.fillRect(490,120 ,35,35);
                g.fillRect(490,155 ,35,35);
                g.fillRect(490,190 ,35,35);
                g.fillRect(490,220 ,35,35);
                g.fillRect(490,255 ,35,35);
                g.fillRect(490,290 ,35,35);
                g.fillRect(490,320 ,35,35);
                g.fillRect(490,355 ,35,35);
                g.fillRect(490,390 ,35,35);
                g.fillRect(490,420 ,35,35);
                g.fillRect(490,455 ,35,35);
                g.fillRect(490,490 ,35,35);
                //top right
                g.fillRect(90,90 ,35,35);
                g.fillRect(90,120 ,35,35);
                g.fillRect(90,155 ,35,35);
                g.fillRect(120,90 ,35,35);
                g.fillRect(155,90 ,35,35);
                g.fillRect(90, 420, 35, 35);
                g.fillRect(90, 390, 35, 35);
                g.fillRect(90, 355, 35, 35);
                g.fillRect(120, 420, 35, 35);
                g.fillRect(155, 420, 35, 35);
                g.fillRect(420, 90, 35, 35);
                g.fillRect(390, 90, 35, 35);
                g.fillRect(355, 90, 35, 35);
                g.fillRect(420, 120, 35, 35);
                g.fillRect(420, 155, 35, 35);
                g.fillRect(390, 420, 35, 35);
                g.fillRect(420, 390, 35, 35);
                g.fillRect(420, 355, 35, 35);
                g.fillRect(420, 420, 35, 35);
                g.fillRect(355, 420, 35, 35);
                
               
               
           
            }
        }
    
        public static void main(String[] args) 
        {
            gameBoard test = new gameBoard();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(560,575);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class SquareRx 
    {
        private final int row;
        private final int col;
        private boolean occupied = false;
    
        public SquareRx(int r, int c) 
        {
            row = r;
            col = c;
        }
    
        public void setOccupied(boolean occupied) 
        {
            this.occupied = occupied;
        }
    
        public boolean isOccupied() 
        {
            return occupied;
        }
        
        
    }

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    There should be a g.drawImage(bla,bla,bla,bla) method... You can get the image with Image myImage=Toolkit.getDefaultToolkit().createImage(im ageName);...
    Who Cares... As Long As It Works...

  3. #3
    Thebigchalupa is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    I used what you told me
    everything looks good no errors
    but still no image is coming up >.<


    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    
    public class gameBoard extends JPanel 
    {
        SquareRx[][] squares;
        final int PAD = 20; 	
        [B]Image myImage=Toolkit.getDefaultToolkit().createImage("pacy.jpg");[/B]
       
        public gameBoard() 
        {
        	
            int ROWS = 10;
            int COLS = 10;
            squares = new SquareRx[ROWS][COLS];
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j] = new SquareRx(i, j);
                }
            }
        }
    
        protected void paintComponent(Graphics g) 
        {
        	 
        	 [B]g.drawImage(myImage,1,1,this);[/B]
        	
        	
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/squares[0].length;
            double yInc = (double)(h - 2*PAD)/squares.length;
            
            g2.setPaint(Color.black);
            for(int i = 0; i <= squares[0].length; i++) 
            {
                double x = PAD + i*xInc;
                //g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            
            for(int i = 0; i <= squares.length; i++) 
            {
                double y = PAD + i*yInc;
                //g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
                //left column
                g.fillRect(20,20 ,35,35);
                g.fillRect(20,55 ,35,35);
                g.fillRect(20,90 ,35,35);
                g.fillRect(20,120 ,35,35);
                g.fillRect(20,155 ,35,35);
                g.fillRect(20,190 ,35,35);
                g.fillRect(20,220 ,35,35);
                g.fillRect(20,255 ,35,35);
                g.fillRect(20,290 ,35,35);
                g.fillRect(20,320 ,35,35);
                g.fillRect(20,355 ,35,35);
                g.fillRect(20,390 ,35,35);
                g.fillRect(20,420 ,35,35);
                g.fillRect(20,455 ,35,35);
                g.fillRect(20,490 ,35,35);
                //top row
                g.fillRect(55,20 ,35,35);
                g.fillRect(90,20 ,35,35);
                g.fillRect(120,20 ,35,35);
                g.fillRect(155,20 ,35,35);
                g.fillRect(190,20 ,35,35);
                g.fillRect(220,20 ,35,35);
                g.fillRect(255,20 ,35,35);
                g.fillRect(290,20 ,35,35);
                g.fillRect(320,20 ,35,35);
                g.fillRect(355,20 ,35,35);
                g.fillRect(390,20 ,35,35);
                g.fillRect(420,20 ,35,35);
                g.fillRect(455,20 ,35,35);
                g.fillRect(490,20 ,35,35);
                //bottom row
                g.fillRect(55,490 ,35,35);
                g.fillRect(90,490 ,35,35);
                g.fillRect(120,490 ,35,35);
                g.fillRect(155,490 ,35,35);
                g.fillRect(190,490 ,35,35);
                g.fillRect(220,490 ,35,35);
                g.fillRect(255,490 ,35,35);
                g.fillRect(290,490 ,35,35);
                g.fillRect(320,490 ,35,35);
                g.fillRect(355,490 ,35,35);
                g.fillRect(390,490 ,35,35);
                g.fillRect(420,490 ,35,35);
                g.fillRect(455,490 ,35,35);
                g.fillRect(490,490 ,35,35);
                //right column
                g.fillRect(490,55 ,35,35);
                g.fillRect(490,90 ,35,35);
                g.fillRect(490,120 ,35,35);
                g.fillRect(490,155 ,35,35);
                g.fillRect(490,190 ,35,35);
                g.fillRect(490,220 ,35,35);
                g.fillRect(490,255 ,35,35);
                g.fillRect(490,290 ,35,35);
                g.fillRect(490,320 ,35,35);
                g.fillRect(490,355 ,35,35);
                g.fillRect(490,390 ,35,35);
                g.fillRect(490,420 ,35,35);
                g.fillRect(490,455 ,35,35);
                g.fillRect(490,490 ,35,35);
                //top right
                g.fillRect(90,90 ,35,35);
                g.fillRect(90,120 ,35,35);
                g.fillRect(90,155 ,35,35);
                g.fillRect(120,90 ,35,35);
                g.fillRect(155,90 ,35,35);
                g.fillRect(90, 420, 35, 35);
                g.fillRect(90, 390, 35, 35);
                g.fillRect(90, 355, 35, 35);
                g.fillRect(120, 420, 35, 35);
                g.fillRect(155, 420, 35, 35);
                g.fillRect(420, 90, 35, 35);
                g.fillRect(390, 90, 35, 35);
                g.fillRect(355, 90, 35, 35);
                g.fillRect(420, 120, 35, 35);
                g.fillRect(420, 155, 35, 35);
                g.fillRect(390, 420, 35, 35);
                g.fillRect(420, 390, 35, 35);
                g.fillRect(420, 355, 35, 35);
                g.fillRect(420, 420, 35, 35);
                g.fillRect(355, 420, 35, 35);
                
               
               
           
            }
        }
    
        public static void main(String[] args) 
        {
            gameBoard test = new gameBoard();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(560,575);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class SquareRx 
    {
        private final int row;
        private final int col;
        private boolean occupied = false;
    
        public SquareRx(int r, int c) 
        {
            row = r;
            col = c;
        }
    
        public void setOccupied(boolean occupied) 
        {
            this.occupied = occupied;
        }
    
        public boolean isOccupied() 
        {
            return occupied;
        }
        
        
    }

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    I ran you program and if you fix the following things you should be ok...

    The first thing is that you pretty much always want to call any type of super first... So you will need to update that in your paintComponent

    The second thing I am not really sure why this affects it but you want to take this out:
    Java Code:
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    Other than that, your program should work...
    Last edited by markw8500; 04-22-2009 at 08:44 PM.
    Who Cares... As Long As It Works...

  5. #5
    Thebigchalupa is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    I'm still getting nothing to come up
    What are you refering to by calling the super first?
    i tried a few different things too >.>


    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    
    public class gameBoard extends JPanel 
    {
        SquareRx[][] squares;
        final int PAD = 20; 	
        Image myImage=Toolkit.getDefaultToolkit().createImage("pacy.jpg");
       
        public gameBoard() 
        {
            int ROWS = 10;
            int COLS = 10;
            squares = new SquareRx[ROWS][COLS];
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j] = new SquareRx(i, j);
                }
            }
        }
    
        public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            
            g.drawImage(myImage,1,1,this);
            
            
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/squares[0].length;
            double yInc = (double)(h - 2*PAD)/squares.length;
            
            g2.setPaint(Color.black);
            for(int i = 0; i <= squares[0].length; i++) 
            {
                double x = PAD + i*xInc;
                //g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            
            for(int i = 0; i <= squares.length; i++) 
            {
                double y = PAD + i*yInc;
                //g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
              }
        }
    
        public static void main(String[] args) 
        {
            gameBoard test = new gameBoard();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(560,575);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class SquareRx 
    {
        private final int row;
        private final int col;
        private boolean occupied = false;
    
        public SquareRx(int r, int c) 
        {
            row = r;
            col = c;
        }
    
        public void setOccupied(boolean occupied) 
        {
            this.occupied = occupied;
        }
    
        public boolean isOccupied() 
        {
            return occupied;
        }
        
        
    }

  6. #6
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Super referes to your super.paintComponent(g);... I was always tought to call super of any class first...

    Everything that you have works for me... So double check your image name spelling and location... You can test it by creating a File object with your image and the ifFile() method...
    Who Cares... As Long As It Works...

Similar Threads

  1. Resizing Images in JPanel??
    By NoNickName in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 11:19 PM
  2. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 03:54 PM
  3. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 11:19 AM
  4. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 09:38 PM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 07:01 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
  •