NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

need help running a java program

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
marygirl



Joined: 07 Jan 2009
Posts: 2

PostPosted: Wed Jan 07, 2009 12:47 am    Post subject: need help running a java program Reply with quote

I have some Java source code from a book that I want to compile. The name of the file is HashTest.java. In order to compile and run this Java program I created a project in the NetBeans IDE named javaapplication16, and I created a class named HashTest. Once the project was created, I cut and pasted the below source code into my java file that was default created for HashTest.java.

Now I can compile and build the project with no errors, but when I try and run it, I get a dialog box that says the following below (Ignore the [...])
[..................Dialog Box......................................]
Hash Test class wasn't found in JavaApplication16 project

Select the main class:

<No main classes found>
[..................Dialog Box......................................]

I then tried adding a main method but still nothing happens. I don't understand. The original code never used a main method.
Does anyone know what the problem is here? Why won't the project run?


// Here is the source code: *****************************************************************************************************

import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

public class HashTest extends Applet implements ItemListener
{


// Hashtable to add tile images
private Hashtable imageTable;

// a Choice of the various tile images
private Choice selections;

// assume tiles will have the same width and height; this represents
// both a tile's width and height
private int imageSize;

// filename description of our images
private final String[] filenames = { "cement.gif", "dirt.gif", "grass.gif",
"pebbles.gif", "stone.gif", "water.gif" };

// initializes the Applet
public void init()
{
int n = filenames.length;

// create a new Hashtable with n members
imageTable = new Hashtable(n);

// create the Choice
selections = new Choice();

// create a Panel to add our choice at the bottom of the window
Panel p = new Panel();
p.add(selections, BorderLayout.SOUTH);
p.setBackground(Color.RED);

// add the Choice to the applet and register the ItemListener
setLayout(new BorderLayout());
add(p, BorderLayout.SOUTH);
selections.addItemListener(this);

// allocate memory for the images and load 'em in
for(int i = 0; i < n; i++)
{
Image img = getImage(getCodeBase(), filenames[i]);
while(img.getWidth(this) < 0);

// add the image to the Hashtable and the Choice
imageTable.put(filenames[i], img);
selections.add(filenames[i]);

// set the imageSize field
if(i == 0)
{
imageSize = img.getWidth(this);
}
}
} // init

// tiles the currently selected tile image within the Applet
public void paint(Graphics g)
{
// cast the sent Graphics context to get a usable Graphics2D object
Graphics2D g2d = (Graphics2D)g;

// save the Applet's width and height
int width = getSize().width;
int height = getSize().height;

// create an AffineTransform to place tile images
AffineTransform at = new AffineTransform();

// get the currently selected tile image
Image currImage = (Image)imageTable.get(selections.getSelectedItem());

// tile the image throughout the Applet
int y = 0;
while(y < height)
{
int x = 0;
while(x < width)
{
at.setToTranslation(x, y);

// draw the image
g2d.drawImage(currImage, at, this);
x += imageSize;
}
y += imageSize;
}
} // paint

// called when the tile image Choice is changed
public void itemStateChanged(ItemEvent e)
{
// our drop box has changed-- redraw the scene
repaint();
}
} // HashTest



grass.gif
 Description:
 Filesize:  1.31 KB
 Viewed:  3675 Time(s)

grass.gif



dirt.gif
 Description:
 Filesize:  1.91 KB
 Viewed:  3675 Time(s)

dirt.gif



cement.gif
 Description:
 Filesize:  3.15 KB
 Viewed:  3675 Time(s)

cement.gif


Back to top
donlelel



Joined: 05 Jan 2009
Posts: 6

PostPosted: Thu Jan 08, 2009 5:01 pm    Post subject: Reply with quote

It's an applet so you should insert it into a web page.
Back to top
Victor M. Ramirez
Posted via mailing list.





PostPosted: Tue Jan 13, 2009 3:04 pm    Post subject: need help running a java program Reply with quote

If you're tryin' to create an applet in nb6.5, I recommend you this blog entry Wink

http://blogs.sun.com/pc/entry/how_to_create_applet_with

