Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Drawing Basics.  (Read 1941 times)
0 Members and 1 Guest are viewing this topic.
Offline gauo

Junior Newbie





« Posted 2008-09-26 20:04:31 »

Hi, i have a problem with the Basics of Game Programming.
I have programmed the logic of my game, and was now about to
try to implement the graphical representation.

Since its a strategy game, i there is not much of animations,
only some kind of Solarsystem(with different planets)
within wich an little ship icon can be moved and etc.

I have collected Gifs (about 90x90 Planets, suns 100x100, and background
800x600)
and wanted to draw them on a JPanel. And maybe, after i would
have achieved this, make some kind of orbits, animations(rotation).
But i cant even draw one image , without an overflow.
here my Code:
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
public class SolarSystemView extends JPanel {

    int width = 800, height = 600;
    private SolarSystem system;
    private BufferedImage planetD;
    private BufferedImage planetH;
    private BufferedImage planetJ;
    private BufferedImage planetK;
    private BufferedImage planetL;
    private BufferedImage planetM;
    private BufferedImage planetN;
    private BufferedImage planetT;
    private BufferedImage planetY;
    private BufferedImage sunRedDwarf;
    private BufferedImage sunYellowDwarf;
    private BufferedImage sunRedGiant;
    private BufferedImage background;
    private BufferedImage offImage;
    private Graphics dbg;

    public SolarSystemView() {
        try {
            this.setPreferredSize(new Dimension(800,600));
            background = ImageIO.read(SolarSystem.class.getResource("/Icons/Planets/background.gif"));
            sunRedGiant = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/RedGiant.gif"));
        } catch (IOException e) {
            System.err.println("IOException thrown 45");
        }
    }

    public SolarSystemView(SolarSystem system) {
        try {
            planetD = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/D.gif"));
            planetH = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/H.gif"));
            planetJ = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/J.gif"));
            planetK = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/K.gif"));
            planetL = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/L.gif"));
            planetM = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/M.gif"));
            planetN = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/N.gif"));
            planetT = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/T.gif"));
            planetY = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/Y.gif"));

            sunRedDwarf = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/RedDwarf.gif"));
            sunYellowDwarf = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/YellowDwarf.gif"));
            sunRedGiant = ImageIO.read(SolarSystemView.class.getResource("/Icons/Planets/RedGiant.gif"));

            background = ImageIO.read(SolarSystem.class.getResource("/Icons/Planets/background.gif"));

            GraphicsConfiguration gfxConf = GraphicsEnvironment.getLocalGraphicsEnvironment().
                    getDefaultScreenDevice().getDefaultConfiguration();

            offImage = gfxConf.createCompatibleImage(width, height);

        } catch (IOException e) {
            System.err.println("IOException thrown #line");
        }
        this.system = system;
    }

    public void paint(Graphics g) {
       
        if (offImage == null) {
            int width = 800, height = 600;
           
            offImage = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
            dbg = offImage.getGraphics();
        }

       
        dbg.setColor(Color.BLACK);
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
       
        dbg.drawImage(sunRedGiant, width / 2 - 100, height / 2 - 100, this);

       

        paint(dbg);

       
        g.drawImage(offImage, 0, 0, this);
    }
}



I used the empty constructor.
The planets are not used yet...lateron i would paint a specified sun, up to 10 planets...-.-
plz help XD


edit: ah, and excuse my poor english -.-


Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #1 - Posted 2008-09-26 21:20:13 »

The problem is on the "paint(dbg)" line, this calls recursively the paint method, without a condition to exit the recursion.

The idea of double buffering is:
1.- get the graphics of the buffer
2.- paint/draw/write all the objects into the buffer's graphics
3.- draw the buffer image to the graphics parameter of paint().

There is no need to call again paint() inside paint().

Simply comment or delete the line "paint(dbg)" and it should run.
Offline gauo

Junior Newbie





« Reply #2 - Posted 2008-09-26 22:12:43 »

one big homer *dough*

:/ i really cant imagine why i put that call there...sry to have bothered you ^^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2008-09-27 00:17:21 »

one big homer *dough*

:/ i really cant imagine why i put that call there...sry to have bothered you ^^
It's a Homer D'Oh!

