Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help making an image move from one side of screen to the other.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help making an image move from one side of screen to the other.

    Hi, I am doing a college project and have been told I have to create a splash screen with an image that moves across the screen I have tried and tried but always end up with errors or failing.....SpaceShip.png is the image I need animating....

    can someone here help me?? My code is made in BlueJ
    Attached below is a copy
    I had to make the buttons separate classes as the project requirements said so....

    Here is my code:

    My main Code:

    /**
     * Libraries
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
    import java.awt.Color.*;
    import java.awt.Insets;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.FlowLayout;
     
     
     
    /**
     * 
     * @author (Stefan xxxxx) 
     * @version (v1.2)
     */
    public class SpaceDetectives extends JFrame
    {
       private JPanel panel;
       private JPanel SpaceFrame;
     
     
     
       public static void main(String args[])
        {
             JFrame SpaceFrame = new SpaceDetectives();
             SpaceFrame.setSize(650, 470);
             SpaceFrame.setBackground(Color.black);
             SpaceFrame.setUndecorated(true);
             SpaceFrame.setVisible(true);
     
             Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
             int w = SpaceFrame.getSize().width; 
             int h = SpaceFrame.getSize().height; 
             int x = (dim.width-w)/2; 
             int y = (dim.height-h)/2;   
             SpaceFrame.setLocationRelativeTo(null);
     
     
     
     
        }
     
     
        public SpaceDetectives()
        {
          setTitle("Space Detectives");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setLayout(new BorderLayout());
          JPanel panel = new Background();
          JPanel slogancon = new JPanel();
          JLabel label = new JLabel("Testing");
     
          JPanel pane1 = new BtnStart();
          JPanel pane2 = new BtnExit();
     
          panel.setOpaque(false);
          pane1.setOpaque(false);
     
     
     
     
          GridBagConstraints gc = new GridBagConstraints();
     
     
     
     
            gc.insets = new Insets(0, 0, 0, 0);
            gc.gridx = 1;
            gc.gridy = 0;
     
          panel.add(pane1, gc);
     
     
          panel.add(pane2, gc);
     
     
     
          pane2.setOpaque(false);
     
          getContentPane().add(panel);
     
          panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 390));
     
     
     
        }
     
     
     
     
       public class Background extends JPanel 
       {
           Image bgimage = null;
           Image test = null;
     
     
     
           public Background()
           {
     
     
     
               MediaTracker mt = new MediaTracker(this);
        bgimage = Toolkit.getDefaultToolkit().getImage("b2.png");
        mt.addImage(bgimage, 0);
        test = Toolkit.getDefaultToolkit().getImage("SpaceShip.png");
        mt.addImage(test, 0);
     
            }
     
     
            public void paintComponent(Graphics g) 
            { 
                super.paintComponent(g); 
                int xPos = 10;
     
     
     
     
                if(bgimage != null) 
                { 
                    int imwidth = bgimage.getWidth(null); 
                    int imheight = bgimage.getHeight(null); 
     
                    if((imwidth > 0) && (imheight > 0)) 
                    {                 
                        for(int y = 0; y<getHeight(); y+=imheight) 
                        { 
                            for(int x = 0; x<getWidth(); x+=imwidth) 
                            {                     
                                g.drawImage(bgimage, x, y, null); 
                                g.drawImage(test,xPos,50,null);
                                g.setFont(new Font("Helvetica", Font.BOLD,  13));
                                g.setColor(Color.black);
                                g.drawString("The mysteries of the universe are just around the corner", 175, 274);
     
     
     
        }
                            } 
     
     
     
     
     
        }
    }
    }
     
    }
     
     
    }

    My Buttons:

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class BtnStart extends JPanel implements ActionListener
    {
    private JButton start;
     
     
    public BtnStart()
    {   
                 ImageIcon start = new ImageIcon("Start.png");
                 JButton starrt = new JButton(start);
                 starrt.setPreferredSize(new Dimension(95, 40));
                 starrt.setOpaque(false);
    starrt.setContentAreaFilled(false);
    starrt.setBorderPainted(false);
                 Icon rolloverIcon = new ImageIcon("StartRoll.png");
                 starrt.setRolloverIcon(rolloverIcon);
                 starrt.addActionListener(this);
                 add(starrt);
     
     
                }
     
                public void actionPerformed(ActionEvent event) {
            JFrame frame = new JFrame();
            JButton button = new JButton("Dummy game screen");
     
            frame.setSize(600,600);
            frame.getContentPane().add(button);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
        }
            }

     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    /**
     * Write a description of class BtnExit here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class BtnExit extends JPanel implements ActionListener
    {
    	// instance variables - replace the example below with your own
    	private JButton exit;
     
    	/**
    	 * Constructor for objects of class BtnExit
    	 */
    	public BtnExit()
    	{
    	         ImageIcon Exit = new ImageIcon("Exit.png");
    		    JButton exitt = new JButton(Exit);
                 exitt.setPreferredSize(new Dimension(80, 40));
                 exitt.setOpaque(false);
                 exitt.setContentAreaFilled(false);
                 exitt.setBorderPainted(false);
                 Icon rolloverIcon = new ImageIcon("ExitRoll.png");
                 exitt.setRolloverIcon(rolloverIcon);
                  exitt.addActionListener(this);
                 add(exitt);
    	}
     
    	public void actionPerformed(ActionEvent event) {
            System.exit(1); 
        }
     
    }
    Attached Files Attached Files
    Last edited by Xillius200; November 25th, 2011 at 12:10 PM. Reason: Edited out my full name


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help making an image move from one side of screen to the other.

    You say you have errors. What are they? What do you mean when you say that it fails? If you want help, you'll have to provide an SSCCE that demonstrates the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  2. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  3. Replies: 0
    Last Post: August 6th, 2011, 01:07 AM
  4. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 07:49 AM
  5. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 11:25 AM

Tags for this Thread