Solved

Problem with my Main Method

Posted on 2014-12-09
12
104 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
Comment Utility
Try new WatchMe();
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
 
LVL 3

Author Comment

by:Computer Guy
Comment Utility
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
Comment Utility
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
Comment Utility
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The method WatchMe() is undefined for the type WatchMe
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Author Comment

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

Expert Comment

by:krakatoa
Comment Utility
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
Comment Utility
xeyes for Java?
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
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
Comment Utility
Glad you managed to solve your problem.
Thanx 4 axxepting.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now