2009/1/6 marygirl <address-removed ([email]address-removed[/email])>
Quote:
I have some Java source code from a book that I want to compile. The name of the file is HashTest.java. In order to compile and run this Java program I created a project in the NetBeans IDE named javaapplication16, and I created a class named HashTest. Once the project was created, I cut and pasted the below source code into my java file that was default created for HashTest.java.



Now I can compile and build the project with no errors, but when I try and run it, I get a dialog box that says the following below (Ignore the [...])

[..................Dialog Box......................................]

Hash Test class wasn't found in JavaApplication16 project



Select the main class:



<No main classes found>

[..................Dialog Box......................................]



I then tried adding a main method but still nothing happens. I don't understand. The original code never used a main method.

Does anyone know what the problem is here? Why won't the project run?





// Here is the source code: *****************************************************************************************************



import java.applet.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.event.*;

import java.util.*;



public class HashTest extends Applet implements ItemListener

{





// Hashtable to add tile images

private Hashtable imageTable;



// a Choice of the various tile images

private Choice selections;



// assume tiles will have the same width and height; this represents

// both a tile's width and height

private int imageSize;



// filename description of our images

private final String[] filenames = { "cement.gif", "dirt.gif", "grass.gif",

"pebbles.gif", "stone.gif", "water.gif" };



// initializes the Applet

public void init()

{

int n = filenames.length;



// create a new Hashtable with n members

imageTable = new Hashtable(n);



// create the Choice

selections = new Choice();



// create a Panel to add our choice at the bottom of the window

Panel p = new Panel();

p.add(selections, BorderLayout.SOUTH);

p.setBackground(Color.RED);



// add the Choice to the applet and register the ItemListener

setLayout(new BorderLayout());

add(p, BorderLayout.SOUTH);

selections.addItemListener(this);



// allocate memory for the images and load 'em in

for(int i = 0; i < n; i++)

{

Image img = getImage(getCodeBase(), filenames[i]);

while(img.getWidth(this) < 0);



// add the image to the Hashtable and the Choice

imageTable.put(filenames[i], img);

selections.add(filenames[i]);



// set the imageSize field

if(i == 0)

{

imageSize = img.getWidth(this);

}

}

} // init



// tiles the currently selected tile image within the Applet

public void paint(Graphics g)

{

// cast the sent Graphics context to get a usable Graphics2D object

Graphics2D g2d = (Graphics2D)g;



// save the Applet's width and height

int width = getSize().width;

int height = getSize().height;



// create an AffineTransform to place tile images

AffineTransform at = new AffineTransform();



// get the currently selected tile image

Image currImage = (Image)imageTable.get(selections.getSelectedItem());



// tile the image throughout the Applet

int y = 0;

while(y < height)

{

int x = 0;

while(x < width)

{

at.setToTranslation(x, y);



// draw the image

g2d.drawImage(currImage, at, this);

x += imageSize;

}

y += imageSize;

}

} // paint



// called when the tile image Choice is changed

public void itemStateChanged(ItemEvent e)

{

// our drop box has changed-- redraw the scene

repaint();

}

} // HashTest




Attachments:
http://forums.netbeans.org//files/grass_704.gif
http://forums.netbeans.org//files/dirt_197.gif
http://forums.netbeans.org//files/cement_374.gif




--
Victor M. Ramirez A.
Ingeniero de Desarrollo
Alemsys S.A.



NOTA CONFIDENCIAL:

La información contenida en este e-mail es confidencial y sólo puede ser utilizada por la persona o la compañia a la cual está dirigido. Si usted no es el receptor autorizado, cualquier retención, difusión, distribución o copia de este mensaje es prohibida y sancionada por la ley. Si por error recibe este mensaje, favor devolver y borrar el mensaje recibido inmediatamente.

CONFIDENCIAL NOTE:

The information in this e-mail is intended to be confidential and only for the use of the person or entity to whom it is addressed. If you are not the intended recipient, any retention, dissemination, distribution or copying of this message is strictly prohibited and sanctioned by law. If you receive this e-mail by mistake, please immediately send back and delete the received message.
Back to top
marygirl



Joined: 07 Jan 2009
Posts: 2

PostPosted: Tue Jan 13, 2009 5:43 pm    Post subject: Problem resolved Reply with quote

Thank you so much Mr. Ramirez for the help. The code is working now.

Mary
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo