Hi,My problem is when I am running my applet in appletviewer index.html, Its work fine but not run from the browser as http://localhost:8080/ProgressoApplication/index.html I am using "dynamic web application" in eclipse. I want all the 52 cards will be show in browser as by "appletviewer" and run as "java applet" I can be problem in code also. please check my code.
1) Card.java
package com.progresso;
import java.awt.Image;
public class Card{
private static final long serialVersionUID = 1L;
Image image;
int x;
int y;
public Card(Image image, int x, int y) //here x and y is 0-51;
{
this.image = image;
this.x = x;
this.y = y;
//System.out.println("Card is: "+this.image); // image name of the package cards.cards
}
}
2) CardDemo.java
I am running this file as java applet its working, but not on browser.
Please check can I call a CardDemoGUI constructor from CardDemo class constructor, because when I am comiling this .java file from command prompt with JDK set path, its giving ERROR.
ERROR is ----
CardDemo.java:94: cannot find symbol
symbol : class CardDemoGUI
location: class com.progresso.CardDemo
this.setContentPane(new **C**ardDemoGUI());// error on bold line,
please help me ASAP, because I am trying for three day's, please help me
am using class file that is generated in build/classes/com/progress/MyAll.class file
Code is for CardDemo.java------
package com.progresso;
import java.awt.TextField;
import java.util.Date;
import javax.swing.*;
public class CardDemo extends JApplet implements Runnable
{
private static final long serialVersionUID = 1L;
Thread clockThread;
TextField clockField;
Date date;
//========================================================== method main
/** Create JFrame, add the CardDemoPanel, display. */
boolean running = true;
public void init()
{
// a standard layout to place just one textfield
clockField = new TextField();
add(clockField,"Center");
// Create the thread.
clockThread= new Thread(this);
// and let it start running
clockThread.start();
}
public void destroy()
{
// will cause thread to stop looping
running = false;
// destroy it.
clockThread = null;
}
public void run()
{
// loop until told to stop
while (running)
{
// Construct the current date.
date = new Date();
// Get the hours, minutes and hours
String time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
// Put that result in the textfield
clockField.setText(time);
//Now the reason for threads
try
{
// Wait 500milliseconds before continuing
clockThread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println(e);
}
// he has wait and will now restart his actions.
}
}
/**public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Progresso-Not The Soup");
//window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//window.setContentPane(new CardDemoGUI());
window.pack();
window.show();
window.dispose();
}//end main*/
//=================================================== applet constructor
/** Constructor for applet */
public CardDemo() {
this.setContentPane(new CardDemoGUI());
}
}//endclass CardDemo
and 3) CardDemoGUI:-
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;
import java.lang.reflect.InvocationTargetException;
/////////////////////////////////////////////////////////// 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[] _deck1 = 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";
String title_panel= "Progresso-Not The Soup";
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++);
setBackground(Color.PINK);
}
}
}catch (IOException ex) {
// handle exception...
}
this.addMouseListener(this);
this.addMouseMotionListener(this);
}//endconstructor
//=================================================== method paintComponent
/** Ball is drawn at the last recorded mouse listener coordinates. */
public void paint(Graphics g) {
super.paint(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++)
{
for (crd=0; crd<_deck1.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.drawOval(20, 27, 50, 50);
g.drawImage(c.image, c.x+50,c.y+50, this);
//c.setBound(100,100,100,100);
}
}
}
}//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
_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);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
repaint();
}
});
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);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
repaint();
}
});
}
}//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
public static void invokeAndWait(Runnable doRun) throws InterruptedException,InvocationTargetException
{
final Runnable doHelloWorld = new Runnable() {
public void run() {
System.out.println("Hello World on " + Thread.currentThread());
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doHelloWorld);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished on " + Thread.currentThread());
}
};
appThread.start();
}
}//endclass CardDemoGUI