Display 2 images - one on top of the other

I am currently overriding paint and using createImage to pull images from files, and Graphics.drawImage to display the 2 images in a panel, with one completely on top of the other, obscuring it. There is a significant "flash" because it looks like 1) paint is being called twice; and 2) the "foreground" image is being drawn first and then the "background" image is being drawn. Most of the time, that is. The order seems to be determined by something beyond me. In paint I am calling Graphics.drawImage(background params..) and then Graphics.drawImage(foreground params...)
Here is my question: How can I get this to look good? I cannot use a LayeredPane; the program is supposed to be light weight, J2SE. Can I somehow combine the images before displaying them? Like, write on on top of the other, and then display the result? I don't need to save the result.
OR, is there a way to draw the background image once, and then up date the foreground image when necessary? My background image is constant and the foregrounds change...
I'm not very up on Java 2D. Should I be using Graphics2D and BufferedImage? What other classes should I use or look at?
Thanks for any help! Feel free to provide code samples!

Flashing sounds like you are doing something basically wrong, like using the AWT instead of Swing.
Learn about Java2D, you're going to need it sooner or later, and don't fear BufferedImages, they're easier to
work with than java.awt.Image:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class TwoImageExample extends JPanel {
    private BufferedImage backImage, foreImage, foreImage1, foreImage2;
    private Timer timer = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                foreImage = (foreImage==foreImage1)? foreImage2 : foreImage1;
                repaint();
    public TwoImageExample(BufferedImage backImage, BufferedImage foreImage1, BufferedImage foreImage2) {
        this.backImage = backImage;
        this.foreImage1 = foreImage1;
        this.foreImage2 = foreImage2;
        //alternates:
        this.foreImage = foreImage1;
        timer.start();
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Insets insets = getInsets();
        int w = getWidth() - insets.left - insets.right;
        int h = getHeight()- insets.top - insets.bottom;
        Graphics2D g2 = (Graphics2D) g.create(insets.left, insets.top, w, h);
        AffineTransform xform = AffineTransform.getTranslateInstance((w-backImage.getWidth())/2, (h-backImage.getHeight())/2);
        g2.drawRenderedImage(backImage, xform);
        xform = AffineTransform.getTranslateInstance((w-foreImage.getWidth())/2, (h-foreImage.getHeight())/2);
        g2.drawRenderedImage(foreImage, xform);
    public static void main(String[] args) throws IOException {
        URL url1 = new URL("http://today.java.net/jag/Image24-large.jpeg");
        URL url2 = new URL("http://java.sun.com/j2se/1.5.0/docs/guide/deployment/images/javalogo52x88.gif");
        URL url3 = new URL("http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/images/javalogo52x88-gray-bg-3.gif");
        JComponent app = new TwoImageExample(ImageIO.read(url1), ImageIO.read(url2), ImageIO.read(url3));
        JFrame f = new JFrame("TwoImageExample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(app);
        f.setSize(800,600);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
}

Similar Messages

  • My Bars are all stacked one on top of the other ie ASK A QUESTIONS, under that: File, Edit, History etc, under that Mozilla Firefox start page, tab tab etc. This uses 1/3 of my screen, can I make it all one bar

    I don't know how to insert a screen shot of my problem but as described above, right now I have 5 bars on top of my window.
    1. Ask a question under that
    2. File Edit View History Bookmarks Tools Help under that
    3; Mozilla Firefox start page (tab Ask a Question)
    under that
    4. Address line (www.you ask me.com) under that
    5. Latest headlines, most visited, City of Balch Springs etc.
    I don't want all these bars and I don't want all that window space used.
    What ever you can do to help will be GREATLY appreciated
    Thanks
    Al

    Thanks for the reply however, I don't want to "not show" these toolbars I just don't want them stacked one on top of the other. See what I mean with the snapshot attached. (Hopefully attached)

  • When i watch vids on any website i see the same vid twice. One on top of the other with a green line dividing them

    When i watch vids on any website i see the same vid twice. One on top of the other with a green line dividing them
    == This happened ==
    Every time Firefox opened
    == Today

    First of all, Firefox support SUCKS! I am looking onto Opera or Chrome.
    Here is my solution. I have WinXP 64 bit with a Nvidia card. I had the same issue and all I had to do is update the video drivers and it fixed the problem. Try this out.

  • After plugging my ipod into my mac mini, my screen split into 2 duplicate screens, one on top of the other. How do i make that go away?

    after plugging my ipod into my mac mini, my screen split into 2 duplicate screens, one on top of the other. How do i make that go away?

    I am not sure about everything, but there is something that may fix some of your problems. Go to System Preferences, and choose the "Universal Access" preference pane. In Universal Access, turn off VoiceOver, Zoom (seeing tab), Sticky Keys, Slow Keys (Keyboard tab), and Mouse Keys (Mouse tab). Make sure the "Enable access for assistive devices" checkbox, which is at the bottom of the preference pane. From there, look at other options and see if they are things you want turned off, or changed from how they are set. If you don't know what different options do, click on the help icon at the bottom left corner of the window. And if you are still baffled, ask here and someone will tell you what it does.
    If you use any of the universal access options or think they could be useful, feel free to leave them on.
    Also, go to the Keyboard and Mouse preference pane, choose the Keyboard Shortcuts tab, and make sure there are no strange keyboard shortcuts set that may interfere with the keys you are having issues with.
    Did you install any new software before this started happening, and if so, how many days before it started happening?
    Let us know if these tips get you anywhere.

  • When I open multiple images, they open one on top of the other, filling the entire workspace

    I'd like to be able to view them and edit them side by side, but when I click on one, it opens on top of the other. I don't know how to change this so the images can be edited side-by-side. I tried Cascade, but it's grayed out and not an option. Help!

      It’s best to change preferences and then cascade and the other options become available.
    Click Edit à Preferences à General
    Then check Allow Floating Documents in full Edit.
     

  • Why do I get two(2) different GUI displays. One with Mcafee advisor the other not?

    Each time I open firefox, I never know which Gui page will appear. One has the mcafee site advisor at the top and bottom right. The other which I have now, has not site advisor displayed??? Am I getting a phoney page display. I have windows XP. Before the update to release 8, I didn't have this problem.

    the CMYK values for the blue logo are 79, 56, 2, 0 and when I click on the RGB tab in InDesign or Illustrator, these programs tell me that the RGB equivalent is 69, 112, 179.
    69|112|179 is the conversion you'll get using the default North American General Purpose Color Settings—or US Web Coated SWOP v2 converted to sRGB. As Willi implies there are many flavors of CMYK and RGB so different sources and destinations, as well as the conversion intent, will produce different results.
    So "branding" color this way doesn't really work because 79|56|2|0 CMYK will look very different on newsprint than it will on a GRAcol profiled press with glossy paper, and 69|112|179 RGB will look very different in a CC app with a ProPhoto RGB assignment then it will look in a web browser with no profile assigned.
    The best you can do is qualify the values—79|56|2|0 US Web Coated (SWOP) v2 CMYK = 69|112|179 sRGB IEC6196-2.1 RGB

  • 2 Clock/Time display on Ipod. One on top of the other.

    This is only visually available when I have my Ipod docked and when I attempt to input my password.
    No idea as to how I can remove this.
    I noticed that 1 time stamp (reads: 6:26) never changes under the current time and I saw a difference after Daylight savings time occured last weekend.
    Have resert time in Settings and that does not make a difference.
    No trauma to device -- just regular use.

    - Reset the iPOd. Nothing will be lost.
    Reset iPod touch:  Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears
    - Restore from backup
    - Restore to factorydefaults/new iPod

  • Is it possible to have 2 display names (one for internal use; the other for external use) in Active Directory?

    The idea:
    When communicating amongst our own staff via email we'd like to see our display names in this format:
    Firstname Lastname (Division/Department/Branch/Unit)
    Whereas, when we send emails to people outside the company, we'd like to be seen as:
    Firstname Lastname (Company Name)
    Is this technically possible?
    Regards,
    Anthony

    Please read about Exchange Simple Display Name if you are using Exchange in your environment. It should be suitable for your needs, but as far I know not every Exchange version support it.
    In Exchange 2003 this Simple Display Name was at Advanced tab of user, but now..
    With Exchange 2013, SimpleDisplayName is configured using Exchange Management Shell (EMS)
    Example:
    Set-Mailbox ALIAS -SimpleDisplayName $True
    ..and as with Exchange 2007/2010, you will need to enable UseSimpleDisplayName on RemoteDomain.
    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer.

  • I have layed out a site with 2 rectangles (one on top of the other) in the footer and the one is being cut off when I publish the site

    The depth of the rectangle is fine when I view in muse but only cuts off when published

    Hello,
    Issue is because of the text box in the middle of the rectangle (In MAIN Master page), Please increase its width and then try to preview it.
    Please take a look at the screenshot. I increased it to 311 px and its working form me.
    Please take a look at the screenshot for better understanding.
    Regards
    Vivek

  • HT1349 itunes print option scrambles all  song titles one on top of the other

    Can anyone solve this problemfor me please Version is windows 7 11.02 (feb 2013) 64 bit and has been installed twice !!

    There is nothing wrong with either your computer or printer. The problem is with the recent iTunes11 download. Apple have said that they are aware of a number of problems and a new release rectifying all of them will be released in a few weeks time. We can only wait and see but until then yes as mentioned by another contributor I have been just printing a playlist and then scale it down in windows excel by copy and pasting. Hope this helps.

  • Two different photos appear in an Event, one photo is on top of the other.  Please help!

    I own a Mac Book Air OS X computer.  In one iPhoto Event I have 936 photos.  I noticed that two completely different photos in the Event are on top of each other.  "Landscape photo A" appears in the slideshow bar at the bottom of the Event, while "Face photo B" appears on the Viewing screen.  Two photos are stored one on top of the other.  The photos were taken on the same trip, but on different days.  I have tried to re-number or re-date all the photos in the Event, hoping that it would separate these photos.  I have also tried to duplicate photo "A" as it appears on the Viewing screen, but photo "B" still lies on top of photo "A" in the slideshow bar at the bottom of the Viewing screen.  Photo "B" also appears as an icon in the Event, while photo "A" doesn't appear at all, it continues to be unseen behind photo "B."
    Please help.  I have already lost several photos in the "Trash" while trying to resolve this problem.
    Thanks,
    d olsen

    What version of iphoto? Is OS X 10.9.5 as you state correct for you OS? where is your iphoto library located? (if on an external drive what forat is it and how is it connected to your Mac?)
    you can try Back up your iPhoto library, Depress and hold the option (alt) and command keys and launch iPhoto - from the resulting first aid window repair permissions, rebuild thumbnails  and if necessary rebuild your database
    LN

  • I have created two related books in Lightroom 5 (Volumes 1 and 2) but my balance of page numbers is off. So I'd like to take some pages out of one book (complete with images) and paste them into the other. Is this possible?

    I have created two related Blurb books in Lightroom 5 (Volumes 1 and 2) but my balance of page numbers is off. So I'd like to take some pages out of one book (complete with images) and paste them into the other. Is this possible?

    Can you zip up a few of your GoPro images, upload them to dropbox.com and post a share link, here, so others can experiment with them, or do you mean this issue is global to all camera models?

  • I have been unsuccessful merging serval pdf files into one file.  I select the Sidebar for both pdfs, then drag one pdf on top of the other and Save the Version.  When I open the file I think I have saved, it shows only the original pdf.  Advice would be

    I have been unsuccessful merging serval pdf files into one file.  I select the Sidebar for both pdfs, then drag one pdf on top of the other and Save the Version.  When I open the file I think I have saved, it shows only the original pdf.  Advice would be appreciated.  Thank you.

    wouldnt it be easier if you could do something like this?
    https://discussions.apple.com/thread/3392535

  • A video on any site is shown double, one on top of each other!

    When ever I want to see a video, no matter what site it is, the screen, where the video is on, shows two images of the video. Basically, the video is condensed into two, one on top of each other, making them wide, but I dont know why there show two!!

    This seems to have happened after the latest Adobe Flash Player update. Might be an issue of compatibility with Firefox, since in Internet Explorer the problem doesn't appear. Haven't tried any other browsers yet.
    To me, it seems that the two images that appear are similar to the ones used in making 3D movies, but instead of displaying them overlapped they are displayed separately. This is just a wild guess and I have no actual proof to back this up.
    PS: I am also using Firefox 3.6.13 on Windows XP.
    Later edit: I have found a workaround for this issue. Right-Click on any improper displayed video and choose Settings. On one of the tabs found there is a checkbox "Enable Hardware Acceleration". It was enabled for me, after disabling it and restarting the browser the video was back to normal.

  • Problem in displaying images one by one in JList

    I have a problem in displaying images one by one in Jlist. I want to show 100 images as a thumbnailview in jlist but it is talking too much time to show all the images after scalling 100 images to thumbnail view and making it into ImageIcon. So, i thought that it would be better way to show one by one after scaling it into thumnailview. But my renderer is getting calling after 100 images scaled down and setting into list. I have posted my code 2 days back but there is no reply.
    http://forum.java.sun.com/thread.jspa?threadID=789943
    I donno where i am missing in this code.
    Plz suggest me some solution for it.

    Where is the scaling done? In the ListCellRenderer? Regardless of where that is being done, I assume you are caching the scaled images after you scale them, right? If not, consider adding some sort of caching.
    This should do the trick...
    Rework your ListCellRenderer to check to see if a scaled version of the image is available. If not, start a thread to do the scaling and create an ImageIcon. In the meantime, while that thread is running, have the renderer return some sort of stock icon, like a little generic image icon. When the scaling thread completes, make a call to repaint() on the JList so the cell renderer gets re-asked for the icons. This time around, the renderer should notice that a scaled version (the ImageIcon) is available. Return that. So, this method doesn't guarantee that the images will be produced in order, 1...2...3..., but it will return images as they become available, and the overall JList rendering won't wait until all 100 are ready. If you want them returned in order, just do all of the scaling in a single thread and queue up the images to be scaled, in order.
    I hoped this helps. (If it does, please throw me some Duke Dollars.)

Maybe you are looking for

  • Time server 1:55:50 wrong!! how could this be??

    Hello everybody, I am develloping an application where I get the time from a time sever and update the (embedded) system time. But I am getting from the server 1h55min minutes earlier than it should be. Most of it I think come from my GMT+0200 locati

  • Multiple VI's using different digital lines in one VI.

    I have created multiple vi's, each of which require either reading or wroting to a different digital line on the CB-68LP board. I need to put them all in one individual vi so that I can see everything on the front panel. When I have tried this the vi

  • What's a good book on RAW?

    Does anyone have any recommendations for someone who would like to learn more about camera RAW, both within the camera itself, and within Photoshop/Bridge? I am a bit of a novice photographer, and that's putting it nicely, and i plan on picking up Sc

  • Re: screen exits

    Hi, I had to add one date field on the me51n trnasaction. Sap is providing one screen exit for that. i.e mereq001 I developed the subscreen saplxm02 0111. And i added one date field for that. And i activated the project too. But iam unable to find th

  • As2 car following the the mouse on x axis

    Hello I would like to make a similar effect with car movement found on that site: http://www.berlinkebab.pl/ i assume that there will be something to do with "_x = _x+_xmouse"  but how do i define the boundries and what about the wheel movement? Or m