import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.StringTokenizer;
public class Duke extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private Image man;
private Graphics g;
private JLabel message, CommandLabel;
private JTextField Command;
private int x,y,y1,x1;
public Duke()
{
super (" Double O Duxe ");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
message = new JLabel("Duke's Travel");
message.setFont(new Font("Arial",Font.BOLD,50));
c.add(message);
c.setLayout(new FlowLayout());
setSize(600,600);
panel = new JPanel();
c.add(panel);
panel.setPreferredSize(new Dimension(500,500));
panel.setBackground(Color.yellow);
CommandLabel = new JLabel ("Command Line");
c.add(CommandLabel);
Command = new JTextField(25);
c.add(Command);
Command.addActionListener (this);
man = Toolkit.getDefaultToolkit().getImage("G:\\man.gif");
show();
g = panel.getGraphics();
g.drawImage(man,200,200,100,100,this);
x = 200;
y = 200;
g.setColor(Color.YELLOW);
x1 = 200;
y1 = 200;
}
public static void main(String[] args)
{
Duke myParsing = new Duke();
}
public void actionPerformed(ActionEvent arg0)
{
String distance;
String Token;
int length;
String text = Command.getText();
StringTokenizer words = new StringTokenizer(text);
while (words.hasMoreTokens())
{
Token = words.nextToken();
if(Token.equals("show"))
{
g.drawImage(man,x,y,100,100,this);
}
if (Token.equalsIgnoreCase("Help"))
{
JOptionPane.showMessageDialog(null,"Welcome to dukes travels\n Up\n Down\n Left\n Right\n");
}
if(Token.equals("up"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
y = y- length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("down"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
y = y+ length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("left"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
x = x1 - length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("right"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
x = x1 + length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("pendown")
g.drawLine(50,250,50,50);
}
Command.setText("");
}
}
Take a look for me please , basically so far all i need is a little picture to move about i.e. up 100 and it moves 100 places does this look ok so far? i need to do a pendown and up command next to make a line follow him anyidea how to achieve this?
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.StringTokenizer;
public class Duke extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private Image man;
private Graphics g;
private JLabel message, CommandLabel;
private JTextField Command;
private int x,y,y1,x1;
public Duke()
{
super (" Double O Duxe ");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
message = new JLabel("Duke's Travel");
message.setFont(new Font("Arial",Font.BOLD,50));
c.add(message);
c.setLayout(new FlowLayout());
setSize(600,600);
panel = new JPanel();
c.add(panel);
panel.setPreferredSize(new Dimension(500,500));
panel.setBackground(Color.yellow);
CommandLabel = new JLabel ("Command Line");
c.add(CommandLabel);
Command = new JTextField(25);
c.add(Command);
Command.addActionListener (this);
man = Toolkit.getDefaultToolkit().getImage("G:\\man.gif");
show();
g = panel.getGraphics();
g.drawImage(man,200,200,100,100,this);
x = 200;
y = 200;
g.setColor(Color.YELLOW);
x1 = 200;
y1 = 200;
}
public static void main(String[] args)
{
Duke myParsing = new Duke();
}
public void actionPerformed(ActionEvent arg0)
{
String distance;
String Token;
int length;
String text = Command.getText();
StringTokenizer words = new StringTokenizer(text);
while (words.hasMoreTokens())
{
Token = words.nextToken();
if(Token.equals("show"))
{
g.drawImage(man,x,y,100,100,this);
}
if (Token.equalsIgnoreCase("Help"))
{
JOptionPane.showMessageDialog(null,"Welcome to dukes travels\n Up\n Down\n Left\n Right\n");
}
if(Token.equals("up"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
y = y- length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("down"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
y = y+ length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("left"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
x = x1 - length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("right"))
{
g.setColor(Color.YELLOW);
g.fillRect(x,y,100,100);
distance = words.nextToken();
length = Integer.parseInt(distance);
x = x1 + length;
g.setColor(Color.yellow);
g.drawImage(man,x,y,100,100,this);
}
if(Token.equals("pendown")
g.drawLine(50,250,50,50);
}
Command.setText("");
}
}
Take a look for me please , basically so far all i need is a little picture to move about i.e. up 100 and it moves 100 places does this look ok so far? i need to do a pendown and up command next to make a line follow him anyidea how to achieve this?