Solved

Problem with my Main Method

Posted on 2014-12-09
12
118 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 17

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

696 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