9 Replies - 448 Views - Last Post: 10 March 2013 - 11:36 AM Rate Topic: -----

#1 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Problem with set and get methods

Posted 09 March 2013 - 05:33 AM

Hello, I'm having an issue with set and get method. When i clicked on a button, i set index as 1 however when i use the get method to get the index, the index is returned as 0. I want to call a method if the index is 1. here is my code:


//class1

public class Buildings {
    private Image img;
    private String s;
    private int b_index;
    private BufferedImage image;
    
    
    
    
    public Buildings(){
        
        try{
            
            img= ImageIO.read(new File("picture1.jpg"));
            
        }
        catch(IOException e){
        System.out.println("Image not found");
    }
        
        
        
    }
    
    public void PaintBuilding(Graphics2D g2d){
        
        g2d.drawImage(img, 175,200,null);
    }
 
    
    public String getString(){
        return s;
    }
    
    public void setString(String s){
        this.s=s;
    }
    
    public void setBIndex(int index){
        this.b_index=index;
    }
    
    public int getBIndex(){
        return b_index;
    }
    
    
}


//class 2


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

/**
 *
 * @author User
 */
public class MainDrawing extends  JPanel {
    int id=2;
    int index;
    Buildings b= new Buildings();
    boolean buildingS=false;
    
    JButton b1;
    
    public MainDrawing(){
        //this.index=index;
        this.setBackground(Color.white);
        this.setSize(300,150);
        b.setBIndex(index);
        b1=new JButton("Add Buildings");
        b1.setSize(25, 50);
        b1.addActionListener(new ButtonListener());
        add(b1);
        
    }
    
    public void paint(Graphics g){
        super.paint(g); 
        Graphics2D g2d=(Graphics2D)g;
        System.out.println("Index is:"+b.getBIndex());
        if(b.getBIndex()==1)
        
            //if(buildingS==true)
            b.PaintBuilding(g2d);
        
        
        
    }
    
    public class ButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b1)
                index=1;
                // buildingS=true;
                b.setBIndex(index);
                System.out.println("Index:"+b.getBIndex());
            
        }
    }
    
    public static void main(String args[]){
        
        MainDrawing md= new MainDrawing();
        JFrame f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(md);
        f.setVisible(true);
        f.setSize(350,350);
        f.show();
        
    }

    
    
    
}








Is This A Good Question/Topic? 0
  • +

Replies To: Problem with set and get methods

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with set and get methods

Posted 09 March 2013 - 05:49 AM

I'm not getting the same results you describe. Seems to work as you intended, especially after the button is pressed. Ensure that you've compiled and are running your latest version. If unsure, delete the .class file and recompile.

show() has been deprecated. Use setVisible(true/false);
Was This Post Helpful? 1
  • +
  • -

#3 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Re: Problem with set and get methods

Posted 09 March 2013 - 06:17 AM

Thanks,i recompiled it and its working, but the thing is i don't see the image appearing instantaneously when i clicked on the button, i get to see it afterwards when i click on the maximise button on the top of the frame. Do you have any idea how this can be solved?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with set and get methods

Posted 09 March 2013 - 06:22 AM

Add a repaint() to your actionPerformed() method. I think that's what you mean. If not, clarify.
Was This Post Helpful? 0
  • +
  • -

#5 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Re: Problem with set and get methods

Posted 09 March 2013 - 06:26 AM

That's great, it worked as i expected it to.. :)
Was This Post Helpful? 0
  • +
  • -

#6 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Re: Problem with set and get methods

Posted 09 March 2013 - 11:44 PM

I did some modification to code, when i click on the button Add Builings, a new frame is opened(BuidingOptions) where i have 2 radio buttons and when i clicked on the first one i set index as 1 however when i use the get method to get the index, the index is returned as 0. I want to call a method if the index is 1. here is my code:


    //class1
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    /**
    *
    * @author User
    */
    public class Buildings {
    private Image img;
    private String s;
    private int b_index;
    private BufferedImage image;
    private String s_shopping="Shopping Mall";
    public Buildings(){
    try{
    img= ImageIO.read(new File("C:/Users/User/Desktop/R0303/src/Designer/simulation.jpg"));
    }
    catch(IOException e){
    System.out.println("Image not found");
    }
    }
    public void PaintBuilding(Graphics2D g2d){
    g2d.drawImage(img, 175,200,null);
    g2d.drawString(s_shopping, 175+6, 200+6);
    }
    public String getString(){
    return s;
    }
    public void setString(String s){
    this.s=s;
    }
    public void setBIndex(int index){
    this.b_index=index;
    }
    public int getBIndex(){
    return b_index;
    }
    }
    //class2
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    /**
    *
    * @author User
    */
    public class BuildingOptions extends JFrame{
    JRadioButton rb1;
    JRadioButton rb2;
    ButtonGroup bgroup;
    private int build_index;
    JPanel panelRbutton;
    Buildings build= new Buildings();
    public BuildingOptions(){
    super("Choose a building");
    build.setBIndex(build_index);
    rb1= new JRadioButton("Add a School");
    rb2=new JRadioButton("Add a Shopping Mall");
    //rb3=new JRadioButton()
    bgroup= new ButtonGroup();
    bgroup.add(rb1);
    bgroup.add(rb2);
    rb1.addItemListener(new ButtonListener());
    rb2.addItemListener(new ButtonListener());
    panelRbutton= new JPanel();
    panelRbutton.add(rb1);
    panelRbutton.add(rb2);
    this.add(panelRbutton);
    this.setSize(250, 300);
    this.setBackground(Color.LIGHT_GRAY);
    this.setVisible(true);
    }
    public class ButtonListener implements ItemListener{
    public void itemStateChanged(ItemEvent e){
    if(e.getSource()==rb1){
    System.out.println("Hello world");
    build_index=1;
    build.setBIndex(build_index);
    //repaint();
    System.out.println("Index here is:"+build.getBIndex());
    }
    }
    }
    public static void Main(String [] args){
    BuildingOptions frame1= new BuildingOptions();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setVisible(true);
    }
    }
    //class3
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    /**
    *
    * @author User
    */
    public class MainDrawing extends JPanel {
    int id=2;
    int index;
    Buildings b= new Buildings();
    boolean buildingS=false;
    JButton b1;
    BuildingOptions bOpt;
    public MainDrawing(){
    //this.index=index;
    this.setBackground(Color.white);
    this.setSize(300,150);
    // b.setBIndex(index);
    b1=new JButton("Add Buildings");
    b1.setSize(25, 50);
    b1.addActionListener(new ButtonListener());
    add(b1);
    }
    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d=(Graphics2D)g;
    System.out.println("Index is:"+b.getBIndex());
    if(b.getBIndex()==1)
    //if(buildingS==true)
    b.PaintBuilding(g2d);
    }
    public class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b1)
    bOpt= new BuildingOptions();
    bOpt.setVisible(true);
    bOpt.setSize(250, 250);
    System.out.println("Index in button listener in class buttonListener:"+b.getBIndex());
    repaint();
    /*index=1;
    // buildingS=true;
    b.setBIndex(index);
    repaint();
    System.out.println("Index:"+b.getBIndex());*/
    }
    }
    public static void main(String args[]){
    MainDrawing md= new MainDrawing();
    JFrame f=new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //f.setBackground(Color.yellow);
    f.setContentPane(md);
    f.setVisible(true);
    f.setSize(350,350);
    f.setVisible(true);
    }
    }








Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with set and get methods

Posted 10 March 2013 - 03:10 AM

A concept that gives everyone fits in their early experience with OOP is how to pass data between objects. What you tried was

