Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:25:21+00:00 2026-05-27T18:25:21+00:00

I want to make it so that while one image is fading out, another

  • 0

I want to make it so that while one image is fading out, another is fading in. I have two BufferedImages and I’m using AWT.

Edit:

package com.cgp.buildtown;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Intro extends JPanel implements Runnable, MouseListener, MouseMotionListener {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private BufferedImage bg, bgsel, bg2, bg2sel;
    private JTextField tf = new JTextField();
    private Font font;
    private int mousex, mousey;
    private boolean buttonClicked = false;

    public Intro() {
        super();
        loadImages();
        addMouseListener(this);
        addMouseMotionListener(this);
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(100, 110, 150, 110));
        tf.setHorizontalAlignment(JTextField.CENTER);
        tf.setFont(loadFont(50f));
        tf.setBackground(new Color(255, 255, 205));
        tf.setBorder(null);
        tf.setForeground(Color.BLACK);
        add(tf, BorderLayout.SOUTH);
    }

    private Font loadFont(Float f) {
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, new File("res/komikatext.ttf"));
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }
        return font.deriveFont(f);
    }

    private void loadImages() {
        try {
            bg = ImageIO.read(new File("res/introbg1.png"));
            bgsel = ImageIO.read(new File("res/introbg1selected.png"));
            bg2 = ImageIO.read(new File("res/introbg2.png"));
            bg2sel = ImageIO.read(new File("res/introbg2selected.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while (true) {
            repaint();
            if (!buttonClicked) {
                if (mousex >= 350 && mousex <= 450 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0)
                    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                else
                    setCursor(Cursor.getDefaultCursor());
            } else {
                if (mousex >= 300 && mousex <= 500 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0)
                    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                else
                    setCursor(Cursor.getDefaultCursor());
            }
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (!buttonClicked) {
            g.drawImage(bg, 0, 0, null);
            if (mousex >= 350 && mousex <= 450 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0)
                g.drawImage(bgsel, 350, 450, null);
        } else if (buttonClicked) {
            g.drawImage(bg2, 0, 0, null);
            if (mousex >= 300 && mousex <= 500 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0)
                g.drawImage(bg2sel, 300, 450, null);
        }
    }

    @SuppressWarnings("deprecation")
    public void mouseClicked(MouseEvent e) {
        if (mousex >= 350 && mousex <= 450 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0 && !buttonClicked) {
            tf.setText(tf.getText() + "'s Town");
            buttonClicked = true;
        } else if (mousex >= 350 && mousex <= 450 && mousey >= 450 && mousey <= 490 && tf.getText().length() > 0 && buttonClicked) {
            BuildTown.replace();
            thread.stop();
        }
    }

    public void mouseMoved(MouseEvent e) {
        mousex = e.getX();
        mousey = e.getY();
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mouseDragged(MouseEvent e) {

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:25:22+00:00Added an answer on May 27, 2026 at 6:25 pm

    You can use Trident to interpolate a property you define in your class. Then during painting you can use this property as alpha in AlphaComposite. Here you can find some examples for AlphaComposite.

    EDIT:
    May be this can help you:

    //define a property to animate
    float opacity;
    
    //define timeline for animation
    Timeline timeline = new Timeline(this);
    timeline.addPropertyToInterpolate("opacity", 1.0f, 0.0f);
    timeline.play();
    
    //inside painting 
    ...
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setComposite(AlphaComposite.SrcOver.derive(this.opacity));
    g2d.drawImage(img1...);
    
    g2d.setComposite(AlphaComposite.SrcOver.derive(1.0 - this.opacity));
    g2d.drawImage(img1...);
    
    g2d.dispose();
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a virtual path (example: ~/Images/Banner.jpg) and I want to make that an
I want to make sure that a set of functions have the same signature
I want to make a table that simply has two integer columns to serve
I have one image button and I would like to fade out one image
I want to make sure that if any error occurs during the database processing
I am creating an xml schema, and I want to make sure that the
Many of my views fetch external resources. I want to make sure that under
I'm troubleshooting a problem with creating Vista shortcuts. I want to make sure that
I want to make an etag that matches what Apache produces. How does apache
I want to make an entity that has an autogenerated primary key, but also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.