1

I've got a class that makes a JFrame and adds a panel on it
and the second one extends the JPanel and paints on it

The first one (JFrame)

class MyWindow {
void qwe() {
 JFrame frame = new JFrame("qwe");
 frame.setVisible(true);
 frame.setLocationRelativeTo(null);
 frame.setSize(300, 200);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 MyPanel panel = new MyPanel();
 panel.setLayout(null);
 frame.add(panel);}}

and the second one (JPanel)

class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
 g.drawRect(50,50,90,70);
}
public void addShape() {
 Graphics g = this.getGraphics();
 Graphics2D gg = (Graphics2D) g;
 gg.drawString("qwe",20,20);}}

how can i add a String on the JPanel by using the addShape() method ?

asked May 31, 2013 at 16:02

2 Answers 2

3

As a concrete example of @camickr's point, note that MyPanel already override's paintComponent(), so you can pass a reference to the Graphics context to addShape(). Additionally,

  • Be sure to invoke super.paintComponent(g).

  • Override getPreferredSize() to establish the component's preferred size.

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyWindow {
 public static void main(String[] args) {
 EventQueue.invokeLater(new Runnable() {
 @Override
 public void run() {
 new MyWindow().qwe();
 }
 });
 }
 void qwe() {
 JFrame frame = new JFrame("qwe");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 MyPanel panel = new MyPanel();
 panel.setLayout(null);
 frame.add(panel);
 frame.pack();
 frame.setLocationRelativeTo(null);
 frame.setVisible(true);
 }
 private static class MyPanel extends JPanel {
 @Override
 public void paintComponent(Graphics g) {
 super.paintComponent(g);
 g.drawRect(50, 50, 90, 70);
 addShape(g);
 }
 public void addShape(Graphics g) {
 g.drawString("qwe", 20, 20);
 }
 @Override
 public Dimension getPreferredSize() {
 return new Dimension(300, 200);
 }
 }
}
answered May 31, 2013 at 18:02

6 Comments

Why does the addShape method need to "get" the 'g' from the paintComponent method ?
That's the one set up to change pixels in the host's peer component. The alternative is to write to the pixels of a BufferedImage and later update the screen.
Well that didn't help me much, but at least now I know what to write to make it work... so thanks
Indeed, it's not very satisfying. At the most basic level, your Java program has to share the host's pixels, which aren't always accessible to your program.
And what should I do, when I want the addShape(Graphics g) method to be ran independently and not everytime I add the MyPanel on a JFrame. Thats what I would want to know the most
|
3

Don't use the getGraphics() method of your component to do custom painting. This type of painting is only temporary and will be lost the next time Swing determines the component needs to be painted.

Custom painting should always be done in the paintComponent() method of your component.

See Custom Painting Approaches for the two commons was to do what you want.

answered May 31, 2013 at 16:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.