Si lo so il titolo è molto vago ma per fortuna il codice da mostrarvi è molto semplice.
date un occhiata:

classe del frame pricipale:

codice:
import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.Border; @SuppressWarnings("serial") public class FramePrincipale extends JFrame { private JPanel pannello1; private DrawPanel pannello2; private JButton bottone1, bottone2,bottone3; private Border bordo; private Container cp; private int frameWidth; private int frameHeight; private final GridBagLayout GBL; private final JFrame frame; public FramePrincipale() { this.setTitle("Esempio eventi associati ai pulsanti"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(740, 600); cp = this.getContentPane(); frame = this; GBL = new GridBagLayout(); this.getContentPane().setLayout(GBL); this.getContentPane().setBackground(Color.GRAY); pannello1 = new JPanel(); pannello2 = new DrawPanel(); bottone1 = new JButton("Disegna Posto"); bottone2 = new JButton("Disegna Transizione"); bottone3 = new JButton("Disegna Arco"); pannello1.setLayout(new FlowLayout(FlowLayout.LEFT)); pannello1.setBackground(Color.GRAY); pannello1.add(bottone1); pannello1.add(bottone2); pannello1.add(bottone3); bordo = BorderFactory.createLineBorder(Color.black, 3); pannello2.setBorder(bordo); this.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { cp.setVisible(false); frameWidth = frame.getSize().width; frameHeight = frame.getSize().height; Dimension D2 = new Dimension((frameWidth-40), (frameHeight-120)); pannello2.setPreferredSize(D2); cp.setVisible(true); } }); GridBagConstraints gbc1 = new GridBagConstraints(); gbc1.fill=GridBagConstraints.HORIZONTAL; cp.add(pannello1); gbc1.gridx=0; gbc1.gridy=0; GBL.setConstraints(pannello1,gbc1); GridBagConstraints gbc2 = new GridBagConstraints(); gbc2.fill=GridBagConstraints.HORIZONTAL; cp.add(pannello2); gbc2.gridx=0; gbc2.gridy=1; GBL.setConstraints(pannello2,gbc2); } public static void main(String[] args) { FramePrincipale pal1 = new FramePrincipale(); pal1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pal1.setVisible(true); } }

classe del mio custom component:

codice:
import java.awt.*; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; @SuppressWarnings("serial") public class Place extends JComponent { private int posx; private int posy; private int height = 20; private int width = 20; public Place(int x,int y) { posx=x; posy=y; } public int getposx() { return posx; } public int getposy() { return posy; } public int getW() { return width; } public int getH() { return height; } public void paintPlace(Graphics g) { Graphics2D g2d = (Graphics2D)g; Ellipse2D.Double ell = new Ellipse2D.Double(posx, posy, height, width); g2d.draw(ell); } }
classe del mio pannello di disegno:

codice:
import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JPanel; @SuppressWarnings("serial") public class DrawPanel extends JPanel { private int c; private Boolean draw = false; private Place[] placesVector = new Place[100]; //private Place rec; public DrawPanel() { setBackground(Color.WHITE); c = 0; addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub super.mousePressed(e); System.out.println("asdasd"); draw = true; placesVector[c] = new Place(e.getX(), e.getY()); repaint(placesVector[c].getposx(),placesVector[c].getposy(),placesVector[c].getW(), placesVector[c].getH()); } }); } public void drawGrid() { } public void paintComponent(Graphics g) { super.paintComponent(g); if(draw.equals(true)) { placesVector[c].paintPlace(g); System.out.println("posto numero: " + c); System.out.println("posx = " + placesVector[c].getposx()); c = c + 1; } } }

il mio problema è dupplice: 1-vengono disegnate mali le ellissi(anche se questo non accade se disegno delle singole istanze no create dinamicamente); 2 - con il resize del frame si verifica il seguente errore(l'errore si verifica solo se prima ho disegnato):Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Tesi.DrawPanel.paintComponent(DrawPanel.java:54)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source) ecc ecc

Non riesco a capire come un ridimensionamento possa rendere il mio Array di Componenti NULLO. Se qualcuno può aiutarmi lo ringrazio in anticipo.