PDA

View Full Version : Need help with small assignment pleaz...



Smirre
01-06-2009, 05:15 PM
Hi all,
I please need help with this question please:

"Write an application that draws an ellipse. It must accept two
parameters that are the sizes of the two axes in pixels.
Center the ellipse in the application"

Here is the code that I've tried so far:


public class EllipseApp extends JComponent implements ActionListener{

JFrame frame = new JFrame();
JTextField field1 = new JTextField("Width", 5);
JTextField field2 = new JTextField("Height", 5);
JButton button = new JButton("OK");

public EllipseApp() {
super();
frame.setTitle("Ellipse");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
button.addActionListener(this);
JPanel pane = new JPanel();
pane.add(field1);
pane.add(field2);
pane.add(button);
frame.add(pane);
setVisible(true);
}

public void paintComponent(Graphics2D g, float a, float b) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Float ellipse = new Ellipse2D.Float(0, 0, a, b);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(ellipse);
g2.fill(ellipse);
g2.setColor(Color.blue);
}

public static void main(String[] args) {
EllipseApp e = new EllipseApp();
JPanel panel = new JPanel();
panel.add(e);
panel.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == button) {

}

}


}


My program doesn't want to work and I do not know what to do further,
can anyone please help me with this,
I need your help.
Thanks in advance

Twey
01-07-2009, 02:26 AM
Of course it 'doesn't want to work': you've missed a whole bunch of closing braces.

Hint: it would be easier to accept the parameters as actual command-line parameters (that's what I think of when someone says 'parameters to an application', anyway).

imaginationac
01-28-2009, 07:19 PM
I'm assuming you've got all import statements you need, thought it might be helpful to include them in the code snippet next time.

:confused: I don't see any missing braces.

I was able compile and run the program just fine. Of course it doesn't do anything yet. I assuming you've yet to complete the code for the actionPerformed method.