Question

I have a problem with Paint- Repaint-Mechanism in Swing java.:

i want to create GraphicEditor, which can create a rectangle and shape with the Mouse.

The application should look like this

http://picload.org/view/lrdwpad/ghh.png.html

My Code Look like this :

public class MiniGrafikEditor extends JFrame implements ActionListener {
    private Vector rectList;

    private Rectangle currentRect;
    private Color color = Color.green;

    private static int v = 0 ;
    JPanel bp;

    public static void main(String[] args) {
        MiniGrafikEditor wnd = new MiniGrafikEditor();
    }

    public MiniGrafikEditor() {
        super("Rechtecke zeichnen");

        rectList = new Vector();
        currentRect = new Rectangle(0, 0, 0, 0);

        setLayout(new BorderLayout());

        addWindowListener(new MyWindowListener());
        addMouseListener(new MyMouseListener());
        addMouseMotionListener(new MyMouseMotionListener());

        bp = new JPanel();
        bp.setBackground(Color.gray);
        add("North", bp);

        JRadioButton b = null;
        bp.add(b = new JRadioButton("Rechteck"));
        b.addActionListener(this);
        bp.add(b = new JRadioButton("kreis"));
        b.addActionListener(this);
        bp.add(b = new JRadioButton("Standard"));
        b.addActionListener(this);

        setLocation(200, 200);
        setSize(400, 300);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        String label = ((JRadioButton) e.getSource()).getLabel();
        bp.repaint();

        if (label.equals("Rechteck")) {

            v=1;
            bp.repaint();
        }
        if (label.equals("Blue")) {
            color = Color.blue;
            bp.repaint();
        }
        Graphics g = getGraphics();
        drawRects(g);
    }

    public void drawRects(Graphics g) {
        Rectangle r;
        Enumeration e;

        g.clearRect(0, 0, getSize().width, getSize().height);
        g.setColor(color);

        for (e = rectList.elements(); e.hasMoreElements();) {
            r = (Rectangle) e.nextElement();
            g.drawRect(r.x, r.y, r.width, r.height);
        }

        if (currentRect != null && (currentRect.x > 0 || currentRect.y > 0)) {
            g.drawRect(currentRect.x, currentRect.y, currentRect.width,
                    currentRect.height);

        }
        bp.repaint();
    }

    class MyMouseListener extends MouseAdapter {
        public void mousePressed(MouseEvent event) {
            bp.repaint();
            if(v==1){
            currentRect = new Rectangle(event.getX(), event.getY(), 0, 0);
            }

        }

        public void mouseReleased(MouseEvent event) {

            if(v==1){

            if (currentRect.width > 0 || currentRect.height > 0) {
                rectList.addElement(currentRect);
                currentRect = null;
            }
            Graphics g = getGraphics();
            drawRects(g);
            }
            bp.repaint();
        }
    }

    class MyMouseMotionListener extends MouseMotionAdapter {

        public void mouseDragged(MouseEvent event) {

            if(v==1){
            int x = event.getX();
            int y = event.getY();
            if (x > currentRect.x && y > currentRect.y) {
                currentRect.width = x - currentRect.x;
                currentRect.height = y - currentRect.y;
            }

            Graphics g = getGraphics();
            drawRects(g);

            }

        }
    }

    class MyWindowListener extends WindowAdapter {  
        public void windowClosing(WindowEvent event) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    }
}

when i run the application its look like this:

http://picload.org/view/lrdwpac/unbenannt.png.html

When i try to draw a rectangle , i can see when the Jpanel repaint it self.

how can i repaint the Jpanel and i cant see it, when it repaint it self.

thx a lot for your Help.

Was it helpful?

Solution

You have to write an extension of JPanel that overrides paint(Graphics g) or even better paintComponent(Graphics g). Under the overridden version of the method you will have to call your drawRects method.

OTHER TIPS

See Custom Painting Approaches for examples of how to do custom painting using:

  1. A List of objects to paint
  2. a BufferedImage to paint the objects.

The examples will paint rectangles of different colors. So you will need to add code to paint different shapes. But the idea is to understand the basic painting concepts first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top