Solved

Problem with my Main Method

Posted on 2014-12-09
12
106 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 44
bitbucket vs gitbucket 3 57
maven example error 3 52
JList custom Cell Renderer refresh 15 53
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

919 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

17 Experts available now in Live!

Get 1:1 Help Now