10 Replies - 1046 Views - Last Post: 12 April 2007 - 10:22 AM Rate Topic: -----

#1 sluh2689  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 07

Help With Mouse Program

Posted 11 April 2007 - 09:03 AM

I just wrote this program, and I cant for the life of me figure out why it wont compile, here is what I have so far

 
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class OffCenterPanel extends JPanel
{
	private DecimalFormat fmt;
	private Point current;
	private int Xcenter, Ycenter;
	private double length;
	
	public OffCenterPanel (int width, int height)
	{
		addMouseListener (new OffCenterListener());
		
		Xcenter = width/2;
		Ycenter = height/2;
		
		fmt = new DecimalFormat ("0.##");
		
		setBackground (Color.black);
	}
	
	public void paintComponent (Graphics page)
	{
	
		super.paintComponent (page);
		
		page.setColor (Color.red);
		page.drawOval (Xcenter-3, Ycenter-3, 6, 6);
		
		if (current != null)
		{
			page.drawLine (current.x, current.y, Xcenter, Ycenter);
			page.drawString ("Distance: " +fmt.format(length), 10, 15);
		}
	}
	
	private class OffCenterListener extends MouseAdapter
	{
		public void mouseClicked (MouseEvent event)
		{
			current = event.getPoint();
			length = Math.sqrt(Math.pow((current.x-Xcenter), 2) + Math.pow((current.y-Ycenter),2));
			
			repaint();
			
		}
	}
}			



This is my driver
import javax.swing.*;

public class OffCenter extends JApplet
{
	
		private final int WIDTH = 300, HEIGHT = 300;
	
	
		public void main()
		{
			getContentPane().add(new OffCenterPanel (WIDTH, HEIGHT));
		
			setSize (WIDTH, HEIGHT);
		}
	
}		



And this is the error I am getting

Exception in thread "main" java.lang.NoSuchMethodError: main

Plz help me

Is This A Good Question/Topic? 0
  • +

Replies To: Help With Mouse Program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help With Mouse Program

Posted 11 April 2007 - 09:41 AM

It's saying it canot locate the program entry point. What compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help With Mouse Program

Posted 11 April 2007 - 09:43 AM

your main method needs to be static.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Help With Mouse Program

Posted 11 April 2007 - 09:50 AM

to further with an example:
public static void main(String args[])

this is the proper declaration for a main method whether it is to take arguments or not.
Was This Post Helpful? 0
  • +
  • -

#5 sluh2689  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 11 April 2007 - 09:58 AM

I tried using the above code earlier, but it just gave me the same error, and told me that my main method could not be static.
Was This Post Helpful? 0
  • +
  • -

#6 tlcoolj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 11 April 2007 - 10:20 AM

Just kinda throwing this one out here without actually trying anything in my IDE but it seems to me that i remember JApplets and Applets don't have a main method.. they use the init() method instead..
Was This Post Helpful? 0
  • +
  • -

#7 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Help With Mouse Program

Posted 11 April 2007 - 11:05 AM

Didn't even notice it was an applet. I'm sleepy and apparently slipping today with my miss of the no-parameter main method and now my miss of it extending applet (if that matters). :)

This post has been edited by alcdotcom: 11 April 2007 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#8 sluh2689  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 11 April 2007 - 12:17 PM

Yah I thought it was supposed to be init() as well, but I am getting an error in main...and I have no idea what it is. It says there are no errors when I compile it, but when I try to run it, it fails.
Was This Post Helpful? 0
  • +
  • -

#9 tlcoolj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 11 April 2007 - 01:15 PM

View Postsluh2689, on 11 Apr, 2007 - 12:17 PM, said:

Yah I thought it was supposed to be init() as well, but I am getting an error in main...and I have no idea what it is. It says there are no errors when I compile it, but when I try to run it, it fails.


ok maybe i didn't go into detail but i will try to go over this again. There is NO main method in an applet.. as your seeing it just won't work. A main method is used in applications, it is the first method called and is where you put the nitty gritty of your app. The method in an applet that is taking place of the main method is init()...

replace your code with:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SampleApplet extends JApplet
{

Container con = this.getContentPane();
private final int WIDTH = 300, HEIGHT = 300;

public void init(){

con.add(new OffCenterPanel (WIDTH, HEIGHT));




}
}

you should see that it complies just fine... PS remember to create an html file:
<HTML>
<HEAD>
<TITLE>MyApplet Example1</TITLE>
</HEAD>
<BODY>
<H1>MyApplet</H1>
<HR>
<P>
<APPLET CODE="SampleApplet.class" WIDTH="300" HEIGHT="300">
</APPLET>
</P>
<HR>
</BODY>
</HTML>

in order to run your applet..
This should get your applet up and running but as far as what your doing with your OffCenterPanel.java i think your still going to have some problems. your logic is still off... One of the things that really sticks out is, i believe you want your applets to respond to mouse driven events but in order for it to do that you must implement MouseListener... Maybe if you break down exactly what you want the applet to do then i could help you get pointed in the right direction
Was This Post Helpful? 0
  • +
  • -

#10 sluh2689  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 12 April 2007 - 10:04 AM

It is supposed to respond to the mouse click event. When the mouse is clicked over the applet a line is drawn from where ever the click was to the center of the applet. The distance (in pixels) of that line is to be displayed in the corner. Does that help?
Was This Post Helpful? 0
  • +
  • -

#11 sluh2689  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-April 07

Re: Help With Mouse Program

Posted 12 April 2007 - 10:22 AM

It works now, thanx for your help man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1