Results 1 to 7 of 7
  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default problems with java2D

    hey I am having a mountain of problems with Graphics2D. I seem to be able to use it to draw lines, but whenever i attempt to render images, it all compiles fine but the Frame is just blank. Fram is my window adapter.
    any help would be appreciated thx.

    here is code:
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.imageio.*;
    import java.io.*;
    public class TRYGRAPHICS2D extends Fram
    {
        GPanel base = new GPanel();
        BufferedImage img;
        int xoff=50, yoff=50;
        public TRYGRAPHICS2D()
        {
            super("TRYGRAPHICS2D");
            add(base);
            setSize(500,500);
            setVisible(true);
        }
        public static void main(String[] args)
        {
            TRYGRAPHICS2D a = new TRYGRAPHICS2D();
        }
    }
    
    public class GPanel extends Panel
    {
        BufferedImage img;
        int xoff=50, yoff=50;
        GPanel()
        {
            setBackground(Color.BLUE);
        }
        public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
            try
            {
                BufferedImage img = ImageIO.read(getClass().getClassLoader().getResource("pic/starfish.jpg"));
            }
            catch(IOException e) {System.out.println("error");}
            //g.drawLine(50,50,100,100);
            //g2.draw(new Line2D.Double(50, 50, 100, 100));
            g2.drawImage(img,50,50,null);
            //g2.translate(50,50);
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: problems with java2D

    I suggest that you move the reading of the image file out of the paint() method.
    What is the value of img after the read?


    The code works for me (after I changed the path to an image on my PC)
    Last edited by Norm; 05-30-2012 at 12:04 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: problems with java2D

    i moved the try catch statement into the GPanel constructor, it still didn't work, but it does make sense that moving it out of the paint method would help. i tried changling the ImageIO.read() to read a File object with the whole URL of the same image, it still didn't work... could it be the JDK? I think I am using version 6, would that matter?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: problems with java2D

    The JDK should not matter. The code works for me. Is the message: 'error' printed? You should call the printStackTrace() method in the catch block to get a better error message.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: problems with java2D

    it doesn't print error, it simply doesn't draw the image.

    here is code:
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.imageio.*;
    import java.io.*;
    public class TRYGRAPHICS2D extends Fram
    {
        GPanel base = new GPanel();
        BufferedImage img;
        int xoff=50, yoff=50;
        public TRYGRAPHICS2D()
        {
            super("TRYGRAPHICS2D");
            add(base);
            setSize(500,500);
            setVisible(true);
        }
        public static void main(String[] args)
        {
            TRYGRAPHICS2D a = new TRYGRAPHICS2D();
        }
    }
    
    public class GPanel extends Panel
    {
        File file = new File("J:\\programmin\\WhiteBook\\CH11\\pic\\starfish.jpg");
        BufferedImage img;
        int xoff=50, yoff=50;
        GPanel()
        {
            setBackground(Color.BLUE);
            try
            {
                BufferedImage img = ImageIO.read(getClass().getClassLoader().getResource("pic/starfish.jpg"));
            }
            catch(IOException e) {System.out.println("error");}
        }
        public void paint(Graphics g)
        {
            //System.out.println(getClass().getClassLoader().getResource("pic/starfish.jpg"));
            Graphics2D g2 = (Graphics2D)g;
            //g.drawLine(50,50,100,100);
            //g2.draw(new Line2D.Double(50, 50, 100, 100));
            g2.drawImage(img,null,50,50);
            //g2.translate(50,50);
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: problems with java2D

    Your code defines two variables: img
    One at the class level, one in a method.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: problems with java2D

    that was it ty sooooooooooo much for the help!!!

Similar Threads

  1. the very very very basics of java2d
    By senca in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 03:30 PM
  2. Traffic Organizing In JAVA2D Problems
    By tamer in forum Java 2D
    Replies: 5
    Last Post: 04-25-2010, 11:41 PM
  3. Java2D Co-Ordinate Rounding
    By Gemma in forum Java 2D
    Replies: 0
    Last Post: 04-17-2010, 11:52 AM
  4. Java2D
    By Alex j in forum Java 2D
    Replies: 1
    Last Post: 04-07-2009, 04:59 PM
  5. n00b help with java2D api
    By michcio in forum Java 2D
    Replies: 7
    Last Post: 10-30-2008, 06:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •