Thanks, Pah for the advice. I can get the output working. Now, I want to implement an Actionlistener for the "moveButton" feature. However, though, when I modified the code and compiled, I get an error stating that there's an "Illegal start of expression" from the paintComponent method. Can anybody help me on this one? Here's the modified program:
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class AppletViewer
{
public static void main(String[] args)
{
Rectangle box = new Rectangle(10,20,30,40);
JFrame frame = new JFrame();
final int FIELD_WIDTH = 5;
final JTextField xField = new JTextField(FIELD_WIDTH);
final JTextField yField = new JTextField(FIELD_WIDTH);
JButton moveButton = new JButton("Move");
JLabel l1 = new JLabel("X =");
JLabel l2 = new JLabel("Y =");
JPanel somePanel = new JPanel();
somePanel.add(l1);
somePanel.add(xField);
somePanel.add(l2);
somePanel.add(yField);
somePanel.add(moveButton);
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int x = Integer.parseInt(xField.getText());
int y = Integer.parseInt(yField.getText());
box.setLocation(x,y);
repaint();
}
}
ActionListener listener = new MyListener();
moveButton.addActionListener(listener);
public void paintComponent(Graphics g)// <---- error: Illegal start of expression
{
super.paintComponent(g);
g.draw(box);
}
frame.getContentPane().add(somePanel);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static final int FRAME_WIDTH = 900;
private static final int FRAME_HEIGHT = 500;
}
thanks,
DFC