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 4 of 4

Thread: Drawing Image in Java

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

    Default Drawing Image in Java

    Hello, I add a second button to paint a second image on the screen, however when i select the second radio button it is overriding the first image which was previously drawn. How can i solve this? I want to draw both images.Here is my code

    // class1
     
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
     
     
     
    import java.awt.Dimension;
     
     
     
     
    /**
     *
     * @author User
     */
    public class Buildings {
        private Image img,img2;
        private String s;
        private int b_index;
        private BufferedImage image, offScreenImage;
        private int width,height;
     
     
        private String s_shopping="School";
        private String building="Building";
     
     
     
     
        public Buildings(){
     
            try{
     
                img= ImageIO.read(new File("building.jpg"));
                img2=ImageIO.read(new File("s1.jpg"));
     
            }
            catch(IOException e){
            System.out.println("Image not found");
        }
     
     
        }
     
     
     
     
     
        public void PaintBuilding(Graphics2D  offScreenG2D, int x, int y){
     
            offScreenG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
     
            offScreenG2D.drawImage(img, x,y,null);
            offScreenG2D.drawString(building, x+1, y+1);
     
        }
     
    public void PaintSchool(Graphics2D g2d, int x, int y){
     
    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
     
    	g2d.drawImage(img2, x,y,null);
            g2d.drawString(s_shopping, x+1, y+1);
     
        }
     
     
        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.ItemEvent;
    import java.awt.event.ItemListener;
     
    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();
    	static 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);
    		repaint();
    	}
    	public class ButtonListener implements ItemListener{
    		public void itemStateChanged(ItemEvent e){
    			if(e.getSource()==rb1){
    				System.out.println("School");
    				build_index=1;
    				build.setBIndex(build_index);
    				repaint();
    				System.out.println("Index here is:"+build.getBIndex());
     
     
     
    			}
    			if(e.getSource()==rb2){
    				System.out.println("Building");
    				build_index=2;
    				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(build);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame1.setVisible(true);
    	}*/ 
     
    	}
    }
     
     
    //class3
     
    import javax.swing.*;
     
    //import Buildings.MouseDrag;
     
     
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
     
     
    public class MainDrawing extends JPanel
    {
    	int id=2;
    	int index;
    	int height, width, x,y;
    	// this creates Object1-A, 'b'
    	Buildings b= new Buildings();
    	boolean buildingS=false;
    	JButton b1;
    	BuildingOptions bOpt;
    	MouseDrag mouseDrag;
    	public MainDrawing()
    	{
    		index=index;
    		this.setBackground(Color.white);
    		this.setSize(300,150);
    		//mouseDrag = new MouseDrag();
           // addMouseListener(mouseDrag);
            //addMouseMotionListener(mouseDrag);
    		// b.setBIndex(index);
    		b1=new JButton("Add Buildings");
    		b1.setSize(25, 50);
    		b1.addActionListener(new ButtonListener());
    		add(b1);
    		  mouseDrag = new MouseDrag();
    	      addMouseListener(mouseDrag);
    	      addMouseMotionListener(mouseDrag);    
     
    	    }
     
     
    	    private final class MouseDrag extends MouseAdapter {
    	        private boolean dragging = false;
    	        private Point last;
     
    	        @Override
    	        public void mousePressed(MouseEvent m) {
    	            last = m.getPoint();
    	           // dragging = isInsideEllipse(last);
    	            if (!dragging) {
    	                x = last.x;
    	                y = last.y;
    	                width = 0;
    	                height = 0;
    	            }
    	         //   repaint();
    	        }
     
    	        @Override
    	        public void mouseReleased(MouseEvent m) {
    	            //last = null;
    	            dragging = true;
    	           repaint();
    	        }
     
    	        @Override
    	        public void mouseDragged(MouseEvent m) {
    	            int dx = m.getX() - last.x;
    	            int dy = m.getY() - last.y;
    	            if (dragging) {
    	                x += dx;
    	                y += dy;
    	            } else {
    	                width += dx;
    	                height += dy;
    	            }
    	            last = m.getPoint();
    	            repaint();
    	        }
    	    }
     
     
    	public void paint(Graphics g){
    		super.paint(g);
    		Graphics2D g2d=(Graphics2D)g;
    		System.out.println("Index is:"+b.getBIndex());
    		if(b.getBIndex()==1)
     
    			b.PaintSchool(g2d, x, y);
    		if(b.getBIndex()==2)
     
    			b.PaintBuilding(g2d, x, y);
    	}
     
    	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);
    	}
    }


  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: Drawing Image in Java

    Every time the paint() method in the code is called, it completely repaints what is on the screen.
    If you want multiple shapes to be drawn, you will have to draw them each time.

    There are other techniques that you could use to preserve what was drawn before: create an image that is saved in the class, draw on it as desired and then draw the current contents of the image in the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Drawing Image in Java

    How can i create an image that is saved in the class? I tried using BufferedImage instead of Image and i include this :
     
     public void PaintBuilding(Graphics2D  offScreenG2D, int x, int y){
     
            offScreenG2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
     
            offScreenG2D.drawImage(img, x,y,null);
            offScreenG2D.drawString(building, x+1, y+1);
     
        }
     
    public void PaintSchool(Graphics2D g2d, int x, int y){
     
    	g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
     
    	g2d.drawImage(img2, x,y,null);
            g2d.drawString(s_shopping, x+1, y+1);
     
        }
    however the problem is still persisting, i can draw 2 different images, and as soon as i click on the button, the image drawn is disappeared.
    Last edited by Norm; March 15th, 2013 at 12:11 PM. Reason: added / to ending code tag

  4. #4
    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: Drawing Image in Java

    Please make a small simple complete program that compiles, executes and shows the problem.

    Pseudo code:
    create image
    begin EVENT loop
    wait for event
    draw next shape on image
    call repaint()
    in the painting method, draw the image with the shapes on the Graphics context
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Drawing lines using angles in Java HELP
    By shadysback in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 02:10 PM
  2. Image Drawing Problem
    By Matta in forum AWT / Java Swing
    Replies: 8
    Last Post: June 11th, 2011, 06:06 AM
  3. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 07:58 AM
  4. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM