Results 1 to 2 of 2
  1. #1
    bttc44 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Problem with images on mousepressed.

    So, I'm new to Java and I was looking to get some help with some simple code,

    just 3 radio buttons, each one selects a different image and is displayed when the mouse is clicked, the problem is that the first time any one of the 3 images is made on the applet it clears all the previous images, but any times after that it works fine.

    Any suggestions?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MouseEventsFP2 extends JApplet implements ItemListener,MouseListener,ActionListener
    
    {
    	
    	int endX,endY,x;
    	JPanel p1,p2;
    	JRadioButton r1,r2,r3;
    	ButtonGroup group;
    	JButton clear;
    	Image img,img2,img3;
    
    	public void init()
    	{
    		setLayout(new BorderLayout());
    		setSize(300,200);
    		
    		p1=new JPanel(new FlowLayout());
    		p1.setBackground(Color.WHITE);
    		add(p1,BorderLayout.CENTER);
    		addMouseListener(this);
    		
    		p2=new JPanel(new FlowLayout());
    		add(p2,BorderLayout.SOUTH);
    		
    		r1=new JRadioButton("Smiley");
    		r2=new JRadioButton("Frowney");
    		r3=new JRadioButton("Paw");
    		group=new ButtonGroup();
    		group.add(r1);
    		group.add(r2);
    		group.add(r3);
    		p2.add(r1);
    		p2.add(r2);
    		p2.add(r3);
    		r1.addItemListener(this);
    		r2.addItemListener(this);
    		r3.addItemListener(this);
    		r1.setSelected(true);
    		
    		clear=new JButton("Clear");
    		p2.add(clear);
    		clear.addActionListener(this);
    		
    		img=getImage(getCodeBase(),"images/smiley.gif");
    		img2=getImage(getCodeBase(),"images/frowney.gif");
    		img3=getImage(getCodeBase(),"images/paw.gif");
    		
    		x=1;
    
    	}
    	
    	
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    	}
    	
    	
    	public void itemStateChanged(ItemEvent ie)
    		
    	{
    
    		Object source=ie.getSource();
    		if(source==r1)
    		{
    		x=1;		
    							
    		}
    		
    		if(source==r2)
    		{
    		x=2;
    	
    		}
    		
    		if(source==r3)
    		{
    		x=3;
    		
    		}
    	}
    	
    	
    	public void actionPerformed(ActionEvent ae)
    	{
    	
    	repaint();
    	
    	}
    
    	
    	
    	// MouseEvent
    	public void mouseClicked(MouseEvent me)
    	{
    
    	}
    
    
    	public void mousePressed(MouseEvent me)
    	{
    		endX = me.getX();
    		endY = me.getY();
    		
    		Graphics g = getGraphics();
    	
    		if(x==1)
    		{	
    		g.drawImage(img,endX-17,endY-17,35,35,this);					
    		}
    		
    		if(x==2)
    		{
    		g.drawImage(img2,endX-17,endY-17,35,35,this);
    		}
    		
    		if(x==3)
    		{
    		g.drawImage(img3,endX-17,endY-17,35,35,this);
    		}
    	
    	}
    	
    	
    	public void mouseReleased(MouseEvent me)
    	{
    	}
    	
    	
    	public void mouseEntered(MouseEvent me)
    	{
    	}
    	
    	
    	public void mouseExited(MouseEvent me)
    	{
    	}
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Overriding a method to call the super implementation and do nothing else is superfluous.

    Never never never never use getGraphics() of a Component.

    db

Similar Threads

  1. MousePressed event help needed(CS106A )
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 11-09-2010, 07:29 PM
  2. problem regarding for storage of images in database
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 04-15-2010, 10:37 PM
  3. Images problem in treeviewer
    By Gandolf in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 02:18 PM
  4. problem in socket connection in sending images
    By vibhor in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 05:39 AM
  5. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 06:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •