Link to home
Start Free TrialLog in
Avatar of OBCT
OBCT

asked on

AWT Fullscreen SlideShow

I'm attempting to create a basic slide show using awt.
So far I've managed to throw togeather a full screen buffer but now I'm having trouble adding an image.

Here's what I've got....


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

public class ImageView implements DisplayModes //the interface contains an array of display modes
{
    private static Color BLACK = Color.black;
    private static Frame mainFrame;
    private        Image image = Toolkit.getDefaultToolkit().getImage("C:\\pictures\\pic1.JPG");

    public ImageView(GraphicsDevice device)
    {
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);

        mainFrame.setLayout(new GridLayout(5,5));
        mainFrame.setUndecorated(true);
        mainFrame.setIgnoreRepaint(true);
        device.setFullScreenWindow(mainFrame);

        addComponents();


        if (device.isDisplayChangeSupported())
        {
            chooseBestDisplayMode(device);
        }

        Rectangle bounds = mainFrame.getBounds();
        mainFrame.createBufferStrategy(1);
        BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();

        Graphics g = bufferStrategy.getDrawGraphics();
                 g.setColor(BLACK);
                 g.fillRect(0,0,bounds.width, bounds.height);

        bufferStrategy.show();
    }

    private static DisplayMode getBestDisplayMode(GraphicsDevice device)
    {
        for (int x = 0; x < BEST_DISPLAY_MODES.length; x++)
        {
            DisplayMode[] modes = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++)
            {
                if (modes[i].getWidth()    == BEST_DISPLAY_MODES[x].getWidth()  &&
                    modes[i].getHeight()   == BEST_DISPLAY_MODES[x].getHeight() &&
                    modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
                {
                    return BEST_DISPLAY_MODES[x];
                }
            }
        }
        return null;
    }

    public static void chooseBestDisplayMode(GraphicsDevice device)
    {
        DisplayMode best = getBestDisplayMode(device);
        if (best != null)
        {
            device.setDisplayMode(best);
        }
    }

    private void addComponents()
    {
        Button exit = new Button("Exit");

        exit.setBounds(0,0,50,50);

        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });

        mainFrame.add(exit);
    }

    public static void main(String[] args)
    {
        GraphicsEnvironment env    = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice      device = env.getDefaultScreenDevice();
        ImageView           test   = new ImageView(device);
        Graphics            graph  = mainFrame.getBufferStrategy().getDrawGraphics();
    }
}

I've tried to add an image using Graphics.drawImage(), however I don't understand how to implement ImageObserver and I'm not even sure if that's the best way to do it.
I still have to write a thread to show each image for however long but I want to take it one step at a time.

Any help, suggestions, tips etc would be greatly appriciated.

Cheers

-OBCT
Avatar of Webstorm
Webstorm

Hi OBCT,

When you call the drawImage method, you can set the ImageObserver parameter to null : g.drawImage(image,x,y,null);

the java.awt.Frame class implements ImageObserver:
g.drawImage(image,x,y,mainFrame);
ASKER CERTIFIED SOLUTION
Avatar of GrandSchtroumpf
GrandSchtroumpf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OBCT

ASKER

Thanks GrandSchtroumpf!!!!
Your always a good help. :)

Cheers

-OBCT