简体   繁体   中英

Java - Graphics - adding another shape on JPanel

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 ?

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.

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 .

图片

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM