Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer will vary slightly depending on whether the application or applet is using <a href="http://java.sun.com/javase/6/docs/technotes/guides/awt/" rel="noreferrer">AWT</a> or <a href="http://java.sun.com/docs/books/tutorial/uiswing/index.html" rel="noreferrer">Swing</a>.</p> <p>(Basically, classes that start with <code>J</code> such as <code>JApplet</code> and <code>JFrame</code> are Swing, and <code>Applet</code> and <code>Frame</code> are AWT.)</p> <p>In either case, the basic steps would be:</p> <ol> <li>Draw or load an image into a <code>Image</code> object.</li> <li>Draw the background image in the painting event of the <code>Component</code> you want to draw the background in.</li> </ol> <p><strong>Step 1.</strong> Loading the image can be either by using the <a href="http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html" rel="noreferrer"><code>Toolkit</code></a> class or by the <a href="http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html" rel="noreferrer"><code>ImageIO</code></a> class.</p> <p>The <a href="http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(java.lang.String)" rel="noreferrer"><code>Toolkit.createImage</code></a> method can be used to load an <code>Image</code> from a location specified in a <code>String</code>:</p> <pre><code>Image img = Toolkit.getDefaultToolkit().createImage("background.jpg"); </code></pre> <p>Similarly, <code>ImageIO</code> can be used:</p> <pre><code>Image img = ImageIO.read(new File("background.jpg"); </code></pre> <p><strong>Step 2.</strong> The painting method for the <code>Component</code> that should get the background will need to be overridden and paint the <code>Image</code> onto the component.</p> <p>For AWT, the method to override is the <a href="http://java.sun.com/javase/6/docs/api/java/awt/Component.html#paint(java.awt.Graphics)" rel="noreferrer"><code>paint</code></a> method, and use the <a href="http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20java.awt.image.ImageObserver)" rel="noreferrer"><code>drawImage</code></a> method of the <a href="http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html" rel="noreferrer"><code>Graphics</code></a> object that is handed into the <code>paint</code> method:</p> <pre><code>public void paint(Graphics g) { // Draw the previously loaded image to Component. g.drawImage(img, 0, 0, null); // Draw sprites, and other things. // .... } </code></pre> <p>For Swing, the method to override is the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)" rel="noreferrer"><code>paintComponent</code></a> method of the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html" rel="noreferrer"><code>JComponent</code></a>, and draw the <code>Image</code> as with what was done in AWT.</p> <pre><code>public void paintComponent(Graphics g) {     // Draw the previously loaded image to Component.     g.drawImage(img, 0, 0, null);     // Draw sprites, and other things.     // .... } </code></pre> <p><strong>Simple Component Example</strong></p> <p>Here's a <code>Panel</code> which loads an image file when instantiated, and draws that image on itself:</p> <pre><code>class BackgroundPanel extends Panel { // The Image to store the background image in. Image img; public BackgroundPanel() { // Loads the background image and stores in img object. img = Toolkit.getDefaultToolkit().createImage("background.jpg"); } public void paint(Graphics g) { // Draws the img to the BackgroundPanel. g.drawImage(img, 0, 0, null); } } </code></pre> <p>For more information on painting:</p> <ul> <li><a href="http://java.sun.com/products/jfc/tsc/articles/painting/" rel="noreferrer">Painting in AWT and Swing</a></li> <li><a href="http://java.sun.com/docs/books/tutorial/uiswing/painting/" rel="noreferrer">Lesson: Performing Custom Painting</a> from <a href="http://java.sun.com/docs/books/tutorial/index.html" rel="noreferrer">The Java Tutorials</a> may be of help.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload