Solved

Problem with my Main Method

Posted on 2014-12-09
12
112 Views
Last Modified: 2014-12-12
Hi,

Not sure what the problem is with my main method, need to do this in a JFrame.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

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


public class WatchMe extends JFrame
{
private JPanel panel;
private final int WINDOW_WIDTH = 200;
private final int WINDOW_HEIGHT = 125;
private int firstEyeX = 30;
private int firstEyeY = 40;
private int secondEyeX = 90;
private int secondEyeY = 40;
private int firstPupilX = firstEyeX + 10;
private int firstPupilY = firstEyeY + 20;
private int secondPupilX = secondEyeX + 10;
private int secondPupilY = secondEyeY + 20;
final double proportion = 0.25;
 
public WatchMe()
  {
	 
	setTitle("Colors");
	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  	
    
    addMouseListener(new MyMouseListener());
    
    addMouseMotionListener(new MyMouseMotionListener());
    add(panel);
    setVisible(true);
  }

  
  public void paint(Graphics g)
  {
    
    super.paint(g);
    
    
    g.setColor(Color.black);
    g.drawOval(firstEyeX,firstEyeY,40,80);
    g.drawOval(secondEyeX,secondEyeY,40,80);
    
    
    g.fillOval(firstPupilX,firstPupilY,20,40);
    g.fillOval(secondPupilX,secondPupilY,20,40);  
  }
  
  
  private class MyMouseListener extends MouseAdapter
  {
    public void mouseExited(MouseEvent e)
    {
      
      firstPupilX = firstEyeX + 10;
      firstPupilY = firstEyeY + 20;
      secondPupilX = secondEyeX + 10;
      secondPupilY = secondEyeY + 20;
      
      repaint();
    }
  }
  
  
  private class MyMouseMotionListener extends MouseMotionAdapter
  {
    public void mouseMoved(MouseEvent e)
    {
      double eyeProportionX;
      double eyeProportionY;
      eyeProportionX = e.getX()*proportion;
      eyeProportionY = e.getY()*proportion;
      firstPupilX = (int) (eyeProportionX);
      firstPupilX = firstEyeX + firstPupilX;
      firstPupilY = (int) (eyeProportionY);
      firstPupilY = firstPupilY + firstEyeY;
      secondPupilX = (int) (eyeProportionX);
      secondPupilX = secondPupilX + secondEyeX - 20;
      secondPupilY = (int) (eyeProportionY);
      secondPupilY = secondPupilY + secondEyeY;
      
      if(firstPupilX < 30)
      {
        firstPupilX = 30;
      }
      else if(firstPupilX > 50)
      {
        firstPupilX = 50;
      }
      
      if(firstPupilY < 40)
      {
        firstPupilY = 40;
      }
      else if(firstPupilY > 80)
      {
        firstPupilY = 80;
      }
      
      if(secondPupilX < 90)
      {
        secondPupilX = 90;
      }
      else if(secondPupilX > 110)
      {
        secondPupilX = 110;
      }
      
      
      if(secondPupilY < 40)
      {
        secondPupilY = 40;
      }
      else if(secondPupilY > 80)
      {
        secondPupilY = 80;
      }
      repaint();
    }
    
  } 
	public static void main(String[] args) {
		WatchMe();

	}

Open in new window

0
Comment
Question by:Computer Guy
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 40489189
Try new WatchMe();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40489868
Not sure what the problem is with my main method
http://technojeeves.com/index.php/aliasjava1/17-errors
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40490827
Your main method should be:

    public static void main(String[] args) {
        WatchMe mainFrame = new WatchMe();
        mainFrame.setVisible(true);
    }

Open in new window


and to not have a NullPointerException you should

1) Or remove this line

      add(panel);

Open in new window


2) or initialize panel before adding it:

      panel = new JPanel();
      add(panel);

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 3

Author Comment

by:Computer Guy
ID: 40491170
Hi,

I tried that and now I am getting this error.The method WatchMe() is undefined for the type WatchMe


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

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


