Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Set and get method problem in java

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Set and get method problem in java

    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, and if the 1st radio button is selected, index is set to 1. Here is the code:


    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
     
     
    //class1
     
    /**
     *
     * @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);
     
        }
     
     
     
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Set and get method problem in java

    Please do not double post your questions. Locking this thread as a duplicate.

Similar Threads

  1. Replies: 1
    Last Post: January 17th, 2013, 07:01 AM
  2. Having java return method problem
    By jayleongwk in forum Java Theory & Questions
    Replies: 2
    Last Post: November 4th, 2012, 03:09 AM
  3. Java Class : How do I set up a toString method for arrays?
    By red7 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 11th, 2012, 05:47 PM
  4. Simple java method problem
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 17th, 2011, 10:39 PM
  5. Should validation code exist in set methods or in a validate method
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 4
    Last Post: May 27th, 2010, 08:37 AM

Tags for this Thread