Dynamic Web Project : Applet not showing in browser [message #764783] |
Mon, 12 December 2011 15:57  |
Eclipse User |
|
|
|
Hi!
I recently finished my interactive GUI-application which I intend to put into a webpage, so I changed the application into an applet and started a dynamic web project. However, the applet doesn't show when I open the html file with internal browser. The applet contains quite a lot of java code, so I decided to make a test with very simple html and java, and the result was the same.
Here is the html body:
"This is HTML text before applet.
<applet code="TestApplet.class" width="200" height="200"></applet>
This is HTML text after applet."
And here is the applet:
"import java.awt.*;
import java.applet.*;
public class TestApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("This is applet text.", 50, 50 );
}
}"
When I open the html in the internal browser, the output is:
"This is HTML text before applet. This is HTML text after applet."
So it seems that something very basic is missing from my project settings. I've been using only default values and I don't have a clue. Any help here would be appreciated.
|
|
|
|
|
|
|
Applet not showing in browser [message #837248 is a reply to message #770554] |
Thu, 05 April 2012 08:46   |
Eclipse User |
|
|
|
Hi have tried web application exactly like above problem,
Now I am really in trouble, Please help me ASAP.
I have created two class 1) Card.java and 2) cardDemo.java(It's Applet) in eclipse and create htmi page index.html in webcontent folder, now I am trying to run this applet in html form its gives a Application Error, I have also tried paste classes folder in WEB-INF and change path applet tag, but not working.
Now my CardDemo and Card class is related ,bellow is the code,
CardDemo.java:-
package com.progresso;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.applet.Applet;
import javax.imageio.ImageIO;
public class CardDemo extends Applet implements MouseListener,MouseMotionListener,ActionListener
{
Button PlayGmae;
Image image;
int x;
int y;
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
/** 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.
public void init()
{
System.out.println("init");
}
public void start()
{
}
public void stop()
{
System.out.println("stop");
}
public void distroy()
{
System.out.println("distroy");
}
//=================================================== applet constructor
//============================================================= constructor
/** Constructor sets size, colors, and adds mouse listeners.
* @param initY
* @param initX
* @param image */
public CardDemo() {
//-- 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
_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 paint(Graphics g) {
super.paint(g); // Required
//-- Display the cards, starting with the first array element.
// The array order defines the z-axis depth.
for (int crd=0; crd<_deck.length; crd++) {
Card c = _deck[crd];
//System.out.println("ok");
//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,c.y, null);
}
g.drawString("Progresso Application", 400, 50);
}//end paintComponent
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
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
//-- 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);
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);
repaint(); // Repaint because position changed.
}
}//end mouseDragged
//=============================================== 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
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}//endclass CardDemo
Card.java:-
package com.progresso;
import java.awt.Image;
/////////////////////////////////////////////////////////////////// Card
/** Card.java - Utility class for grouping the image and its position.
A real card game would expand this into a better class.
@author Fred Swartz
@version 2004-04-23
*/
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
}
public void setLayout(Object object) {
// TODO Auto-generated method stub
}
}
/*
<applet code="CardDemo.class" width=500 height=500>
</applet>
*/
My applet is running from Run as->Java Applet
please -please help me what can i do.
please see the broblem in attachment file
I am running using tomcate URL is
Attachment: problem.bmp
(Size: 2.00MB, Downloaded 331 times)
[Updated on: Thu, 05 April 2012 08:56] by Moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.08289 seconds