Drawing Basics.

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:

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 -.-

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.

one big homer dough

:confused: 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.

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 … :confused:

Here are all formulas for the Positions.

    
    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

    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

        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);
    }

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…


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

XD, thx your ajustment
and

int orbit = (int)(solarSystem[i].getOrbit());

did the trick ^^