Create Object1-A
Create Object2
Create Object1-B in Object 2
Set variables in Object1-B from Object2
Leave Object2 (destroying Object1-B?)
Return to Object1-A
Print variables from Object1-A (and see they haven't changed)

Another way (that works) is to pass Object1-A to Object2 and then change the variables in Object1-A from Object2.

I have minimally changed the code in your second and third classes to demonstrate passing data between objects as described above. I made minimal changes with comments at each change so that you can hopefully recognize your original work and understand the purpose of the changes. However, maximum changes are recommended to better organize and clean up your code, including running the GUI on the EDT rather than the main thread as you're doing now. Once you understand the changes I've made, I'd be happy to show you the other changes I'd recommend.
//class2
class BuildingOptions extends JFrame{
	JRadioButton rb1;
	JRadioButton rb2;
	ButtonGroup bgroup;
	private int build_index;
	JPanel panelRbutton;
	// modify the line below to the second line
	// Buildings build= new Buildings();
	Buildings build;
	
	// modify the constructor as shown
	public BuildingOptions( Buildings build ){
		super("Choose a building");
		
		// add this line:
		this.build = build;
		
		build.setBIndex(build_index);
		rb1= new JRadioButton("Add a School");
		rb2=new JRadioButton("Add a Shopping Mall");
		//rb3=new JRadioButton()
		bgroup= new ButtonGroup();
		bgroup.add(rb1);
		bgroup.add(rb2);
		rb1.addItemListener(new ButtonListener());
		rb2.addItemListener(new ButtonListener());
		panelRbutton= new JPanel();
		panelRbutton.add(rb1);
		panelRbutton.add(rb2);
		this.add(panelRbutton);
		this.setSize(250, 300);
		this.setBackground(Color.LIGHT_GRAY);
		this.setVisible(true);
	}
	public class ButtonListener implements ItemListener{
		public void itemStateChanged(ItemEvent e){
			if(e.getSource()==rb1){
				System.out.println("Hello world");
				build_index=1;
				build.setBIndex(build_index);
				//repaint();
				System.out.println("Index here is:"+build.getBIndex());
			}
		}
	}
	// commented out main() method to avoid confusion:
	/*
	public static void Main(String [] args){
		BuildingOptions frame1= new BuildingOptions();
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame1.setVisible(true);
	} */
}

//class3

public class MainDrawing extends JPanel
{
	int id=2;
	int index;
	// this creates Object1-A, 'b'
	Buildings b= new Buildings();
	boolean buildingS=false;
	JButton b1;
	BuildingOptions bOpt;
	public MainDrawing()
	{
		//this.index=index;
		this.setBackground(Color.white);
		this.setSize(300,150);
		// b.setBIndex(index);
		b1=new JButton("Add Buildings");
		b1.setSize(25, 50);
		b1.addActionListener(new ButtonListener());
		add(b1);
	}
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2d=(Graphics2D)g;
		System.out.println("Index is:"+b.getBIndex());
		if(b.getBIndex()==1)
			//if(buildingS==true)
			b.PaintBuilding(g2d);
	}
	public class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==b1)
				// this creates Object2, passing a reference to Object1-A
				bOpt= new BuildingOptions( b );
			bOpt.setVisible(true);
			bOpt.setSize(250, 250);
			System.out.println("Index in button listener in class buttonListener:"+b.getBIndex());
			repaint();
			/*index=1;
// buildingS=true;
b.setBIndex(index);
repaint();
System.out.println("Index:"+b.getBIndex());*/
		}
	}
	public static void main(String args[]){
		MainDrawing md= new MainDrawing();
		JFrame f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.setBackground(Color.yellow);
		f.setContentPane(md);
		f.setVisible(true);
		f.setSize(350,350);
		f.setVisible(true);
	}
}

Was This Post Helpful? 1
  • +
  • -

#8 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Re: Problem with set and get methods

Posted 10 March 2013 - 04:23 AM

Your reply along with the comments were indeed helpful, i now understand where i was lagging behind. I wrongly used the concept of OOP.
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Problem with set and get methods

Posted 10 March 2013 - 07:35 AM

Glad that helped.

One important change you should make is to override the paintComponent() method in MyPanel rather than the paint() method. You would also change the corresponding super call to:

super.paintComponent( g );

In AWT, it was common to override the paint() method, but Swing changed that to the paintComponent() method. You can learn more about painting in Swing in this tutorial. Note that one of the suggestions in that tutorial is to add a repaint() call to the paintComponent() method which is just wrong - unless they intended to show the negative effects of such a bad practice - but that wasn't their intent. The suggested call to repaint() should be added to the end of the moveSquare() method instead of the paintComponent() method. I already suggested they fix the tutorial.

Keep coding.
Was This Post Helpful? 0
  • +
  • -

#10 Andy_01  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-February 13

Re: Problem with set and get methods

Posted 10 March 2013 - 11:36 AM

Thanks a bunch, i really appreciate the suggestions u included and also your comments. They were helpful as i mentioned earlier. I would consider using the paintComponent() method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1