user2162996
user2162996

Reputation:

Made a JFrame, made an applet, not sure how to combine the two?

I'm a complete Java novice and need some help. Basically, I was tying to make a JFrame that had a background for my already made game. I just used Netbeans JFrame design tab and was hoping I could combine the code in a dedicated section of the JFrame code, which I've been unable to do.

Alternatively, it would be just as good if somebody could show me how to add a background to the JFrame created in my game, the code is below:

    package javagame;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class JavaGame extends JFrame {

    int x, y;
        Image piggy1;
    private Image dbImage;
    private Graphics dbg;


    public class ActionListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            if(keyCode == e.VK_LEFT) {
                if(x <= 0)
                    x = 0;
            else
                x += -5;
            }

            if(keyCode == e.VK_RIGHT) {
                if (x>= 450)
                    x = 450;
                else
                    x += +5;
                        }

            if(keyCode == e.VK_UP) {
                if(y<= 20)
                    y = 20;
            else
                y += -5;
            }
            if(keyCode == e.VK_DOWN) {
                if (y>= 450)
                    y = 450;
            else
                y += +5;
            }

        }
        public void keyReleased(KeyEvent e) {

        }
    }

    public JavaGame() {

                //Images
                ImageIcon i = new ImageIcon("C:/Users/MrBlueMKII/Documents/NetBeansProjects/JavaGame/src/javagame/PIGGY.gif");
                piggy1 = i.getImage();

                addKeyListener(new ActionListener());
        setTitle("Java Game");
        setSize(500, 500);
                setResizable(false);
                setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 150;
        y = 150;

                ImageIcon j = new ImageIcon("C:/Users/MrBlueMKII/Downloads/field.jpg");



    }

    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }


    public void paintComponent(Graphics g) {
        g.drawImage(piggy1, x, y, this);

        repaint();


    }


    public static void main(String[] args)  {

        new JavaGame();


    } 
}

The code for the JFrame is:

package javagame;


public class JFrame extends javax.swing.JFrame {

public JFrame() {
    initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setLayout(null);

    jTextField1.setBackground(new java.awt.Color(223, 248, 248));
    jTextField1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jTextField1.setForeground(new java.awt.Color(255, 255, 255));
    jTextField1.setText("Eat the flies to evolve wings!");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField1ActionPerformed(evt);
        }
    });
    jPanel1.add(jTextField1);
    jTextField1.setBounds(280, 0, 200, 80);

    jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\MrBlueMKII\\Downloads\\field.jpg")); // NOI18N
    jLabel1.setText("jLabel1");
    jPanel1.add(jLabel1);
    jLabel1.setBounds(0, 0, 500, 500);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}



public static void main(String args[]) {

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

Thanks!

Upvotes: 0

Views: 967

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

The best approach would be to remove all the common elements that you need to into separate components (using something like JPanel instead of JFrame and JApplet)

This will allow you to add the to the frame or applet as you see fit.

This is very common design philosophy, don't trap your self to a particular top level container by implementing the logic in a common in a common ancestor

Upvotes: 1

Related Questions