Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Draw extends JPanel{
	private static final long serialVersionUID = 1L;
	String s;
	int x,y,height,width;
	public Draw(){
		x=y=0;
		this.height=this.width=40;
		this.setLayout(null);
		addLisener();
		
	}
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		ImageIcon icon=new ImageIcon("man.gif");
		icon.paintIcon(this, g, x, y);
	}
	
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class paintFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	Draw d;	
	public paintFrame(){
			this.setSize(500, 500);
			this.setLocation(100,100);
			
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			initial();
			addComoPonent();
			this.setLayout(null);
		}
	public void paint(Graphics g){
		g.setColor(Color.RED);
		ImageIcon icon=new ImageIcon("background.jpg");
		Image i=icon.getImage();
		g.drawImage(i, 0, 0, null);
		d.paintComponent(g);
	}
	
	public void initial(){
		d=new Draw();
	}
	public void addComoPonent(){
		this.add(d);
		this.addKeyListener(new keyListen());
	}
		
}
public class Main{
	public static void main(String[] args) {
		paintFrame pai=new paintFrame();
		pai.setVisible(true);

	}

}`
Posted
Comments
Sergey Alexandrovich Kryukov 8-Dec-15 11:11am    
Despite of "addKeyListener" present in the code, there is no a code of a listener (event handle). So, what have you tried so far?
—SA
Member 12193581 8-Dec-15 13:04pm    
yeah i am tried but the key listener doesn't work.but when i am used motion listener it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900