LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2006, 11:39 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
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();
    }
    
}
 
Old 03-11-2006, 12:31 PM   #2
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
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();      
           }
        });
 
Old 03-11-2006, 12:39 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-11-2006, 01:47 PM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
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);

    }

}

Last edited by Mega Man X; 03-11-2006 at 01:57 PM.
 
Old 03-12-2006, 03:24 AM   #5
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a java-swing problem... shosh Programming 2 06-26-2009 05:38 AM
java swing JTextArea gauravbagga Programming 1 05-23-2005 04:34 AM
Clueless and frustrated... RPM has made previous installation disappear rollo Linux - Newbie 7 10-27-2004 04:01 PM
Java help! Using Swing and Layouts Mega Man X Programming 7 02-05-2004 05:02 PM
missing java swing libraries? a1ghagh05t Programming 5 01-26-2004 06:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration