4 Replies - 1567 Views - Last Post: 12 May 2010 - 10:33 AM Rate Topic: -----

#1 santwana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-April 10

I can't draw the image on Jpanel.. pls help

Posted 12 May 2010 - 06:46 AM

I have already loaded the Image i need from the same directory..
img=Toolkit.getDefaultToolkit().getImage("bb.png");



I am trying to draw the image on my JPanel but can't...

public void paintComponent(Graphics g)
{
   super.paintComponent(g);
    int w=getWidth();
        int h=getHeight();
    int divx=w/8;
        int divy=h/8;
        

    g.setColor(Color.RED);
    for(int i=0;i<8;i=i+2)
        for(int j=0;j<8;j=j+2)
        {
            g.fillRect(j*divx,i*divy,divx,divy);
            g.fillRect((1+j)*divx,(1+i)*divy,divx,divy);
        }
    g.drawImage(img,10,10,50,50,this);
    
    

}



Please help me..

Is This A Good Question/Topic? 0
  • +

Replies To: I can't draw the image on Jpanel.. pls help

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,162
  • Joined: 11-December 07

Re: I can't draw the image on Jpanel.. pls help

Posted 12 May 2010 - 06:58 AM

I can't see anything obvious that would prevent it from working. What do you see? Are your red boxes appearing?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10572
  • View blog
  • Posts: 39,143
  • Joined: 27-December 08

Re: I can't draw the image on Jpanel.. pls help

Posted 12 May 2010 - 07:05 AM

Can you post all your code? This isn't enough for us to help you.
Was This Post Helpful? 0
  • +
  • -

#4 santwana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-April 10

Re: I can't draw the image on Jpanel.. pls help

Posted 12 May 2010 - 07:54 AM

Yes.. the red boxes are appearing but there is no image..
When I traced out, I have seen that the g.drawImage method returns false.I don't understand why.

here is the full code
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.imageio.*;
//import java.io.*;
public class Checker extends javax.swing.JPanel {
    private int Xpos,Ypos;
     String position;
     Image img;
     
    /** Creates new form Checker */
    public Checker() {

        
        
        initComponents();
 img=Toolkit.getDefaultToolkit().getImage("bb.png");
        setPreferredSize(new Dimension(400,400));
        setMinimumSize(getPreferredSize());
        
        int w=getWidth();
        int h=getHeight();
        final int stepx=50;
        final int stepy=50;
        addMouseListener(
                new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {


           int x=me.getX();
           int y=me.getY();
           if(x>0&&x<stepx)
               Xpos=0;
           else if(x>stepx&&x<(2*stepx))
               Xpos=1;
           else if(x>(2*stepx)&&x<(3*stepx))
               Xpos=2;
          else if(x>(3*stepx)&&x<(4*stepx))
               Xpos=3;
          else if(x>(4*stepx)&&x<(5*stepx))
               Xpos=4;
           else if(x>(5*stepx)&&x<(6*stepx))
               Xpos=5;
           else if(x>(6*stepx)&&x<(7*stepx))
               Xpos=6;
           else if(x>(7*stepx)&&x<(8*stepx))
                Xpos=7;
           else Xpos=10000;
           if(y>0&&y<=stepy)
               Ypos=0;
           else if(y>stepy&&y<(2*stepy))
               Ypos=1;
           else if(y>(2*stepy)&&y<(3*stepy))
               Ypos=2;
           else if(y>(3*stepy)&&y<(4*stepy))
               Ypos=3;
           else if(y>(4*stepy)&&y<(5*stepy))
               Ypos=4;
           else if(y>(5*stepy)&&y<(6*stepy))
               Ypos=5;
           else if(y>(6*stepy)&&y<(7*stepy))
               Ypos=6;
           else if(y>(7*stepy)&&y<(8*stepy))
                Ypos=7;
           else Ypos=10000;
           position="You are in square ["+Xpos+","+Ypos+"]"+"with coordinates"+x+","+y;
           JOptionPane.showMessageDialog(null,position);



        }
        } );



    }
    
 @Override
public void paintComponent(Graphics g)
{
     
   super.paintComponent(g);
   
    int w=getWidth();
        int h=getHeight();
    int divx=w/8;
        int divy=h/8;
        

    g.setColor(Color.RED);
    for(int i=0;i<8;i=i+2)
        for(int j=0;j<8;j=j+2)
        {
            g.fillRect(j*divx,i*divy,divx,divy);
            g.fillRect((1+j)*divx,(1+i)*divy,divx,divy);
        }
    g.drawImage(img,10,10,this);
     
    

}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    // End of variables declaration                   

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author swan
 */
import java.awt.*;
import javax.swing.*;
public class TestChecker {
public static void main(String args[])
{
    JFrame chess=new JFrame("The Checker board for chess game");
    
    Checker c=new Checker();
    
    
    
    c.setBackground(Color.WHITE);
   
    chess.add(c);
    chess.pack();
    chess.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
    chess.setVisible(true);
}
}


Was This Post Helpful? 0
  • +
  • -

#5 santwana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-April 10

Re: I can't draw the image on Jpanel.. pls help

Posted 12 May 2010 - 10:33 AM

I tried executing the same in cmd prompt instead of my NetBeans 6.8 IDE, It works just fine.. Thank you so much for helping me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1