Hi All,
I have a doubt in paint method of the Component Class. I know how to use that in Applet but im finding it hard to use it in GUI app. Im posting my code here, please edit it to make the program use paint method to render any GUI component. Be it any Component, any Rendering, i just want a push up on how to use that method.
Thanks
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
public class MetricConversion extends JFrame{
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final double KILOMETER = 1.60;
private static final double KILOGRAM = 0.45;
private static final double GALLONS = 3.78;
private static final double FAHRENHEIT = 1.8;
private static final double FREEZINGPOINT = 32.0;
private JTextField jText1;
private JTextField jText2;
private JTextField jText3;
private JTextField jText4;
private JTextField jText5;
private JTextField jText6;
private JTextField jText7;
private JTextField jText8;
public MetricConversion() {
// TODO Auto-generated constructor stub
super("Metric Conversion");
super.setSize(WIDTH, HEIGHT);
super.setVisible(true);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container reference = super.getContentPane();
reference.setLayout(new GridLayout(4,4));
JLabel jMiles = new JLabel("MILES",SwingConstants.CENTER);
JLabel jKilometer = new JLabel("Kilometer",SwingConstants.CENTER);
JLabel jPounds = new JLabel("POUNDS",SwingConstants.CENTER);
JLabel jKilogram = new JLabel("KILOGRAM",SwingConstants.CENTER);
JLabel jGallons = new JLabel("GALLONS",SwingConstants.CENTER);
JLabel jLitres = new JLabel("LITRES",SwingConstants.CENTER);
JLabel jFahrenheit = new JLabel("Fahrenheit",SwingConstants.CENTER);
JLabel jCentigrade = new JLabel("CENTIGRADE",SwingConstants.CENTER);
jText1 = new JTextField();
jText1.addActionListener(new computeKilometer());
jText2 = new JTextField() ;
jText2.addActionListener(new computeMiles());
jText3 = new JTextField() ;
jText3.addActionListener(new computeKilogram());
jText4 = new JTextField() ;
jText4.addActionListener(new computePounds());
jText5 = new JTextField() ;
jText5.addActionListener(new computeLitres());
jText6 = new JTextField() ;
jText6.addActionListener(new computeGallons());
jText7 = new JTextField() ;
jText7.addActionListener(new computeCentigrade());
jText8 = new JTextField() ;
jText8.addActionListener(new computeFahrenheit());
reference.add(jMiles);
reference.add(jText1);
reference.add(jKilometer);
reference.add(jText2);
reference.add(jPounds);
reference.add(jText3);
reference.add(jKilogram);
reference.add(jText4);
reference.add(jGallons);
reference.add(jText5);
reference.add(jLitres);
reference.add(jText6);
reference.add(jFahrenheit);
reference.add(jText7);
reference.add(jCentigrade);
reference.add(jText8);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MetricConversion converter = new MetricConversion();
}
private class computeKilometer implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText1.getText());
b = a * KILOMETER ;
jText2.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeMiles implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText2.getText());
b = a/KILOMETER;
jText1.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeKilogram implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText3.getText());
b = a * KILOGRAM ;
jText4.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computePounds implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText4.getText());
b = a / KILOGRAM ;
jText3.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeLitres implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText5.getText());
b = a * GALLONS ;
jText6.setText(""+ fourPlaces.format(b));
// throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeGallons implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText6.getText());
b = a / GALLONS ;
jText5.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeCentigrade implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText7.getText());
b = (a - FREEZINGPOINT) * FAHRENHEIT;
jText8.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
private class computeFahrenheit implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
double a,b;
DecimalFormat fourPlaces = new DecimalFormat("0.0000");
a = Double.parseDouble(jText8.getText());
// b = a * FAHRENHEIT + FREEZINGPOINT;
b = a * FAHRENHEIT +FREEZINGPOINT;
jText7.setText(""+ fourPlaces.format(b));
//throw new UnsupportedOperationException("Not supported yet.");
}
}
}