Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  No picture, what am I missing?  (Read 1527 times)
0 Members and 1 Guest are viewing this topic.
Offline glome

Junior Newbie




Thou art God


« Posted 2008-05-16 06:06:36 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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);
   }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #1 - Posted 2008-05-16 15:37:04 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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.

Offline glome

Junior Newbie




Thou art God


« Reply #2 - Posted 2008-05-17 02:24:29 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #3 - Posted 2008-05-17 03:47:39 »

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.

Offline glome

Junior Newbie




Thou art God


« Reply #4 - Posted 2008-05-19 22:28:45 »

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?
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #5 - Posted 2008-05-20 04:12:22 »

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?

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #6 - Posted 2008-05-20 05:34:16 »

I'll look up what extend means.

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.

See my work:
OTC Software
Offline fletchergames

Senior Member





« Reply #7 - Posted 2008-05-20 18:30:01 »

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.
Yes, you must extend JPanel.  Or some other Swing class, but JPanel is probably the best.

You are overriding the paintComponent method of JPanel.

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?
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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #8 - Posted 2008-05-20 19:06:48 »

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

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!