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

    Default Images in JPanel

    Hey, I am working on a Pacman game for my Java class
    but i am having some troubles getting the image of pacman
    the name of the file is 'pacy.jpg'
    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
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    10

    Default

    You haven't got any code that even mentions an image. Have you read Lesson: Working with Images?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. need help with JPanel Images
    By Thebigchalupa in forum New To Java
    Replies: 5
    Last Post: 04-23-2009, 08:15 PM
  2. Resizing Images in JPanel??
    By NoNickName in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 11:19 PM
  3. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 03:54 PM
  4. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 11:19 AM
  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
  •