0

I've tried to draw some stuff to JGlassPane. With basic shapes like a rectangle it worked perfectly. But when I try to draw an image, it always shows me the error of Unknown Source. I don't know what it means but I tried everything to fix it: tried relative/absolute path, added the image as source, added it to the build path, and nothing works.

package soft;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class MapObjects 
{
    private int TypObjektu;//1-osoba,2-vozidlo,3-budova,4-custom
    private float []GPSpos;
    private String nazov;
    private String popis;
    private int userId;
    private int priority;
    private BufferedImage ikona;


    public MapObjects(String nazov,String popis,int typ)
    {
        nazov=this.nazov;
        popis=this.popis;
        TypObjektu=typ;
        File file=new File("D:/workspace/sources/iconPerson.jpg");
        try {
            ikona = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Image getImage()
    {
        return ikona;
    }
    public void setAsPerson()
    {
        TypObjektu=1;
    }
    public void setAsCar()
    {
        TypObjektu=2;
    }
    public void setAsBuilding()
    {
        TypObjektu=3;
    }
    public void setAsCustom()
    {
        TypObjektu=4;
    }

}

Error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at soft.MapDrawer.paintComponent(MapDrawer.java:34)
    at soft.MapDrawer.<init>(MapDrawer.java:22)
    at gui.MainWindow.paint(MainWindow.java:189)
    at gui.MainWindow$2.actionPerformed(MainWindow.java:146)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

MapDrawer class

package soft;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

public class MapDrawer extends JPanel
{
     Graphics testGrafika;
     DrawerThread drawingThread;
     MapObjects objekt;

    public MapDrawer(JPanel drawPanel)
    {
        drawPanel.add(this);
        testGrafika=drawPanel.getGraphics();    
        paintComponent(testGrafika);
        objekt=new MapObjects("tada","dada",1);
        drawingThread=new DrawerThread();
        drawingThread.start();

    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        this.setBackground(Color.WHITE);
        g.drawImage(objekt.getImage(), 50, 50, null);
    }

    public Graphics getGraphics()
    {
        return testGrafika;

    }

    public class DrawerThread extends Thread implements Runnable
    {


        @Override
        public void run()
        {
            while(true)
            {
                paintComponent(testGrafika);
                try {
                    DrawerThread.sleep(30);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
durron597
  • 31,968
  • 17
  • 99
  • 158
Ján Srniček
  • 505
  • 1
  • 10
  • 34

2 Answers2

4

Your painting code is completely wrong. You almost never call paintComponent directly, and certainly not in this situation. Your Graphics object is likely null because you're getting it by calling getGraphics() on a component, something you shouldn't be doing since this object won't persist. You will want to read the painting tutorials to see how to do this correctly as much needs to be changed. Recommendations include:

  • Drawing in the paintComponent method
  • But not calling it directly
  • Using a Swing Timer for your timer loop rather than a while code.
  • Using repaint() to suggest to the JVM to repaint the component.

Again, the tutorials, which you can find with Google, will explain all of this and more.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • One Think i dont understant,what shoud i put to the parameter of paintComponent when i shoud not use getGraphics(),how will i obtain that graphics object ? – Ján Srniček Feb 06 '13 at 19:05
  • 1
    [getGraphics() is for creating an snapshot of Graphics that already exist there](http://stackoverflow.com/a/14725042/714968), for creating an Object for printing or saving current image or graphics to the File (for example), this snapshot can expire on first mouse, key event, or when JComponents required for repaint internally (implementations in rellated APIs) – mKorbel Feb 06 '13 at 19:09
  • [best of all is](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659), by @Gagandeep Bali – mKorbel Feb 06 '13 at 19:15
  • @JánSrniček You should never be calling any paint method yourself. These are called on your behalf by the repaint manager. – MadProgrammer Feb 06 '13 at 20:11