Solved

AWT Fullscreen SlideShow

Posted on 2004-04-06
5
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

732 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