Come posso disegnare un object senza una class (che estende JFrame
)? Ho trovato il metodo getGraphics
ma non disegna l’object.
import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel(); frame.add(panel); Graphics g = panel.getGraphics(); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }
Se si desidera modificare il modo in cui viene disegnato il componente (si aggiungono rettangoli), è necessario ridefinire paintComponent()
in quel componente. Nel tuo codice, stai usando getGraphics()
.
Non dovresti chiamare getGraphics()
su un componente. Ogni dipinto che fai (alla Graphics
restituita) sarà temporaneo e andrà perso la volta successiva che Swing determina che un componente deve essere ridipinto.
Invece, è necessario sovrascrivere il paintComponent(Graphics)
(di JComponent
o JPanel
) e eseguire la pittura in questo metodo, utilizzando l’object Graphics
ricevuto come argomento.
Controlla questo link per ulteriori letture.
Di seguito è riportato il tuo codice, corretto.
import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }; frame.add(panel); // Graphics g = panel.getGraphics(); // g.setColor(Color.BLUE); // g.fillRect(0, 0, 100, 100); frame.validate(); // because you added panel after setVisible was called frame.repaint(); // because you added panel after setVisible was called } }
Un’altra versione, fa la stessa identica cosa , ma potrebbe essere più chiara per te:
import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new MyRectangleJPanel(); // changed this line frame.add(panel); // Graphics g = panel.getGraphics(); // g.setColor(Color.BLUE); // g.fillRect(0, 0, 100, 100); frame.validate(); // because you added panel after setVisible was called frame.repaint(); // because you added panel after setVisible was called } } /* A JPanel that overrides the paintComponent() method and draws a rectangle */ class MyRectangleJPanel extends JPanel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }
Devi sovrascrivere il metodo paintComponent nella class JPanel. Quindi dovresti creare una class che estenda JPanel e sovrascriva il metodo paintComponent nella sottoclass
Perché non usare solo un’istanza di java.awt.image.BufferedImage
? per esempio
BufferedImage output = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = output.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillRect(0, 0, output.getWidth(), output.getHeight()); g2.setColor(Color.BLUE); g2.fillRect(0, 0, 100, 100); JOptionPane.showMessageDialog(null, new ImageIcon(output));