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 issues in java.

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

    Default Set and get issues 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, when i click on the 1st button it should set index 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 Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Set and get issues in java.

    it should call a method which draws an image.
    What method is the "it" and what method is "it" calling?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Course Issues (Mod 5)
    By Damarshn in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 18th, 2013, 05:42 AM
  2. Java MySQL issues. Please HELP!
    By pinky4free in forum JDBC & Databases
    Replies: 1
    Last Post: December 22nd, 2012, 06:56 PM
  3. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  4. Java Multithreading Example - Issues
    By vijayinani in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 30th, 2012, 10:46 AM
  5. ISSUES in java.awt
    By mailkamlesh in forum AWT / Java Swing
    Replies: 2
    Last Post: August 25th, 2011, 09:36 AM

Tags for this Thread