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