public class WatchMe extends JFrame
{
private JPanel panel;
private final int WINDOW_WIDTH = 200;
private final int WINDOW_HEIGHT = 125;
private int firstEyeX = 30;
private int firstEyeY = 40;
private int secondEyeX = 90;
private int secondEyeY = 40;
private int firstPupilX = firstEyeX + 10;
private int firstPupilY = firstEyeY + 20;
private int secondPupilX = secondEyeX + 10;
private int secondPupilY = secondEyeY + 20;
final double proportion = 0.25;
 
public WatchMe()
  {
	 
	setTitle("Colors");
	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  	
    
    addMouseListener(new MyMouseListener());
    
    addMouseMotionListener(new MyMouseMotionListener());
    
    
  }

  
  public void paint(Graphics g)
  {
    
    super.paint(g);
    
    
    g.setColor(Color.black);
    g.drawOval(firstEyeX,firstEyeY,40,80);
    g.drawOval(secondEyeX,secondEyeY,40,80);
    
    
    g.fillOval(firstPupilX,firstPupilY,20,40);
    g.fillOval(secondPupilX,secondPupilY,20,40);  
  }
  
  
  private class MyMouseListener extends MouseAdapter
  {
    public void mouseExited(MouseEvent e)
    {
      
      firstPupilX = firstEyeX + 10;
      firstPupilY = firstEyeY + 20;
      secondPupilX = secondEyeX + 10;
      secondPupilY = secondEyeY + 20;
      
      repaint();
    }
  }
  
  
  private class MyMouseMotionListener extends MouseMotionAdapter
  {
    public void mouseMoved(MouseEvent e)
    {
      double eyeProportionX;
      double eyeProportionY;
      eyeProportionX = e.getX()*proportion;
      eyeProportionY = e.getY()*proportion;
      firstPupilX = (int) (eyeProportionX);
      firstPupilX = firstEyeX + firstPupilX;
      firstPupilY = (int) (eyeProportionY);
      firstPupilY = firstPupilY + firstEyeY;
      secondPupilX = (int) (eyeProportionX);
      secondPupilX = secondPupilX + secondEyeX - 20;
      secondPupilY = (int) (eyeProportionY);
      secondPupilY = secondPupilY + secondEyeY;
      
      if(firstPupilX < 30)
      {
        firstPupilX = 30;
      }
      else if(firstPupilX > 50)
      {
        firstPupilX = 50;
      }
      
      if(firstPupilY < 40)
      {
        firstPupilY = 40;
      }
      else if(firstPupilY > 80)
      {
        firstPupilY = 80;
      }
      
      if(secondPupilX < 90)
      {
        secondPupilX = 90;
      }
      else if(secondPupilX > 110)
      {
        secondPupilX = 110;
      }
      
      
      if(secondPupilY < 40)
      {
        secondPupilY = 40;
      }
      else if(secondPupilY > 80)
      {
        secondPupilY = 80;
      }
      repaint();
    }
    
  } 
	public static void main(String[] args) {
		WatchMe mainFrame = new WatchMe();
        mainFrame.setVisible(true);

	}

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 40491199
I copy/pasted your code and I don't have any error. It just runs (I see "the eyes" appear)
So, I don't know what you mean.

I guess you have the error with other/changed code
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40491233
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The method WatchMe() is undefined for the type WatchMe
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40491305
I do, the error is the with the changed code that is posted above ID: 40491170
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40492724
I do, the error is the with the changed code that is posted above ID: 40491170

I ran that code and there were no errors.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40492827
xeyes for Java?
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40492969
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The method WatchMe() is undefined for the type WatchMe
Some things to try...

Ensure that the source files are actually saved in your IDE (I know my Eclipse sometimes loses the plot and stops auto-saving them when I run my code)
Try "cleaning" your project. If you are using Eclipse, go to the "Project" menu and then "Clean...", select your project and go. Also, check that either you have "Build Automatically" checked (in the same "Project" menu) or if you aren't building automatically, ensure that you have actually built your project before running it.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 40493576
Sorry, when I run that code I don't have any errors. Not your's and no others.
Try to create a new file WatchMe2.java and change all "WatchMe" occurrences to "WatchMe2" if mccarl's post doesn't help you.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40496375
Glad you managed to solve your problem.
Thanx 4 axxepting.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 58
Java string replace 11 54
How to convert from xls to xlsx using java 7 55
how to debug htl and js pages 8 39
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question