1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Java-Transformations-Translate-BluJay

Discussion in 'Java' started by danbendlin, Oct 14, 2011.

  1. danbendlin

    danbendlin New Member

    Joined:
    Oct 14, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I have recreated a similar game to space invaders and currently need to get my green ship to multiply a few times (3-5) and make them jump around similar to the game. Below is my code with all of the completions except I cannot seem to multiply my ships. Can anyone fill in the blanks? much appreciated.
    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 TexturePaint wallTexture;
        private BufferedImage wallImage;
        private TexturePaint shipTexture;
        
        public SpaceInvaders()
        {
            // add statements to create the general paths
            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();
            
            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();
           
            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");
            }
            
            // add statements to read in images and create textures
            
            timer = new Timer(1000,this); //second
            timer.setInitialDelay(0);
            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(wallTexture);
            trans.setToTranslation(50,280);
            g2d.transform(trans);
            g2d.fill(wall);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(wall);
            trans.setToTranslation(200,0);
            g2d.transform(trans);
            g2d.fill(wall);
            
            
            g2d.setPaint(Color.green);
            trans.setToTranslation(1,-250);
            g2d.transform(trans);
            g2d.fill(ship);
            trans.setToTranslation(-100,-250);
            g2d.transform(trans);
            g2d.fill(ship);
            trans.setToTranslation(-100,-250);
            g2d.transform(trans);
            g2d.fill(ship);
        }
        
        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);
        }
    }
    
     

Share This Page