I have a pretty simple JFrame (it consists only of one text field and a lot of stuff I paint), though whenever repaint() is called the text field changes. I'm pretty sure it's repaint(), as it happens even when I drag the frame from one monitor to the next as well as whenever I call it in the code.
It starts off fine when I run the program:
However, whenever repaint() is called, this happens:
If I start typing in the field, the rest immediately pops back up and works fine. Ultimately, my end goal is to reset a large portion of my frame to what is painted in paintComponent() while still having the text field visible.
I'm relatively new to Graphics and painting, any help is greatly appreciated!
EDIT
In creating the SSCCE, I discovered the problem with the text field was being caused by the Graphics2D method rotate that was in my paintComponent method. I don't know why it's happening, but I can work around it and consider that problem solved.
Now, I'm having a new problem: my paintComponent method is being called one too many times. Below is a SSCCE of this problem, the order I want things to happen is:
- Call to paintComponent()
- (when button is pressed) Call to repaint()
- (when button is pressed, but after repaint) Call to paintStuff()
That all happens, however afterwards paintComponent is somehow being called, erasing everything painted by paintStuff().
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class tester extends JFrame {
private JPanel contentPane;
private JButton button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tester frame = new tester();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public tester() {
setBounds(100, 100, 450, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel(){
public void paintComponent(Graphics g1) {
System.out.println("draw rectangle - should be first");
super.paintComponent(g1);
g1.drawRect(50,50,50,50);
}
};
contentPane.setLayout(null);
setContentPane(contentPane);
button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("repaint - should be second");
repaint();
paintStuff(contentPane.getGraphics());
}
});
button.setBounds(10, 11, 95, 20);
contentPane.add(button);
}
public void paintStuff(Graphics g){
System.out.println("draw circle - should be last");
g.drawOval(100,100,10,10);
}
}