1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Display Image in an Applet Example This Java example shows how to display an image using drawImage method of an Java Graphics class. */ import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; /* <applet code = "DisplayImageExample" width = 500 height = 300> <param name = "Image1" value = "one.jpg"> <param name = "Image2" value = "two.jpg"> </applet> */ public class DisplayImageExample extends Applet { Image img1, img2; public void init(){ img1 = getImage(getDocumentBase(), getParameter("Image1")); img2 = getImage(getDocumentBase(), getParameter("Image2")); } public void paint(Graphics g){ //display an image using drwaImage method of Graphics class. g.drawImage(img1, 0,0,this); g.drawImage(img2, 100,100,this); } } |
how to add multiple image in an applet
I have created Image by “grawImage” in applet and all the image(52 playing cards) is displaying and its moving around inside the applet but all the cards shows in a bunch when I am drage on card its shown below of the above card, so I want that when I dragged any one card in the bounch of 52, this gragable card should be shown, its not showing, top most card is showing, because its on top.
Please help me.
my code is-
CardDemo.java
package com.progresso;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
/////////////////////////////////////////////////////////// class CardDemoGUI
/** CardDemoGUI.java – Panel that allows dragging playing card images.
@author Fred Swartz
@version 2004-04-23
*/
public class CardDemoGUI extends JPanel implements MouseListener,
MouseMotionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int IMAGE_WIDTH = 73;
private static final int IMAGE_HEIGHT = 97;
//— instance variables
/** Initial image coords. */
private int _initX = 0; // x coord – set from drag
private int _initY = 0; // y coord – set from drag
Image image,Casinoimage;
/** Position in image of mouse press to make dragging look better. */
private int _dragFromX = 0; // Displacement inside image of mouse press.
private int _dragFromY = 0;
private Card[] _deck = new Card[52];
private Card _currentCard = null; // Current draggable card.
//============================================================= constructor
/** Constructor sets size, colors, and adds mouse listeners.
* @param deck */
public CardDemoGUI() {
//– Read in the cards
String suits = “shdc”;
String faces = “a23456789tjqk”;
int cardPosition = 0;
try {
//image = ImageIO.read(new File(“cards/2c.gif”));
for (int suit=0; suit<suits.length(); suit++)=”” {=”” for=”” (int=”” face=”0;” face<faces.length();=”” face++)=”” {=”” imageicon=”” img=”new” imageicon(“cards=”” “+=”” faces.charat(face)=”” +=”” suits.charat(suit)=”” +=”” “.gif”);=”” image=”ImageIO.read(new” file(“cards=”” “+=”” faces.charat(face)=”” +=”” suits.charat(suit)=”” +=”” “.gif”));=”” all=”” card=”” fetch=”” from=”” here=”” in=”” loop=”” casinoimage=”ImageIO.read(new” file(“cards=”” casino_table.gif”));=”” _deck[cardposition++]=”new” card(image,=”” _initx++,=”” _inity++);=”” system.out.println(“1″);=”” }=”” }=”” }catch=”” (ioexception=”” ex)=”” {=”” handle=”” exception…=”” }=”” –=”” initialize=”” graphics=”” setpreferredsize(new=”” dimension(600,=”” 600));=”” setbackground(color.gray);=”” setforeground(color.black);=”” —=”” add=”” mouse=”” listeners.=”” this.addmouselistener(this);=”” this.addmousemotionlistener(this);=”” }=”” endconstructor=”” ==”=================================================” method=”” paintcomponent=”” **=”” ball=”” is=”” drawn=”” at=”” the=”” last=”” recorded=”” mouse=”” listener=”” coordinates.=”” *=”” public=”” void=”” paintcomponent(graphics=”” g)=”” {=”” super.paintcomponent(g);=”” required=”” g.drawimage(casinoimage,=”” 400,=”” 200,=”” this);=”” –=”” display=”” the=”” cards,=”” starting=”” with=”” the=”” first=”” array=”” element.=”” the=”” array=”” order=”” defines=”” the=”” z-axis=”” depth.=”” for(int=”” i=”0;i<10;i++)” {=”” for=”” (int=”” crd=”0;” crd<_deck.length;=”” crd++)=”” {=”” card=”” c=”_deck[crd];” c.image.painticon(this,=”” g,=”” c.x,=”” c.y);=”” here=”” c.x=”0-51″ and=”” c.y=”0-51″ g.draw3drect(350,=”” 150,=”” 600,=”” 450,true);=”” g.drawimage(c.image,=”” c.x+200,c.y+200,=”” this);=”” c.setbound(100,100,100,100);=”” }=”” }=”” g.drawimage(c.image,=”” c.x,=”” c.y,=”” null);=”” g.drawimage(c.image,c.x,=”” c.y,100,100,this);=”” g.fillrect(=”” c.x,=”” c.y,=”” 100,=”” 100=”” );=”” g.drawstring(“progresso-not=”” the=”” soup”,=”” 400,=”” 50);=”” }=”” end=”” paintcomponent=”” ==”===================================================” method=”” mousepressed=”” public=”” void=”” mousepressed(mouseevent=”” e)=”” {=”” int=”” x=”e.getX();” save=”” the=”” x=”” coord=”” of=”” the=”” click=”” int=”” y=”e.getY();” save=”” the=”” y=”” coord=”” of=”” the=”” click=”” @suppresswarnings(“unused”)=”” graphics=”” img1;=”” –=”” find=”” card=”” image=”” this=”” is=”” in.=”” check=”” from=”” top=”” down.=”” _currentcard=”null;” assume=”” not=”” in=”” any=”” image.=”” for=”” (int=”” crd=”_deck.length-1;” crd=””>=0; crd–) {
Card testCard = _deck[crd];
if (x >= testCard.x && x <= (testCard.x + IMAGE_WIDTH)
&& y >= testCard.y && y <= (testCard.y + IMAGE_HEIGHT)) {
_dragFromX = x – testCard.x; // how far from left
_dragFromY = y – testCard.y; // how far from top
_currentCard = testCard; // Remember what we’re dragging.
System.out.println(_currentCard);
//_currentCard.draw();
//img1.drawImage(img1,200,200,null);
//_currentCard.addFocusListener(null);
break; // Stop when we find the first match.
}
}
}//end mousePressed
//============================================================ mouseDragged
/** Set x,y to mouse position and repaint. */
public void mouseDragged(MouseEvent e) {
if (_currentCard != null) { // Non-null if pressed inside card image.
_currentCard.x = e.getX() – _dragFromX;
_currentCard.y = e.getY() – _dragFromY;
//— Don’t move the image off the screen sides
_currentCard.x = Math.max(_currentCard.x, 0);
_currentCard.x = Math.min(_currentCard.x, getWidth()-IMAGE_WIDTH);
//— Don’t move the image off top or bottom
_currentCard.y = Math.max(_currentCard.y, 0);
_currentCard.y = Math.min(_currentCard.y, getHeight()-IMAGE_HEIGHT);
//_currentCard.drawImage(_currentCard, 30,30, this);
this.repaint(); // Repaint because position changed.
}
}//end mouseDragged
//====================================================== method mouseExited
/** Turn off dragging if mouse exits panel. */
public void mouseExited(MouseEvent e) {
_currentCard = null;
System.out.println(“7”);
}//end mouseExited
//=============================================== Ignore other mouse events.
public void mouseMoved (MouseEvent e) { } // ignore these events
public void mouseEntered (MouseEvent e) { } // ignore these events
public void mouseClicked (MouseEvent e) { } // ignore these events
public void mouseReleased(MouseEvent e) { } // ignore these events
}//endclass CardDemoGUI
image should in same folder ???…or in d drive….