Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   How To Show Image In Java Frame Window (http://www.abandonia.com/vbullet/showthread.php?t=13816)

Nick 22-03-2007 06:01 PM

Here's my sources.

Code:

package main;

import java.awt.*;

public class Main
{

****/**
**** * @param args
**** */
********
****public static void main(String[] args)
****{
********Frame fr = new Window(640, 480, "My Window");
********fr.setVisible(true);
************************
********Button b = new Button("Test");
********b.setSize(100, 20);
********b.setLocation(0, 50);
********fr.add(b);
********
********/*
********Button b = new Button("Test");
********b.setSize(100, 20);
********b.setLocation(0, 30);
********fr.add(b);
*********/
****************
****************
****}

}

And the Window class:

Code:

package main;

import java.awt.*;
import java.awt.event.*;

public class Window extends Frame
{

****/**
**** *
**** */
****
****Image image;

****public Window(int x, int y, String name)
****{
********setSize(640, 480);
********setVisible(true);
********setTitle(name);
********
********addWindowListener (new WindowAdapter()****
************{
****************public void windowClosing(WindowEvent ev)
****************{
********************System.exit (0);
****************}****
************}
********);
********
********setLayout(null);
****}
****
****public void paint(Graphics g)
****{********
********Image im = getToolkit().getImage("Sprites/Character/idle.bmp");
********g.drawImage(im, 0, 0, null);
****************
********g.setFont(new Font("Serif", Font.ITALIC | Font.BOLD, 30));
********g.drawString("Hello, XXI century World!", 20, 100);**
****}
****

}


Geezer 22-03-2007 07:47 PM

Here is a utility class that i put together to paint background images on JPanels with the option of painting a slogan/title in each panel.



Code:


 

import javax.swing.*;
import java.awt.*;
import java.net.*;

 

/** Background is a utility that creates a panel with a background image and
 * a slogan. With the first contructor, the size of the panel, the location of
 * the image and the color of the slogan are passed as parameters. Another constructor
 * creates a solid color background (no image) with the size of the panel, the
 * background color and slogan color as parameters.**/

 

public class Background extends JPanel
{
 String banner;
 String slogan;
 String imageLocation;
 ImageIcon logoIcon;
 Image logoImage;
 URL imageURL;
 Font logoFont;
 int offsetX;
 int offsetY;
 
 public Background(int backgroundWidth, int backgroundHeight, String imageLocation,
 Color fontColor, String banner, String slogan, Font logoFont, int offsetX, int offsetY)
 {
**this.imageLocation = imageLocation;
**this.banner = banner;
**this.slogan = slogan;
**this.logoFont = logoFont;
**this.offsetX = offsetX;
**this.offsetY = offsetY;**
**
**Dimension backgroundDimension = new Dimension(backgroundWidth, backgroundHeight);
**setPreferredSize(backgroundDimension);
**setForeground(fontColor);
**setFont(logoFont);**
**setOpaque(true);
 }
 
 public Background(int backgroundWidth, int backgroundHeight, Color fontColor,
 Color backColor, String banner, String slogan, Font logoFont, int offsetX, int offsetY)
 {
**this.imageLocation = null;
**this.banner = banner;
**this.slogan = slogan;
**this.logoFont = logoFont;
**this.offsetX = offsetX;
**this.offsetY = offsetY;**
****
**Dimension backgroundDimension = new Dimension(backgroundWidth, backgroundHeight);
**setPreferredSize(backgroundDimension);
**setForeground(fontColor);
**setBackground(backColor);
**setFont(logoFont);**
**setOpaque(true);
 }
 
 protected void paintComponent(Graphics g)
 {
**super.paintComponent(g);
**FontMetrics bfm1 = g.getFontMetrics();**
****
**if (imageLocation != null)
**{
** Class metaObject = this.getClass();
** imageURL = metaObject.getResource(imageLocation);
** logoIcon = new ImageIcon(imageURL);
** logoImage = logoIcon.getImage();
** if (logoImage != null)
****g.drawImage(logoImage, 0, 0, getWidth(), getHeight(), this);
**}

**if (banner != "")
**{
** int bannerWidth = bfm1.stringWidth(banner);
** int xc1 = getWidth() / 2 - bannerWidth / 2;
** g.drawString(banner, xc1, offsetY);
**}
****
**if (slogan != "")
**{
** int sloganWidth = bfm1.stringWidth(slogan);
** int xc2 = getWidth() / 2 - sloganWidth / 2;
** g.drawString(slogan, xc2, (offsetY + 25));**
**}
**
//**System.out.println("Graphics painted.");
**
 }
}


Nick 24-03-2007 05:39 PM

I discovered a problem. A path to file with my picture was incorrect. :) Me is LOL.


The current time is 05:31 PM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.