1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Newb Java Problem - non-static method cannot be accessed from static context

Discussion in 'Software Development' started by TeddyVon, Sep 5, 2010.

Thread Status:
Not open for further replies.
  1. TeddyVon

    TeddyVon Thread Starter

    Joined:
    Jan 13, 2009
    Messages:
    18
    It's the newbie problem of accessing a non-static method from a static context. However, I can't seem to fix the problem. The program "should" just create a PNG and JPG file of an image I created.

    Here is the code:

    Code:
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.awt.Graphics2D;
    import java.awt.image.RenderedImage;
    import javax.imageio.ImageIO;
    import java.awt.Color;
    
    
    public class WriteText_PicTest {
            
        public RenderedImage myCreateImage()
            {
            int width = 100; int height = 100;
            
            // Create a buffered image in which to draw
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            
            // Create a graphics contents on the buffered image
            Graphics2D g2d = bufferedImage.createGraphics();
            
            // Draw graphics
            
            g2d.setColor(Color.white);
            g2d.fillRect(0, 0, width, height);
            g2d.setColor(Color.black);
            g2d.fillOval(0, 0, width, height);
            
            // Graphics context no longer needed so dispose it
            g2d.dispose();
            return bufferedImage;
        }
    
            public static void main (String args[])
        {
    
            // Create an image to save
    
    RenderedImage rendImage = myCreateImage();
    
    // Write generated image to a file
    
            try {
                // Save as PNG File
                File file = new File("newimage.png");
                ImageIO.write(rendImage, "png", file);
    
                // Save as JPEG
                file = new File("newimage.jpg");
                ImageIO.write(rendImage, "jpg", file);
    
            } catch (IOException e) { }
        }
        
        // Returns a generated image.
    
    
    }
    
    I've tried changing

    Code:
    RenderedImage rendImage = myCreateImage();
    
    to

    Code:
    RenderedImage rendImage = new myCreateImage();
    
    But I get an error that it can't find symbol myCreateImage.

    Same if I try

    Code:
     RenderedImage rendImage = new rendImage.myCreateImage();
     
    or

    Code:
     RenderedImage rendImage = rendImage.myCreateImage();
     
    Not that I'm in the position to make demands, but it would be REALLY helpful if someone could explain why it's failing. I know the general explanation is that I'm trying to access the myCreateImage() from the static main. However, I thought one of the three alternate options I listed above would create an instance and I wouldn't have the problem.

    Many thanks!
     
  2. TeddyVon

    TeddyVon Thread Starter

    Joined:
    Jan 13, 2009
    Messages:
    18
    Just an FYI for anyone who might read this thread to find the answer to their own problem.

    The solution was to make the method static. I could then use the first variable declaration 9see below) without any problem.

    RenderedImage rendImage = myCreateImage();


    Though, it still isn't clear to me why the method has to be static.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/948043

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice