Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Swing / AWT / SWT
Images don't painting
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello!
This is my first post, and I need your help!
I'm trying to display five images, but I can't. I don't have any error message, any warning!
I have two classes:
Class 1:
import java.awt.*; import javax.swing.JFrame; /** The SimpleScreenManager class manages initializing and displaying full screen graphics modes. */ public class SimpleScreenManager { private GraphicsDevice device; /** Creates a new SimpleScreenManager object. */ public SimpleScreenManager() { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = environment.getDefaultScreenDevice(); } /** Enters full screen mode and changes the display mode. */ public void setFullScreen(DisplayMode displayMode, JFrame window) { window.setUndecorated(true); window.setResizable(false); device.setFullScreenWindow(window); if (displayMode != null && device.isDisplayChangeSupported()) { try { device.setDisplayMode(displayMode); } catch (IllegalArgumentException ex) { // ignore - illegal mode for this device } } } /** Returns the window currently used in full screen mode. */ public Window getFullScreenWindow() { return device.getFullScreenWindow(); } /** Restores the screen's display mode. */ public void restoreScreen() { Window window = device.getFullScreenWindow(); if (window != null) { window.dispose(); } device.setFullScreenWindow(null); } }
Class 2:
import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; public class ImageTest extends JFrame { public static void main(String[] args) { DisplayMode displayMode; if (args.length == 3) { displayMode = new DisplayMode( Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), DisplayMode.REFRESH_RATE_UNKNOWN); } else { displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); } ImageTest test = new ImageTest(); test.run(displayMode); } private static final int FONT_SIZE = 24; private static final long DEMO_TIME = 2000; private SimpleScreenManager screen; private Image bgImage; private Image opaqueImage; private Image transparentImage; private Image translucentImage; private Image antiAliasedImage; private boolean imagesLoaded; public void run(DisplayMode displayMode) { setBackground(Color.blue); setForeground(Color.white); setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE)); imagesLoaded = false; screen = new SimpleScreenManager(); try { screen.setFullScreen(displayMode, this); loadImages(); try { Thread.sleep(DEMO_TIME); } catch (InterruptedException ex) { } } finally { screen.restoreScreen(); } } public void loadImages() { bgImage = loadImage("images/background.jpg"); opaqueImage = loadImage("images/opaque.png"); transparentImage = loadImage("images/transparent.png"); translucentImage = loadImage("images/translucent.png"); antiAliasedImage = loadImage("images/antialiased.png"); imagesLoaded = true; // signal to AWT to repaint this window repaint(); } private Image loadImage(String fileName) { return new ImageIcon(fileName).getImage(); } public void paint(Graphics g) { // set text anti-aliasing if (g instanceof Graphics2D) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } // draw images if (imagesLoaded) { g.drawImage(bgImage, 0, 0, null); drawImage(g, opaqueImage, 0, 0, "Opaque"); drawImage(g, transparentImage, 320, 0, "Transparent"); drawImage(g, translucentImage, 0, 300, "Translucent"); drawImage(g, antiAliasedImage, 320, 300, "Translucent (Anti-Aliased)"); } else { g.drawString("Loading Images...", 5, FONT_SIZE); } } public void drawImage(Graphics g, Image image, int x, int y, String caption) { g.drawImage(image, x, y, null); g.drawString(caption, x + 5, y + FONT_SIZE + image.getHeight(null)); } }
I see the background, and the Strings, but no images :-(
Any help will be great!
Thank you in advance!
Daniel Lima
Sharad Jadhav
Greenhorn
Posts: 18
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
# public void loadImages() {
# bgImage = loadImage("/images/background.jpg");
# opaqueImage = loadImage("/images/opaque.png");
# transparentImage = loadImage("/images/transparent.png");
# translucentImage = loadImage("/images/translucent.png");
# antiAliasedImage = loadImage("/images/antialiased.png");
# imagesLoaded = true;
# // signal to AWT to repaint this window
# repaint();
# }
Try using now. Hope this will help you
Java J2EE Hibernate Spring
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello Vijay Kakade !
Tnx for your reply!
This don't work. I've tryed...
I don't know what's happening.... I looked the code thousands of times!
Please try
test
this code in your computer...
Tnx in advance!
Sharad Jadhav
Greenhorn
Posts: 18
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Are all images in classpath? Put that in classpath It should display the images.
Java J2EE Hibernate Spring
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hey Vijay Kakade, tnx!
I'm newbe on J2SE...and I'm using eclipse
ide
.... how can I put this images in classpath?
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Add information:
I've edited the .classpath file, no success.
I've edited the run configurations, no succes.
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello!
More information!
if i do this:
public void loadImages() { bgImage = loadImage("C:/daniel/workspace/ImageTest/src//images/background.jpg"); opaqueImage = loadImage("/images/opaque.png"); transparentImage = loadImage("C:/daniel/workspace/ImageTest/src/images/transparent.png"); translucentImage = loadImage("C:/daniel/workspace/ImageTest/src//images/translucent.png"); antiAliasedImage = loadImage("C:/daniel/workspace/ImageTest/src//images/antialiased.png"); imagesLoaded = true; // signal to AWT to repaint this window repaint(); }
It works!
But I think that I can't put the absolute path im my aplication...
Daniel Lima
Greenhorn
Posts: 14
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Resolved with:
private Image loadImage(String fileName) { //return new ImageIcon(fileName).getImage(); try { return ImageIO.read(getClass().getResource(fileName)); } catch (IOException e) { throw new RuntimeException("Unable to load image " + fileName, e); } }
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Developing Games In Java chapter 2 example not doing as i would have expected
Image Overlapping not proper
Drawing An Image In Full-Screen
Switching between fullscreen and windowed
Having problems with Window and Frame
More...