Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

please help can't find getSize() method for my GUI

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to create a window that has a black background...when the mouse is in the left half of the window it changes to red....and when its in the right half it changes to green....this is my code and it's saying it can't find the getSize() method...

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

public class ChangeColorPanel extends JPanel
{
private Point p;
private int x;


/** Creates a new instance of ChangeColorPanel */
public ChangeColorPanel()
{
ColorListener listener = new ColorListener();
addMouseMotionListener(listener);

setBackground(Color.black);
setPreferredSize(new Dimension(400,400));


}
public void paintComponent(Graphics page)
{

super.paintComponent (page);

}
private class ColorListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
Point p = event.getPoint();
if ((p.getX())<((this.getSize().width)/2))
page.setColor(Color.red);
if (p.getX()>(this.getSize().width/2))
page.setColor(Color.green);
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseDragged(MouseEvent event) {}

}


}
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using this.getSize() inside an inner class; the "this" forces the compiler to look for getSize() in that inner class, rather than in the enclosing class. Leave off the "this" and you should be fine. Alternatively, if you want to make things crystal clear, you can write "ChangeColorPanel.this.getSize()" -- that's the way to refer to the "enclosing instance" of an inner class.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm nervous that you declared an instance field in ChangeColorPanel named p and you have a local variable in mouseMoved also named p -- do you realize these are unrelated? Second, you are trying to refer to the parameter passed to paintComponent in method mouseMoved -- that's not going to be allowed -- methods don't have access to other methods' parameters. What you should be doing is changing the panel. For example, changing its background color property. For simplicity, too, I would avoid inner classes if you don't need them. Do you know that MouseEvent has a component property, so that your listener can access the component it is listening to without needed to be an inner class? Demo:

[ February 06, 2006: Message edited by: Jeff Albertson ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really Swing-related - moving to Java in General (intermed.)...
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic