Salve ragazzi, ho un problemino, sto studiando il capitolo delle interfacce swing per un esame di java. Per esercitarmi voglio creare un rettangolo ,prendendo in input i valori di altezza e base da delle caselle di testo.Praticamente una volta creato il Frame, ho creato due pannelli, in uno ci sono le caselle di testo,e nell'altro c'è il tasto "ok" a cui ho associato l'azione della creazione del rettangolo al suo interno. Sicuramente faccio un errore banale ma non riesco a capirlo!.Qunado premo il tasto ok per disegnare il rettangolo non mi disegna niente. Se qualcuno riuscisse ad aiutarmi sarei molo felice!Grazie comunque!
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.geom.*;
public class Programsw
{
public static void main(String[] arg)
{
Frame frame= new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
class Frame extends JFrame
{
public Frame()
{
setTitle("FormatTest");
setSize(Width,Heigth);
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(2,2));
add(buttonPanel,BorderLayout.NORTH);
Panel2 grafico = new Panel2();
Container contentPane = getContentPane();
contentPane.add(grafico,BorderLayout.CENTER);
//add(grafico,BorderLayout.CENTER);
}
public static final int Width=500;
public static final int Heigth=250;
}
class Panel extends JPanel
{
public Panel()
{
JLabel label1 = new JLabel("Base");
JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstanc e());
intField.setValue(new Integer(100));
add(label1);
add(intField);
JLabel label2 = new JLabel("Altezza");
JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstanc e());
intField2.setValue(new Integer(200));
add(label2);
add(intField2);
Number valuealtezza = (Number) intField2.getValue();
altezza = valuealtezza.intValue();
Number valuebase = (Number) intField.getValue();
base = valuebase.intValue();
}
public static int altezza,base;
}
class Panel2 extends JPanel
{
static Rectangle2D paintComponent()
{
Rectangle2D rect = new Rectangle2D.Double(Panel.base,Panel.base,Panel.alt ezza,Panel.altezza);
return rect;
}
public Panel2()
{
JButton okButton = new JButton("OK");
ActionListener ok = new okAction();
okButton.addActionListener(ok);
add(okButton);
}
class okAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Rectangle2D mioRect=paintComponent();
//setBackground(Color.yellow);
g.draw(mioRect);
//setPaint(g.draw(mioRect));
}
}
public static Graphics2D g;
}