Results 1 to 2 of 2
- 12-09-2010, 10:26 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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) { } }
- 12-10-2010, 08:39 AM #2
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
-
MousePressed event help needed(CS106A )
By ccie007 in forum New To JavaReplies: 4Last Post: 11-09-2010, 07:29 PM -
problem regarding for storage of images in database
By mudit222 in forum JavaServer Pages (JSP) and JSTLReplies: 11Last Post: 04-15-2010, 10:37 PM -
Images problem in treeviewer
By Gandolf in forum SWT / JFaceReplies: 0Last Post: 04-03-2009, 02:18 PM -
problem in socket connection in sending images
By vibhor in forum NetworkingReplies: 2Last Post: 02-20-2009, 05:39 AM -
ArrayList problem with images
By Cymro in forum New To JavaReplies: 2Last Post: 02-05-2008, 06:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks