Inner Classes

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1187 Views - Last Post: 22 February 2011 - 09:00 AM Rate Topic: -----

#1 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Inner Classes

Posted 21 February 2011 - 06:07 AM

The following is my code in which i used inner classes. the purpose of the code is to change the color of the circle when the button is clicked.however, i am finding trouble in calling the object in another class.

import javax.swing.*;//Imports everything in the javax.swing package
import java.awt.*;//Imports everything in the java.awt package
import java.awt.geom.Ellipse2D;//Imports the java.awt.geom.Ellipse2D. The Ellipse2D class describes an ellipse that is defined by a bounding rectangle.
import java.awt.event.*;

public class ColourCircle1 {

	

	public static void main(String args[]){
		ColourCircle1 thing = new ColourCircle1();
		thing.go();
		
	}//close main method
	
	public void go(){
		JFrame frame = new JFrame("Coursework Three");//Creates JFrame
		frame.setSize(400,400);//Sets size of JFrame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Sets JFrame to exit on close
		JPanel panel = new NewDrawPanel();//Creates JPanel for NewDrawPanel
		JButton button = new MyButton();//creates JButton while calling class "MyButton1"
		frame.getContentPane().add(button,BorderLayout.SOUTH);
		frame.add(panel,BorderLayout.CENTER);
		frame.setVisible(true);//Sets JFrame to be visible	

	}//close go method
		
	public class MyButton extends JButton implements ActionListener { 								
	 
       String text = "click me";
	 
	   
      public MyButton() {
          setText(text);	//adds text to JButton
          addActionListener(this); //registers JButton with ActionListener
     }//Close MyButton1 Method
	   
	   
	   public void actionPerformed(ActionEvent e){ 
		/*i need to call the shape here in order to change the color but don't know how*/
	
	   
		
					
		}//Close actionPerformed Method
	   }//close mybutton class
		
		
		
	public class NewDrawPanel extends JPanel {

	public Ellipse2D shape;//Encapsulate
		
	public NewDrawPanel(){
		super();//Invokes overriden method
        shape=new Ellipse2D.Double(125,125,150,150);//Sets position and size of shape on JFrame
    }//close RandomDrawPanel

	/*----Getters and Setters----*/
	public Ellipse2D getShape() {
        return shape;
    }


    public void setShape(Ellipse2D shape) {
        this.shape = shape;
    }
	/*---------------------------*/

		@Override//Overrides paintComponent()
		public void paintComponent (Graphics g){
        super.paintComponent(g);//Invokes overriden method.
        Graphics2D g2=(Graphics2D)g;
		g2.setColor(Color.BLUE);//Sets color to blue
		g2.fill(getShape());//Sets shape to be filled with color
		}//close paintComponent method

	}//close NewDrawPanel

}//close class ColourCircle1







Is This A Good Question/Topic? 0
  • +

Replies To: Inner Classes

#2 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 06:45 AM

This should do the trick:
public void actionPerformed(ActionEvent e){ 
  ColourCircle1.this.panel.callYourMethodHere();
}


Was This Post Helpful? 0
  • +
  • -

#3 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 06:54 AM

View Postn8schatten, on 21 February 2011 - 01:45 PM, said:

This should do the trick:
public void actionPerformed(ActionEvent e){ 
  ColourCircle1.this.panel.callYourMethodHere();
}



that didn't work. why are you using panel? panel in my code is the name of the JPanel.
Was This Post Helpful? 0
  • +
  • -

#4 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 06:58 AM

The only shape I can see in your code is the one NewDrawPanel (and thus panel) holds. So you can retrieve it by panel.getShape().
Do you mean another shape? If so, let me know.
Was This Post Helpful? 0
  • +
  • -

#5 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 07:06 AM

View Postn8schatten, on 21 February 2011 - 01:58 PM, said:

The only shape I can see in your code is the one NewDrawPanel (and thus panel) holds. So you can retrieve it by panel.getShape().
Do you mean another shape? If so, let me know.


No, the same shape. But when I use panel it says, cannot find variable.I've also tried
NewDrawPanel.getShape()
to which it said non-static method getShape() cannot be referenced from a static context which confuses me because nothing is static but main.
Was This Post Helpful? 0
  • +
  • -

#6 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 07:13 AM

By doing NewDrawPanel.getShape() you are calling getShape() on the class, as if it was static. That's why it tells you "from a static context". You have to call the getShape() on an object of NewDrawPanel.

Just had a second look at you code and saw I missed a detail. Try making panel a global variable Colorcirlce1. Then try the solution I proposed. (sorry for that, just missed it).
Was This Post Helpful? 0
  • +
  • -

#7 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 07:43 AM

View Postn8schatten, on 21 February 2011 - 02:13 PM, said:

By doing NewDrawPanel.getShape() you are calling getShape() on the class, as if it was static. That's why it tells you "from a static context". You have to call the getShape() on an object of NewDrawPanel.

Just had a second look at you code and saw I missed a detail. Try making panel a global variable Colorcirlce1. Then try the solution I proposed. (sorry for that, just missed it).


will calling getShape() call the same blue circle in paintComponent() or just the plain shape? I thought it would call the plain shape but someone told me it would call the blue circle
Was This Post Helpful? 0
  • +
  • -

#8 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 07:58 AM

It will get you the shape which is currently hold by panel. If you didn't call setShape(someOtherShape), it will return the initially set shape (which is a circle). The color is defined in the paintComponent(..). The shape itself will actually have no color (it's the plain shape).
If you want to change the color, you don't have to change or get the shape. Add a variable containing the color (and the getters and setters) in NewDrawPanel. Then, when the button is pressed, use setColor(..) to set the color. Use this variable when setting the color in your paintComponent().
Was This Post Helpful? 0
  • +
  • -

#9 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 08:13 AM

View Postn8schatten, on 21 February 2011 - 02:58 PM, said:

It will get you the shape which is currently hold by panel. If you didn't call setShape(someOtherShape), it will return the initially set shape (which is a circle). The color is defined in the paintComponent(..). The shape itself will actually have no color (it's the plain shape).
If you want to change the color, you don't have to change or get the shape. Add a variable containing the color (and the getters and setters) in NewDrawPanel. Then, when the button is pressed, use setColor(..) to set the color. Use this variable when setting the color in your paintComponent().


That actually sounds simpler and most logical.Thank You! i will have to make one for each of the two colors won't i?

This post has been edited by serena_3: 21 February 2011 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#10 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 08:22 AM

oh it works! it works! thank you!
Was This Post Helpful? 0
  • +
  • -

#11 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 08:25 AM

Quote

i will have to make one for each of the two colors won't i?

- What do you mean by one?

If you mean one variable per color: No.
If you have the following in NewDrawLabel:
private Color colorOfShape;
...
public void setColor(Color c) {
  this.colorOfShape = c;
}


you only need one variable. You'd then have to provide the color you want to set when calling the setColor-method.

If you mean one button per color: If you want.

Edit: Glad to have helped.

This post has been edited by n8schatten: 21 February 2011 - 08:25 AM

Was This Post Helpful? 1
  • +
  • -

#12 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 10:54 AM

i did everything you told me, but the circle isn't changing color. this is my code:
public void actionPerformed(ActionEvent e){ 
		//ColourCircle1.this.panel.callYourMethodHere();
		NewDrawPanel thing2 = new NewDrawPanel();
		thing2.getColor1();
		thing2.getShape();
		
		thing2.setColor(thing2.getColor1());
		//thing2.fill(thing2.getShape());
		repaint();
	   
		//setText("the circle is now red"); //sets text on JButton to display the number of clicks
					
		}//Close actionPerformed Method
	   }//close mybutton class

Was This Post Helpful? 0
  • +
  • -

#13 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Inner Classes

Posted 21 February 2011 - 11:55 AM

Call repaint on the panel, not on the button.
Was This Post Helpful? 0
  • +
  • -

#14 serena_3  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 31-January 08

Re: Inner Classes

Posted 21 February 2011 - 12:34 PM

View Postn8schatten, on 21 February 2011 - 06:55 PM, said:

Call repaint on the panel, not on the button.

i've tried panel.repaint(); and ColourCircle1.this.panel.repaint(); neither worked
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Inner Classes

Posted 21 February 2011 - 12:47 PM

Can you post all of your code up to this point?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2