Solved

AWT Fullscreen SlideShow

Posted on 2004-04-06
5
781 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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