A Homer Dough is like a hotdog bun before being cooked or something.

See my work:
OTC Software
Offline gauo

Junior Newbie





« Reply #4 - Posted 2008-09-27 15:29:50 »

thx, i just know how it sounded and took a shot in the dark ^^.
..back to Topic...
i have by now implemented my SolarSystem, but the calculation, position on the circular Orbit(a circle, no ellipse ^^)
is quite off ... :/

Here are all formulas for the Positions.
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
    
    public PlanetWrapper(Planet planet,int radius){
        this.planet=planet;
        this.orbit=radius;
        degreesTraRot=Rand.getNextInt(360);
        move();
        try{
        image=ImageIO.read(PlanetWrapper.class.getResource("/Icons/Planets/"+planet.getPlanetClass().getTypeName()+".gif"));
        }catch(IOException e){
            System.err.println("Failed to load planet gif in PlanetWrapper");
        }
    }
    public void rotate(double degrees){
        this.degreesRotation+=degrees;
        this.degreesRotation%=360;
        move();
    }
    public void move(){
     degreesTraRot++;
     degreesTraRot%=360;
    x= orbit*Math.cos(degreesTraRot*Math.PI/180);
    y= Math.sin(degreesTraRot*Math.PI/180)*orbit;


    }

public BufferedImage getImageR(){
        return rotateImage(image,this.degreesRotation);
    }
    private static BufferedImage rotateImage(BufferedImage src, double degrees) {
        AffineTransform affineTransform = AffineTransform.getRotateInstance(
                Math.toRadians(degrees),
                src.getWidth() / 2,
                src.getHeight() / 2);
        BufferedImage rotatedImage = new BufferedImage(src.getWidth(), src
                .getHeight(), src.getType());
        Graphics2D g = (Graphics2D) rotatedImage.getGraphics();
        g.setTransform(affineTransform);
        g.drawImage(src, 0, 0, null);
        return rotatedImage;
    }
   



Here is my paint method, solarSystem, contains PlanetWrappers
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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
    public synchronized void paint(Graphics g) {

        if (offImage == null) {

            offImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        }

        dbg = offImage.getGraphics();
        dbg.drawImage(background, 0, 0, this);
        dbg.translate(width/2, height/2);
       
        dbg.setColor(Color.WHITE);
        //dbg.drawImage(this.solarSystem[0], 0, height/2-solarSystem[1].getHeight()/2, this);
       dbg.drawImage(sun, 0-sun.getWidth()/2,0-sun.getHeight()/2, this);
        for (int i = 1; i < solarSystem.length; i++) {
            int orbit = (int)(solarSystem[i].getOrbit()+solarSystem[i].getImage().getWidth()/2);
            dbg.drawOval((0-orbit), (0-orbit),2*orbit,2*orbit);
            dbg.drawImage(solarSystem[i].getImageR(), (int)solarSystem[i].getX(), (int)solarSystem[i].getY(), this);
        }

this is the initialisation and the run method for the animation thread
[code]            for (Planet planet : system.getOrbits()) {
                solarSystem[i] = new PlanetWrapper(planet,((height-sun.getHeight())/2/solarSystem.length*i)+sun.getHeight()/2);
                i++;
            }
    public void run() {
        try {
            while(true){
           
            for (PlanetWrapper planet : solarSystem) {
                planet.rotate(10);
               
            }
           
            repaint();
            Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            m_orbit.interrupt();
        }

    }




        g.drawImage(offImage, 0, 0, this);
    }[/code]

I know i shouldnt call move from rotate, but these are just first drafts.
Now, the problem is that planets dont move on the line drawn by drawOval in the paint method,
Instead some are only a little bit off, some have a elliptic orbit.
Thx for any help and comment and time....


Online Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2008-09-27 17:33:52 »

1  
dbg.drawImage(solarSystem[i].getImageR(), (int)solarSystem[i].getX(), (int)solarSystem[i].getY(), this);


you must subtract half the width and height from the X and Y, to draw the Image centered on X and Y

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gauo

Junior Newbie





« Reply #6 - Posted 2008-09-27 18:07:21 »

XD, thx your ajustment
and
1  
int orbit = (int)(solarSystem[i].getOrbit());

did the trick ^^
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.

Dwinin (5 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!