Paint in JFrame not working (Java)

So, in the class we create a program in Java. We are trying to use the paint (Graphics g) function in a JFrame. We tried this in the past (a few weeks ago) and it worked. But now this is not so (or we made a mistake somewhere). We also tried using paintComponent (Graphics g), but nothing works. Here is our code:

public class MainAc { public static void main(String[] args) { JFrame frame = new JFrame("Class Paint"); JButton button = new JButton("Click for more"); frame.setSize(800, 600); frame.add(button); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button.setLayout(null); button.setLocation(100,100); button.setSize(200,100); frame.setVisible(true); } public void paint(Graphics g){ g.drawString("Hello", 200, 50); } } 
+4
source share
4 answers

So what you do there, implements the paint method inside your own MainAc class, not the JFrame .

Your MainAc class should be inferred from a JFrame .

The code below should work. If you do not understand inheritance, you should look at your notes about the class, it will be there.

 public class MainAc extends JFrame { public static void main(String[] args) { new MainAc(); } public MainAc() { super("Class Paint"); JButton button = new JButton("Click for more"); setSize(800, 600); add(button); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button.setLayout(null); button.setLocation(100,100); button.setSize(200,100); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.drawString("Hello", 200, 50); } } 
+4
source

Your class does not extend to any Component capable of drawing

Read Performing Custom Painting for More Ideas

You can do something like:

enter image description here

 public class SimplePaint { public static void main(String[] args) { new SimplePaint(); } public SimplePaint() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new PaintPane()); frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } protected class PaintPane extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); String text = "Look ma, no hands"; FontMetrics fm = g2d.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.drawString(text, x, y); g2d.dispose(); } } } 

Whenever possible, you should avoid overriding the methods of the top-level container of paint , if not for any other reason, they are not duplicated by the buffer.

You should also try and expand Swing components based on the same reason, as mixing heavy and light components can cause painting problems.

+9
source

As code, you simply define the paint method in the class. The extends Object class, so you are not overridding paint on JComponent .

Your class must extend any subclass of Component or Component itself to actually override paint or paintComponent . In your case, MainAc will expand on JFrame .

+4
source

Here is a simplified version of sticky code that seems to work. I changed it to a JApplet extension and used the init() method instead of main() .

 public class JavaApplication51 extends JApplet { public void something() { JButton button = new JButton("Click for more"); add(button); setLayout(null); button.setLayout(null); button.setLocation(100,100); button.setSize(101,20); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.drawString("Hello", 200, 50); } public void init() { something(); } } 
-1
source

Source: https://habr.com/ru/post/1442952/


All Articles