No picture, what am I missing?

I’ve been googling all day, just trying to figure out how to show an image. My code is distilled from a dozen diferent web pages, only using things I think I might understand. Obviously, I don’t really understand any of it. How do I make this work?


import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class sigil
{
	public static void main(String[]args)
	{
		Frame f = new JFrame("ZV's Sigil");
		Jf.addWindowListener(new WindowAdapter()
			{public void windowClosing(WindowEvent e){System.exit(0);}});
		f.setSize(new Dimension(750,400));
		f.setVisible(true);
		Graphics2D g = (Graphics2D)f.getGraphics();
		drawMyEllipse(g);
	}
	public static void drawMyEllipse(Graphics2D g)
	{
		Ellipse2D myEllipse = new Ellipse2D.Double(10.0, 10.0, 200.0, 100.0);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 			RenderingHints.VALUE_ANTIALIAS_ON);
		g.setStroke(new BasicStroke(5));
		g.setPaint(Color.white);
		g.fill(myEllipse);
		g.setPaint(Color.red);
		g.draw(myEllipse);
	}
}


import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Test extends JPanel {
    public static void main(String args[]) {
        JFrame f = new JFrame("Test");
        f.setContentPane(new Test());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600, 500);
        f.setVisible(true);
    }
    public void paintComponent(Graphics gr) {
        Graphics2D g = (Graphics2D)gr;
        Ellipse2D myEllipse = new Ellipse2D.Double(10.0, 10.0, 200.0, 100.0);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setStroke(new BasicStroke(5));
        g.setPaint(Color.white);
        g.fill(myEllipse);
        g.setPaint(Color.red);
        g.draw(myEllipse);
    }
}

You are looking in the wrong spots. I recommend to all people new to Java, read the ENTIRE tutorial http://java.sun.com/docs/books/tutorial/index.html. The specific section that answers your question is Creating Graphical User Interfaces->Creating a GUI with Swing. You should still do the whole thing.

Ok, I’m reading that section, and I’ve read other parts of that tutorial before, as applicable.
Why does the paintComponent method run? I do not see any place that calls it.
Mine now has the oval too high, like the title bar is covering the top of it, I’m not sure why yet.
Thanks.

Read it from the start to the finish. Some parts build on information presented before it.

paintComponent is called internally. When you start a Java any program the event dispatch thread is started automatically. This is where you get event notifications such has keyboard or mouse events or other things like window events. This is also where all painting methods are called. In each swing component paintComponent is called from paint.

Maybe I’m being too lazy, but it seems much faster to ask questions than to read. Is it necessary for the class to extend JPanel, or is that just a design preference? I’ll look up what extend means.

I’m still a bit confused about the paintComponent method. So it comes up automatically, and I call any methods for drawing things from within it?

Yes you are being lazy. We are not here to answer questions that you don’t want to spend the time and effort to try and learn first. If you won’t expend any effort on yourself, why should we?

[quote=“glome,post:5,topic:31811”]
If you don’t know this, you shouldn’t be messing around with images yet. Stick with console-based stuff until you learn the ins and outs of the programming language you’re using. If you had a lot of programming experience, you’d be able to learn a new language in a day or two, but if you don’t know extends then you’re probably a lot more than just a Java newbie.

Yes, you must extend JPanel. Or some other Swing class, but JPanel is probably the best.

You are overriding the paintComponent method of JPanel.

Look, the AWT Event Thread calls f.paint(). You tell it to start doing this when you call f.setVisible(true).

f.paint() calls the paint(g) method of Test because you put the Test object into the JFrame with f.setContentPane(new Test()).

The paint(g) method of Test calls the paintComponent(g) method of Test. That’s how it gets called.

You have to either read a tutorial. You could buy a book about Swing as well, but the Java Tutorial is a good source for information about Swing. Spending a couple of days reading the tutorial is the only way you’re ever going to understand. Asking random questions isn’t effective.

For actual game programming, I would suggest reading Developing Games in Java and/or Killer Game Programming in Java. However, they don’t really address the basics of Swing too much.

Admittedly that is a much more useful response than my own. :stuck_out_tongue: