LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java swing: previous paintings disappear (https://www.linuxquestions.org/questions/programming-9/java-swing-previous-paintings-disappear-423843/)

kpachopoulos 03-11-2006 11:39 AM

java swing: previous paintings disappear
 
Hi,
an oval is created in graphArea (a JPanel) on mouseClicked. However, as soon as the mouse is clicked for the second (n-th) time, the first (previous) oval disappears and another oval appears on the new location. How can i keep the old the new ovals, too?
In general, how can i keep the old painted components and the new ones? (It has probably something to do with the repaint(), but i don't know what...)
A hint?

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;

public class MainPanel extends JFrame
{   
    /*  To make a local variable available to an 
    *  inner class, just save a copy of the variable as a
    *  final local variable.
    */
   
    int clickedX;
    int clickedY;
    JPanel graphArea; 
    JPanel outPanel;
    JPanel inPanel;
   
    public MainPanel()
    {
        //setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(new Dimension(800, 600));       
        setLayout(new BorderLayout());
       
        /*  setting up the area of the graph    */
        graphArea=new JPanel()
        {
            public void paintComponent(Graphics g)
            {
                g.drawOval(clickedX,clickedY, 25,25);
                g.drawString("a",clickedX+13, clickedY+13);
            }
        }; 
       
        graphArea.addMouseListener(new MouseAdapter()
        {
          public void mouseClicked (MouseEvent e)
          {
                clickedY=e.getY();
                clickedX=e.getX();
                System.out.println("Mouse clicked X: "+clickedX+" Y:"+clickedY);         
                repaint();     
          }
        });
        graphArea.setBorder(BorderFactory.createTitledBorder("graphArea"));
        graphArea.setPreferredSize(new Dimension(640, 340));       
        getContentPane().add(graphArea, BorderLayout.WEST);
       
        /* setting up the area of the outcoming connections */
        outPanel=new JPanel();
        outPanel.setBackground(Color.LIGHT_GRAY);
        addCheckBox(outPanel);
        outPanel.setBorder(BorderFactory.createTitledBorder("Outcoming Connections"));
        outPanel.setPreferredSize(new Dimension(120, 200));       
        getContentPane().add(outPanel,BorderLayout.PAGE_END);
       
       
        /* setting up the area of the incoming connections */
        inPanel=new JPanel();
        inPanel.setBackground(Color.LIGHT_GRAY);
        addCheckBox(inPanel);
        inPanel.setBorder(BorderFactory.createTitledBorder("Incoming Connections"));
        inPanel.setPreferredSize(new Dimension(160, 240));       
        getContentPane().add(inPanel,BorderLayout.LINE_END);
       
        setVisible(true);
    }   
   
    /*  provided, that num is even  */
    void addCheckBox(JPanel pane)
    {
        int j=0;
        for (int i=0; i<4; i++)
        {
            pane.add(new JCheckBox(Integer.toString(++j)));           
            pane.add(new JCheckBox(Integer.toString(++j)));
        }
    }

    public static void main(String[] args)
    {
        new MainPanel();
    }
   
}


kpachopoulos 03-11-2006 12:31 PM

Here it is...
Method repaint() clears the background. However, method updateUI() of JPanel, must be implemented on method update() of JComponent, which doesn't clear the background.

Code:

  graphArea.addMouseListener(new MouseAdapter()
        {
          public void mouseClicked (MouseEvent e)
          {
                clickedY=e.getY();
                clickedX=e.getX();
                System.out.println("Mouse clicked X: "+clickedX+" Y:"+clickedY);         
                graphArea.updateUI();     
          }
        });


paulsm4 03-11-2006 12:39 PM

The only sure way to add multiple items to be displayed - and then to make sure they're all displayed each and every time the panel is refreshed (expose event, minimize/maximize, resize, etc) is:

a) Make a list of your graphics entities (an array, one or another Java container - etc)
b) Iterate through that list in your "paint()" method

Mega Man X 03-11-2006 01:47 PM

I did not understand the whole question, but I wrote this and it will keep the previous Oval thing. Is this what you were looking for?

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DrawOval extends JFrame {

    private static int x, y;

    private JPanel pane = new JPanel();

    public DrawOval() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);
               
               
        pane.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                x = e.getX();
                y = e.getY();
                System.out.println(e.toString());
            }
        });
               
        getContentPane().add(pane);
               
        setVisible(true);
               
    }
       
    public static void main(String[] args) {
        new DrawOval();
    }

    public void paint (Graphics g) {
        super.repaint();
        g.setColor(Color.BLACK);
        g.drawOval(x, y, 30, 30);

    }

}


kpachopoulos 03-12-2006 03:24 AM

Hi Mega Man X,
you understood right. The repaint() does indirectly exactly what updateUI() and update() does. From the API:
Quote:

If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.


All times are GMT -5. The time now is 10:34 PM.