Solved

AWT Fullscreen SlideShow

Posted on 2004-04-06
5
768 Views
Last Modified: 2013-12-29
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
0
Comment
Question by:OBCT
  • 3
5 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10768570
Hi OBCT,

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

0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10769128
the java.awt.Frame class implements ImageObserver:
g.drawImage(image,x,y,mainFrame);
0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 500 total points
ID: 10769410
here is a working version of your code.
i modified a few things here and there, to make it easier for me:


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

interface DisplayModes {
  DisplayMode[] BEST_DISPLAY_MODES = {
    new DisplayMode(1280, 1024, 32, 85),
    new DisplayMode(1152, 864, 32, 85),
    new DisplayMode(1024, 768, 32, 85),
    new DisplayMode(800, 600, 32, 85),
  };
}

public class ImageView extends Panel implements DisplayModes, ActionListener
{
    private static Color BLACK = Color.black;

    private Frame mainFrame;
    private Image image;
    private Button exit = new Button("Exit");

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == exit)
        {
          System.exit(0);
        }
    }

    public ImageView(GraphicsDevice device)
    {
        super();

        image = this.getToolkit().createImage("starwars.jpg");
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image, 0);
        try {
            tracker.waitForID(0);
            if (tracker.isErrorAny()) {
                System.err.println("Error loading image");
            }
        } catch (Exception ex) { ex.printStackTrace();
        }

        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);

        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(exit, BorderLayout.NORTH);
        mainFrame.add(this, BorderLayout.CENTER);

        mainFrame.setUndecorated(true);
        device.setFullScreenWindow(mainFrame);

        exit.addActionListener(this);

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

    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 void paint(Graphics g) {
        g.drawImage(image,10,10,mainFrame);
    }

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

    public static void main(String[] args)
    {
        GraphicsEnvironment env    = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice      device = env.getDefaultScreenDevice();
        ImageView           test   = new ImageView(device);
    }
}
0
 
LVL 9

Author Comment

by:OBCT
ID: 10771085
Thanks GrandSchtroumpf!!!!
Your always a good help. :)

Cheers

-OBCT
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10772589
:°)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question