Any Java Gurus out there that can help a newbie?
I'm trying to create a program to write a png and jpeg file from an image I create. I'm getting the usual "non-static method cannot be accessed by static context" error. I know the general explanation is that I'm trying to access a method without creating a new instance of it. However, I thought one of my alternate solutions would solve the problem - but they don''t.
Here's the 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. }
Here's what I've tried (and what has failed). It says it cannot find the symbol myCreateImage
changing:
RenderedImage rendImage = myCreateImage();
to
RenderedImage rendImage = new myCreateImage();
or
RenderedImage rendImage = new rendImage.myCreateImage();
or
RenderedImage rendImage = rendImage.myCreateImage();
Any help anyone could give me (especially an explantion why a new instance isn't created) would be appreciated.