Ridefinire paintComponent() è corretto.
Ma non sarebbe più semplice fare così?
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; class MiddlePanel extends JPanel { MiddlePanel() { super(new BorderLayout()); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int width = getWidth(); int height = getHeight(); for(int i=0; i<width; i+=20) { g.drawLine(i,0,i,height); g.drawLine(0,i,width,i); } } } class FrameExample extends JFrame { private MiddlePanel mp; FrameExample() { mp = new MiddlePanel(); add(mp); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void makeGUI() { FrameExample fe = new FrameExample(); fe.setSize(800,600); fe.setVisible(true); } public static void main(String[] args) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { makeGUI(); } }); } catch(Exception e) {} } }


Rispondi quotando