Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

AWT Applet Instantiation Error

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe someone here can help me. I keep getting an instantiation error (runtime) in my applet code. I'm trying to get an applet to display an image wherever I click the mouse. here's the code:
//< applet code="AppletApp.class" width=200 height=300>< /applet>
import java.awt.*; //<-- added code
import java.applet.*; //<-- added code
import java.awt.event.*; //<-- added code
abstract class AppletApp extends Applet implements MouseListener{

private Image duke;
private int xPos,yPos;

public void init() {

duke = getImage(getDocumentBase(),"duke.gif");
addMouseListener(this);

}
public void paint(Graphics g) {

setBackground(Color.white);
g.drawImage(duke, xPos, yPos, this);
}
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
repaint();
}

}
thanks to anyone who can help
Dave
[This message has been edited by Jim Yingst (edited July 07, 2001).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Record:
maybe someone here can help me. I keep getting an instantiation error (runtime) in my applet code. I'm trying to get an applet to display an image wherever I click the mouse. here's the code:

//< applet code="AppletApp.class" width=200 height=300>< /applet>

import java.awt.*; //<-- added code
import java.applet.*; //<-- added code
import java.awt.event.*; //<-- added code

abstract class AppletApp extends Applet implements MouseListener{

private Image duke;

private int xPos,yPos;


public void init() {

duke = getImage(getDocumentBase(),"duke.gif");
addMouseListener(this);

}

public void paint(Graphics g) {

setBackground(Color.white);
g.drawImage(duke, xPos, yPos, this);
}

public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
repaint();
}


}

thanks to anyone who can help

Dave

[This message has been edited by Jim Yingst (edited July 07, 2001).]


I myself am bit new to Java. But as far as my knowledge goes, u need do 2 things in order for ur applet to function- the first being, the class should be declared public. Since, the class is now no more abstract, it SHOULD implement ALL the methods in the interface MouseListener (I see only mousePressed being implemented in ur applet code). Even though u don't have any functionality to be written inside the other methods of the interface, u need include the method implementation with empty 'body'. [mouseClicked, mouseEntered, mouseExited, mouseReleased are the other methods of the interface]. A shortcut to do the above would be to use the MouseAdaptor class which does provide default implementations for all the methods of the interface MouseListener.
 
Suma MM
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code after making the class 'non-abstract' and after adding the implementation for the other methods.
-----------------------------------------------------------------
import java.awt.*; //<-- added code
import java.applet.*; //<-- added code
import java.awt.event.*; //<-- added code
public class AppletApp extends Applet implements MouseListener{
private Image duke;
private int xPos,yPos;

public void init() {
duke = getImage(getDocumentBase(),"duke.gif");
addMouseListener(this);
}

public void paint(Graphics g) {
setBackground(Color.white);
g.drawImage(duke, xPos, yPos, this);
}
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
repaint();
}
public void mouseExited(MouseEvent e){}
public void mouseClicked (MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased (MouseEvent e){}
}
-----------------------------------------------------------------
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic