Archived

This topic is now archived and is closed to further replies.

[java] beginner, need help on something stupid

This topic is 5333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this is a real big newb question, but I am working on a java game, as I am learning the language, before I go looking into all these books, and tutorials that ive got I wanted to ask on here this question. All I want to do right now is know how to add like a bitmap or jpeg image that I could do in photoshop to a form. I am currently reading thinking in java by bruce eckel and I took a class on it that i learned the basics. here is the code I have right now. /* * MyFirstJavaGame.java * * May 6 */ /** * * @author beavis6325 */ public class MyFirstJavaGame extends javax.swing.JFrame { public MyFirstJavaGame() { initComponents(); } private void initComponents() { menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); exitMenuItem = new javax.swing.JMenuItem(); helpMenu = new javax.swing.JMenu(); contentsMenuItem = new javax.swing.JMenuItem(); aboutMenuItem = new javax.swing.JMenuItem(); newGameMenuItem = new javax.swing.JMenuItem(); fileMenu.add(newGameMenuItem); fileMenu.setText("File"); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); newGameMenuItem.setText("New Game"); menuBar.add(fileMenu); helpMenu.setText("Help"); contentsMenuItem.setText("Contents"); helpMenu.add(contentsMenuItem); aboutMenuItem.setText("About"); helpMenu.add(aboutMenuItem); menuBar.add(helpMenu); setJMenuBar(menuBar); pack(); } private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } public static void main(String args[]) { new MyFirstJavaGame().show(); } // Variables declaration private javax.swing.JMenuItem newGameMenuItem; private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenu helpMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem contentsMenuItem; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; // End of variables declaration } the code got kinda messed up copying and pasting, but its still readable. I just want to know how to add a picture to the form just to start out with, so I have something to work with.

Share this post


Link to post
Share on other sites
The easiest way is to draw the image in its own JPanel. Then add the JPanel to the JFrame. Inside the JPanel you need to:

1. use the class java.util.Toolkit to load the image.
2. use java.util.MediaTracker to make sure it is fully loaded before you draw it
3. override the paintComponent() method to do the actual drawing
4. override getPreferredSize, getMinimumSize and getMaximumSize to return the size of the image so the JPanel will be drawn correctly by its parent.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites