package maladorintro;
import java.awt.*;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.applet.AudioClip;
public class intro extends JApplet {
int appWidth=450;
int appHeight=600;
Image logo;
AudioClip sound;
Font myFont=new Font("Serif",Font.BOLD,30); //Create new font for text
public void init() {
//Load images and sound
logo=getImage(getCodeBase(),"malador.jpg");
sound=getAudioClip(getCodeBase(),"wind.WAV");
}
public void intro(Graphics g) {
//Play sound
sound.play();
//Set background
g.fillRect(0,0,appWidth,appHeight);
//Set the text to red and change the font
g.setColor(Color.RED);
g.setFont(myFont);
//Draw the text
g.drawString("Malador",(appWidth/2)-45,(appHeight/2));
//Pause
try {
Thread.sleep(2000);
}
catch(Exception e) {
System.out.println(e);
}
//Display the image
g.drawImage(logo,0,0,appWidth,appHeight,this);
}
public void paint(Graphics g) {
intro(g);
}
}
I only want the code to run once. However I need a graphics object thats initialized and thats the only reason I have a paint function. Is there a better way?
Java question
I''m trying to use images and sound, and I know what''s wrong. Everytime I change the screen paint gets called again and so it ends up doing this three times. My question is, how can I get it so in an applet, it only runs code once. Here''s what I got right now"
Simplest solution - create a boolean member and initialize it to false. In intro, set it to true. In paint, only call intro if it is false.
I am not 100% sure what you mean here, so maybe I misunderstand you but it sounds like you only want that intro(g) is called once, right?
Then you just add one more variable, boolean for example and set it to either true or false indicating if intro is run or not....
declaration:
boolean bRunIntro;
Inside init:
bRunIntro = true;
In paint
if (bRunIntro) {
bRunIntro = false;
intro(g);
}
That will prevent the applet from redrawing itself.
A better way would be to have an image object that holds the final graphics (red background, Malador text and logo). Like this:
This way, your applet is kind of double buffered and multiple calls to paint() shouldn''t bother you anymore.
- Christoph
---
Teamwork Software - Stuff That Does Something
A better way would be to have an image object that holds the final graphics (red background, Malador text and logo). Like this:
package maladorintro;import java.awt.*;import java.awt.image.*;import javax.swing.JApplet;import javax.swing.JOptionPane;import java.applet.AudioClip;public class intro extends JApplet { int appWidth=450; int appHeight=600; Image logo; Image applet = null; AudioClip sound; Font myFont=new Font("Serif",Font.BOLD,30); //Create new font for text public void init() { //Load images and sound logo=getImage(getCodeBase(),"malador.jpg"); sound=getAudioClip(getCodeBase(),"wind.WAV"); } public void intro(Graphics g) { if (applet == null) { // Create new image applet = new BufferedImage(appWidth, appHeight, BufferedImage.TYPE_INT_RGB); Graphics graph = applet.createGraphics(); //Play sound sound.play(); //Set background graph.fillRect(0,0,appWidth,appHeight); //Set the text to red and change the font graph.setColor(Color.RED); graph.setFont(myFont); //Draw the text graph.drawString("Malador",(appWidth/2)-45,(appHeight/2)); // Draw image g.drawImage(applet, 0, 0, appWidth, appHeight, this); //Pause try { Thread.sleep(2000); } catch(Exception e) { System.out.println(e); } //Display the image graph.drawImage(logo,0,0,appWidth,appHeight,this); g.drawImage(applet, 0, 0, appWidth, appHeight, this); } else { g.drawImage(applet, 0, 0, appWidth, appHeight, this); } } public void paint(Graphics g) { intro(g); }}
This way, your applet is kind of double buffered and multiple calls to paint() shouldn''t bother you anymore.
- Christoph
---
Teamwork Software - Stuff That Does Something
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement