1

I am a beginner in Java GUI, The problem is that I am using a splash screen which has a gradient as a background and I want to write the text , erase it and then write the new one. I Managed to write the text and even I can write the new one but the problem is that the new text overwrites the old text, and the old text is not erased, Since I have a gradient as background so I cannot use .clearRect() or .fillRect() of Graphics2D Class since they fill it with a solid color, here is my code which can help you to guide me.

SplashScreen splash = SplashScreen.getSplashScreen();
int wpro = 0, strx = 491, stry = 414, prox = 491, proy = 389;    
Graphics2D str = splash.createGraphics();
str.setComposite(AlphaComposite.Clear);
str.setPaintMode();
str.setColor(Color.WHITE);
Font font = new Font("", 0, 13); 
str.setFont(font);
str.drawString("Loading Login Class ...", strx, stry);
splash.update();
try{
    Thread.sleep(5000);
}catch(InterruptedException ex){
}
str.drawString("Loading Main Class ...", strx, stry);
splash.update();
try{
    Thread.sleep(5000);
}catch(InterruptedException ex){
}
splash.close();

Thanks in Advance!

MMujtabaRoohani
  • 483
  • 4
  • 19
  • 1
    What are you using for your IDE Netbeans or Eclipse? – yams Jul 02 '13 at 22:36
  • How this question depends on IDE, I don't think so, it even matters? – MMujtabaRoohani Jul 02 '13 at 22:58
  • I don't see any Gradient painting code. Post your [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Jul 03 '13 at 00:17
  • Just so you know I was asking because Netbeans Comes with a Runtime Container that creates its own splash screen, the whole development process is made easier. Loook it up. – yams Jul 03 '13 at 00:34
  • I am not painting gradient it is previously in my Splash Screen Image that is defined in the manifest.mf file. – MMujtabaRoohani Jul 03 '13 at 00:34
  • I am pointing this out because you said you are new to java. Just take a look at their NeTbeans Runtime Container. – yams Jul 03 '13 at 00:55
  • @MarkBasler , thanks for your advice but can you please suggest me some sites from where i can get the trusted and easy to understand information. – MMujtabaRoohani Jul 03 '13 at 10:30
  • I would be really thankful to you! when i was studying about the painting code i also listened that netbeans had made it much easier but i can't find the tutorial. – MMujtabaRoohani Jul 03 '13 at 10:30
  • Ok Hold on let me find a link for you. – yams Jul 03 '13 at 14:30
  • https://platform.netbeans.org/tutorials/nbm-runtime-container.html this link explains the netbeans platform. – yams Jul 03 '13 at 14:31
  • And here is a tutorial that will have you create a paint program in netbeans https://platform.netbeans.org/tutorials/nbm-paintapp.html – yams Jul 03 '13 at 14:32

1 Answers1

3

You seem to be missing one critical element...

/******************************************************************/
str.setComposite(AlphaComposite.Clear);
str.fillRect(0, 0, width, height);
/******************************************************************/

You basically need to "clear" the graphics context...

enter image description hereenter image description here

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import sun.font.FontManager;

public class TestSplashScreen02 {

    public static void main(String[] args) {
        new TestSplashScreen02();
    }

    public TestSplashScreen02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                SwingWorker worker = new SwingWorker() {
                    @Override
                    protected Object doInBackground() throws Exception {

                        SplashScreen splash = SplashScreen.getSplashScreen();
                        int width = 400;
                        int height = 300;
                        Graphics2D str = splash.createGraphics();
                        /******************************************************************/
                        str.setComposite(AlphaComposite.Clear);
                        str.fillRect(0, 0, width, height);
                        /******************************************************************/
                        str.setPaintMode();
                        str.setColor(Color.WHITE);
                        Font font = str.getFont().deriveFont(Font.BOLD, 24);
                        FontMetrics fm = str.getFontMetrics(font);
                        str.setFont(font);

                        String text = "Loading Login Class ...";

                        int x = (width - fm.stringWidth(text)) / 2;
                        int y = (height - fm.getHeight()) / 2;
                        str.drawString(text, x, y + fm.getAscent());

                        splash.update();
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                        }
                        str.setComposite(AlphaComposite.Clear);
                        str.fillRect(0,0,400,300);
                        str.setPaintMode();

                        text = "Loading Main Class ...";

                        x = (width - fm.stringWidth(text)) / 2;
                        y = (height - fm.getHeight()) / 2;
                        str.drawString(text, x, y + fm.getAscent());
                        System.out.println("Update...");
                        splash.update();
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                        }
                        splash.close();
                        return null;
                    }
                };

                worker.execute();
                try {
                    worker.get();
                } catch (InterruptedException | ExecutionException ex) {
                }

            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366