CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Help with java program that involves timing & motion

    Hi, Im recreating a Space Invaders like program that takes my green ships and moves them across the screen like in the game. I have the whole code written but the movement is not working and I was hoping someone could point out my missing item.
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import javax.imageio.ImageIO;
    import java.awt.image.*;
    import java.io.IOException;
    
    public class SpaceInvaders extends JPanel implements ActionListener
    {
        private Timer timer;
        private GeneralPath wall;
        private GeneralPath ship;
        private GeneralPath ship2;
        private TexturePaint wallTexture;
        private BufferedImage wallImage;
        private TexturePaint shipTexture;
        
        public SpaceInvaders()
        {
            // add statements to create the general paths
            
            ship = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
            ship.moveTo(1,30);
            ship.lineTo(20,1);
            ship.lineTo(100,1);
            ship.lineTo(120,30);
            ship.lineTo(100,50);
            ship.lineTo(100,40);
            ship.lineTo(70,40);
            ship.lineTo(60,55);
            ship.lineTo(50,40);
            ship.lineTo(20,40);
            ship.lineTo(20,50);
            ship.lineTo(1,30);
            ship.closePath();
            
            ship2 = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
            ship2.moveTo(1,30);
            ship2.lineTo(20,1);
            ship2.lineTo(100,1);
            ship2.lineTo(120,30);
            ship2.lineTo(100,50);
            ship2.lineTo(100,40);
            ship2.lineTo(70,40);
            ship2.lineTo(60,55);
            ship2.lineTo(50,40);
            ship2.lineTo(20,40);
            ship2.lineTo(20,50);
            ship2.lineTo(1,30);
            ship2.closePath();
            
            wall = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
            wall.moveTo(1,1);
            wall.lineTo(100,1);
            wall.lineTo(100,50);
            wall.lineTo(70,50);
            wall.lineTo(70,40);
            wall.lineTo(30,40);
            wall.lineTo(30,50);
            wall.lineTo(1,50);
            wall.lineTo(1,1);
            wall.closePath();
            
            // add statements to read in images and create textures
            try {
                wallImage = ImageIO.read(this.getClass().getResource("CementWall-Program 2.jpg"));
                wallTexture = new TexturePaint(wallImage,
                    new Rectangle(0,0,wallImage.getWidth(),wallImage.getHeight()));
            }
            catch (IOException e) {
                System.out.println("It failed");
            }
            
            timer = new Timer(1000,this); //second
            timer.setInitialDelay(1);
            timer.setCoalesce(true);
            timer.start();
        }
        
        public void actionPerformed(ActionEvent e)
        {
            this.repaint();
        }
        
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            customDrawingMethod(g);
        }
        
        public void customDrawingMethod(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            AffineTransform trans = new AffineTransform();
            
            // replace all of this - draw walls and aliens
            g2d.setPaint(Color.green);
            trans.setToTranslation(25,25);
            g2d.transform(trans);
            g2d.fill(ship);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(ship);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(ship);
            
            g2d.setPaint(Color.green);
            trans.setToTranslation(-300,125);
            g2d.transform(trans);
            g2d.fill(ship);
            g2d.setPaint(Color.green);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(ship);
            
            g2d.setPaint(wallTexture);
            trans.setToTranslation(-275,150);
            g2d.transform(trans);
            g2d.fill(wall);
            g2d.setPaint(wallTexture);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(wall);
            g2d.setPaint(wallTexture);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(wall);
            
        }
        
        public static void main()
        {
            JFrame myFrame = new JFrame();
            myFrame.getContentPane().add(new SpaceInvaders());
            myFrame.setSize(new java.awt.Dimension(600,400));
            myFrame.setTitle("2D Animation Example");
            myFrame.setBackground(Color.black);
            myFrame.setVisible(true);
        }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with java program that involves timing & motion

    I have the whole code written but the movement is not working and I was hoping someone could point out my missing item.
    Don't you mean the program isn't even starting? Please give accurate descriptions of what is happening, it makes helping you so much easier.

    Your main method declaration isn't correct. To be the 'main' method required to start a program it needs to have a parameter of type String[] ie
    Code:
    public static void main(String[] args)
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured