0

I need to make a program that will open an image and later be able to draw over it. However I got stuck because it returns my graphics as null. Here is the chunk of code where the problem should be. If anything I can add the entire thing.

 public void paintComponent(Graphics gd) {
  // super.paintComponent(g);

super.paintComponent(gd);
//gd.drawString("This is my custom Panel!",10,20);
pencil(gd);



    }


  public void pencil(Graphics gd){
     Graphics2D g2 = (Graphics2D)gd;
  System.out.println("g2 " + g2 + "method was called");

 //g2.setColor(Color.yellow);
 //Line2D line = new Line2D();
   g2.drawLine(x1,y1,x2,y2);  
//System.out.println("pencil");


 }



  public void actionPerformed(ActionEvent e) {
  if (e.getSource() == open){
  int returnVal = fc.showOpenDialog(Lab3.this);

  if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fc.getSelectedFile();
          try {
                    img=ImageIO.read(file);
                    ImageIcon icon=new ImageIcon(img); // ADDED
                    image.setIcon(icon); // ADDED

                    Dimension imageSize = new           Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED
                    image.setPreferredSize(imageSize); // ADDED
                    image.add(hbar, BorderLayout.SOUTH);
                    image.add(vbar, BorderLayout.EAST);
                    image.revalidate(); // ADDED
                    image.repaint(); // ADDED
                    Graphics gd = image.getGraphics();
                }
                catch(IOException e1) {}
            }
}
 if (e.getSource() == pencil){
 try{

     System.out.println("g2" + g2);
     //pencil();
    pencil(g2);
    //g2.draw(new Ellipse2D.Double(0, 100, 30, 30));
     //g2.drawRectanlge(100,100,200,200); 
     }
 catch ( Exception err1 ) {
       System.out.println( err1.getMessage( ) );
   }
 } 
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Oct 30 '13 at 05:37
  • Sounds like you are trying to do GFX before anything is rendered - can't tell from the code posted so far though. – John3136 Oct 30 '13 at 05:38
  • When dealing with `Graphics` to the screen or printer, you shouldn't be supplying your own `Graphics` context, but should be letting the paint sub system take care of it, so calling `pencil(g2);` from the `actionListener` is not appropriate – MadProgrammer Oct 30 '13 at 05:50
  • This [example](http://stackoverflow.com/a/2545642/230513) draws on the glass pane. – trashgod Oct 30 '13 at 09:50
  • is this a duplicate? http://stackoverflow.com/questions/19682176/cant-initialize-java-graphics-component – MadConan Oct 30 '13 at 12:40

1 Answers1

0

I'm guessing you can't get Graphics to work

so what you need to do is get your canvas/panel/frame from swing

Canvas canvas = new Canvas();
JFrame frame = new JFrame();

frame.add(canvas);
Graphics g = canvas.getGraphics();