?
Solved

Problem with my Main Method

Posted on 2014-12-09
12
Medium Priority
?
119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 36

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

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