• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Java Tutorial. It has a chapter on Swing which describes how to write event listeners.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic