Set and get methods

Page 1 of 1

1 Replies - 211 Views - Last Post: 09 March 2013 - 01:10 PM Rate Topic: -----

#1 Andy_01  Icon User is offline

  • New D.I.C Head

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

Set and get methods

Posted 09 March 2013 - 01:01 PM

Hello I am using a set method to set an index and a get method to return the index, and if index is 1,it should call a method which draws an image. But the index is not changing, and the image is not drawn. I have 3 classes, Building: where i have the set and get methods, MainDrawing, whereby i have a button which on click should display a new frame(BuildingOptions) and lastly BuildingOptions which has radio buttons, here is the 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("picture1.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);
        
    }

    
    
    
}








Is This A Good Question/Topic? 0
  • +

Replies To: 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: Set and get methods

Posted 09 March 2013 - 01:10 PM

Please continue in the topic you've already started rather than starting a duplicate topic. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1