import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
class Move extends JFrame
{
JButton left=new JButton("Left"),right=new JButton("Right");
JButton up=new JButton("up"),down=new JButton("down");
JComponent movingthing;
JLayeredPane pane=new JLayeredPane();
JLayeredPane frame=new JLayeredPane();
Timer timerl,timerr,timerd,timerup;
JPanel jp;
public Mover()
{
this.setSize(400,400);
this.setResizable(true);
setContentPane(pane);
movingthing=new JComponent() {
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fill3DRect((movingthing.getWidth()/2)-25, (movingthing.getHeight()/2)-25, 50, 50,true);
}};
left.setBounds((getWidth()/2)-140,getHeight()-50,100,20);
right.setBounds((getWidth()/2)+2,getHeight()-50,100,20);
up.setBounds((getWidth()/2)-80,getHeight()-90,100,20);
down.setBounds((getWidth()/2)-80,getHeight()-20,100,20);
movingthing.setBounds((getWidth()/2)-25,getY()+10,50,50);
getContentPane().add(movingthing,0);
getContentPane().add(left,0);
getContentPane().add(right,0);
getContentPane().add(up,0);
getContentPane().add(down,0);
timerl=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX()-10,movingthing.getY());
}
});
timerr=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX()+10,movingthing.getY());
}
});
timerup=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX(),movingthing.getY()-10);
}
});
timerd=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX(),movingthing.getY()+10);
}
});
left.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerl.start();
}
public void mouseReleased(MouseEvent e) {
timerl.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
down.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerd.start();
}
public void mouseReleased(MouseEvent e) {
timerd.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
up.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerup.start();
}
public void mouseReleased(MouseEvent e) {
timerup.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
right.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerr.start();
}
public void mouseReleased(MouseEvent e) {
timerr.stop();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e) {
}
} );
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Move t=new Move();
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
class Move extends JFrame
{
JButton left=new JButton("Left"),right=new JButton("Right");
JButton up=new JButton("up"),down=new JButton("down");
JComponent movingthing;
JLayeredPane pane=new JLayeredPane();
JLayeredPane frame=new JLayeredPane();
Timer timerl,timerr,timerd,timerup;
JPanel jp;
public Mover()
{
this.setSize(400,400);
this.setResizable(true);
setContentPane(pane);
movingthing=new JComponent() {
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fill3DRect((movingthing.getWidth()/2)-25, (movingthing.getHeight()/2)-25, 50, 50,true);
}};
left.setBounds((getWidth()/2)-140,getHeight()-50,100,20);
right.setBounds((getWidth()/2)+2,getHeight()-50,100,20);
up.setBounds((getWidth()/2)-80,getHeight()-90,100,20);
down.setBounds((getWidth()/2)-80,getHeight()-20,100,20);
movingthing.setBounds((getWidth()/2)-25,getY()+10,50,50);
getContentPane().add(movingthing,0);
getContentPane().add(left,0);
getContentPane().add(right,0);
getContentPane().add(up,0);
getContentPane().add(down,0);
timerl=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX()-10,movingthing.getY());
}
});
timerr=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX()+10,movingthing.getY());
}
});
timerup=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX(),movingthing.getY()-10);
}
});
timerd=new Timer(50,new ActionListener() {
public void actionPerformed(ActionEvent e) {
movingthing.setLocation(movingthing.getX(),movingthing.getY()+10);
}
});
left.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerl.start();
}
public void mouseReleased(MouseEvent e) {
timerl.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
down.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerd.start();
}
public void mouseReleased(MouseEvent e) {
timerd.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
up.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerup.start();
}
public void mouseReleased(MouseEvent e) {
timerup.stop();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
right.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
timerr.start();
}
public void mouseReleased(MouseEvent e) {
timerr.stop();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e) {
}
} );
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Move t=new Move();
}
}
No comments:
Post a Comment