wood burning stoves*
The moose likes Applets and the fly likes Need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Need help" Watch "Need help" New topic
Author

Need help

Bashar Ayyash
Ranch Hand

Joined: Jun 24, 2004
Posts: 44
Hi guys I need help with the following applet:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Draw1 extends JApplet {
JTextField field1;
JTextField field2;
JButton rectButton;
JButton ovalButton;
JButton clrButton;
Graphics gr;

//initializes applet
public void init(){
JFrame frame1 = new JFrame("Ovals & Rectangles");
Container c = frame1.getContentPane();
c.setLayout(new BorderLayout());

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
frame1.setBounds(width/4, height/4, width/2, height/4);

JLabel label1 = new JLabel("Height ");
JLabel label2 = new JLabel("Width ");
field1 = new JTextField(15);
field2 = new JTextField(15);

JPanel panel11 = new JPanel();
panel11.setLayout(new FlowLayout());
panel11.add(label1);
panel11.add(field1);

JPanel panel12 = new JPanel();
panel12.setLayout(new FlowLayout());
panel12.add(label2);
panel12.add(field2);

JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,1));
panel1.add(panel11);
panel1.add(panel12);

rectButton = new JButton("Rectangle");
ovalButton = new JButton("Oval");
clrButton = new JButton("Clear");
clrButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
field1.setText("");
field2.setText("");
}
}
);//end call to addActionListener method

JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout());
buttonsPanel.add(rectButton);
buttonsPanel.add(new JLabel(" "));
buttonsPanel.add(ovalButton);
buttonsPanel.add(new JLabel(" "));
buttonsPanel.add(clrButton);

c.add(panel1, BorderLayout.NORTH);
c.add(buttonsPanel, BorderLayout.CENTER);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}//end init method

//draw ovals and rectangles on applet background
public void paint(Graphics g){
super.paint(g);
// need help here
}//end paint method

}//end class Draw1

I want to addActionListener for the rectButton and ovalButton to draw Rectangle and oval.

Thanx,
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8877
    
    8

Take a look at the Java Tutorial. It has a chapter on Swing which describes how to write event listeners.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help