ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
}
}
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.