Repaint() not calling paint() in Java












2















Let me start off by saying I know I've violated some basic Java principles in this messy code, but I'm desperately trying to finish a program by Tuesday for a social science experiment, and I don't know Java, so I'm basically just fumbling through it for now.



With that disclaimer out of the way, I have a separate program working where a circle is moving around the screen and the user must click on it. It works fine when its in its own separate class file, but when I add the code to my main program, it's no longer working. I don't even really understand why repaint() calls my paint() function — as far as I'm concerned, it's magic, but I've noticed that repaint() calls paint() in my test program, but not in the more complicated actual program, and I assume that's why the circle is no longer painting on my program.



Entire code is below:



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

public class Reflexology1 extends JFrame{

private static final long serialVersionUID = -1295261024563143679L;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
private Timer moveBallTimer;
int _ballXpos, _ballYpos;
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");
final JFrame frame2 = new JFrame("The Test");
JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");

public static void main(String args){
new Reflexology1();
}

public Reflexology1(){
frame.setSize(600, 475);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Reflexology 1.0");
frame.setResizable(false);

frame2.setSize(600, 475);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("Reflexology 1.0");
frame2.setResizable(false);

button1 = new JButton("Accept");
button2 = new JButton("Decline");
//movingButton = new JButton("Click Me");

ListenForAcceptButton lForAButton = new ListenForAcceptButton();
ListenForDeclineButton lForDButton = new ListenForDeclineButton();
button1.addActionListener(lForAButton);
button2.addActionListener(lForDButton);
//movingButton.addActionListener(lForMButton);

JTextArea textArea1 = new JTextArea(24, 50);

textArea1.setText("Tracking Eventsn");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setSize(15, 50);
textArea1.setEditable(false);

FileReader reader = null;
try {
reader = new FileReader("EULA.txt");
textArea1.read(reader, "EULA.txt");
} catch (IOException exception) {
System.err.println("Problem loading file");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}

}

JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
AdjustmentListener listener = new MyAdjustmentListener();

thePanel.add(scrollBar1);
thePanel.add(button1);
thePanel.add(button2);

frame.add(thePanel);
ListenForMouse lForMouse = new ListenForMouse();
thePlacebo.addMouseListener(lForMouse);
thePlacebo.add(label1);
frame2.add(thePlacebo);

ListenForWindow lForWindow = new ListenForWindow();
frame.addWindowListener(lForWindow);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
}
});
frame.setVisible(true);

moveBallTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBall();
System.out.println("Timer started!");
repaint();
}
});

addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(frame2.isVisible()){ moveBallTimer.start(); }
}
});

}



private class ListenForAcceptButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button1){
Calendar ClCDateTime = Calendar.getInstance();
System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
_closeTime = ClCDateTime.getTimeInMillis() - _openTime;
//frame.getContentPane().remove(thePanel);
//thePlacebo.addKeyListener(lForKeys);
//frame.getContentPane().add(thePlacebo);

//frame.repaint();
//moveBallTimer.start();
frame.setVisible(false);
frame2.setVisible(true);
frame2.revalidate();
frame2.repaint();


}
}
}


private class ListenForDeclineButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button2){
JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}


private class ListenForWindow implements WindowListener{

public void windowActivated(WindowEvent e) {
//textArea1.append("Window is active");

}

// if this.dispose() is called, this is called:
public void windowClosed(WindowEvent arg0) {

}

// When a window is closed from a menu, this is called:
public void windowClosing(WindowEvent arg0) {

}

// Called when the window is no longer the active window:
public void windowDeactivated(WindowEvent arg0) {
//textArea1.append("Window is NOT active");

}

// Window gone from minimized to normal state
public void windowDeiconified(WindowEvent arg0) {
//textArea1.append("Window is in normal state");

}

// Window has been minimized
public void windowIconified(WindowEvent arg0) {
//textArea1.append("Window is minimized");

}

// Called when the Window is originally created
public void windowOpened(WindowEvent arg0) {
//textArea1.append("Let there be Window!");
Calendar OlCDateTime = Calendar.getInstance();
_openTime = OlCDateTime.getTimeInMillis();
//System.out.println(_openTime);

}

}


private class MyAdjustmentListener implements AdjustmentListener {

public void adjustmentValueChanged(AdjustmentEvent arg0) {
AdjustmentEvent scrollBar1;
//System.out.println(scrollBar1.getValue()));

}

}

public void paint(Graphics g) {
//super.paint(g);
frame2.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
System.out.println("Calling fill()");
}

protected void moveBall() {
//System.out.println("I'm in the moveBall() function!");
int width = getWidth();
int height = getHeight();
int min, max, randomX, randomY;
min =200;
max = -200;
randomX = min + (int)(Math.random() * ((max - min)+1));
randomY = min + (int)(Math.random() * ((max - min)+1));
//System.out.println(randomX + ", " + randomY);

Rectangle ballBounds = ball.getBounds();
//System.out.println(ballBounds.x + ", " + ballBounds.y);
if (ballBounds.x + randomX < 0) {
randomX = 200;
} else if (ballBounds.x + ballBounds.width + randomX > width) {
randomX = -200;
}
if (ballBounds.y + randomY < 0) {
randomY = 200;
} else if (ballBounds.y + ballBounds.height + randomY > height) {
randomY = -200;
}

ballBounds.x += randomX;
ballBounds.y += randomY;
_ballXpos = ballBounds.x;
_ballYpos = ballBounds.y;
ball.setFrame(ballBounds);
}



public void start() {
moveBallTimer.start();
}

public void stop() {
moveBallTimer.stop();
}

private class ListenForMouse implements MouseListener{

// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "n");
if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
System.out.println("TRUE");
}
System.out.println("{e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " / " + "_ballYpos: " + _ballYpos);

}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
// System.out.println("e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos);



// Mouse over
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

// Mouse left the mouseover area:
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}



}


Could anyone tell me what I need to do to get repaint() to call the paint() method in the above program? I'm assuming the multiple frames is causing the problem, but that's just a guess. Thanks.










share|improve this question

























  • Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

    – MadProgrammer
    Oct 28 '12 at 22:16











  • Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

    – allocate
    Oct 28 '12 at 22:43
















2















Let me start off by saying I know I've violated some basic Java principles in this messy code, but I'm desperately trying to finish a program by Tuesday for a social science experiment, and I don't know Java, so I'm basically just fumbling through it for now.



With that disclaimer out of the way, I have a separate program working where a circle is moving around the screen and the user must click on it. It works fine when its in its own separate class file, but when I add the code to my main program, it's no longer working. I don't even really understand why repaint() calls my paint() function — as far as I'm concerned, it's magic, but I've noticed that repaint() calls paint() in my test program, but not in the more complicated actual program, and I assume that's why the circle is no longer painting on my program.



Entire code is below:



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

public class Reflexology1 extends JFrame{

private static final long serialVersionUID = -1295261024563143679L;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
private Timer moveBallTimer;
int _ballXpos, _ballYpos;
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");
final JFrame frame2 = new JFrame("The Test");
JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");

public static void main(String args){
new Reflexology1();
}

public Reflexology1(){
frame.setSize(600, 475);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Reflexology 1.0");
frame.setResizable(false);

frame2.setSize(600, 475);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("Reflexology 1.0");
frame2.setResizable(false);

button1 = new JButton("Accept");
button2 = new JButton("Decline");
//movingButton = new JButton("Click Me");

ListenForAcceptButton lForAButton = new ListenForAcceptButton();
ListenForDeclineButton lForDButton = new ListenForDeclineButton();
button1.addActionListener(lForAButton);
button2.addActionListener(lForDButton);
//movingButton.addActionListener(lForMButton);

JTextArea textArea1 = new JTextArea(24, 50);

textArea1.setText("Tracking Eventsn");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setSize(15, 50);
textArea1.setEditable(false);

FileReader reader = null;
try {
reader = new FileReader("EULA.txt");
textArea1.read(reader, "EULA.txt");
} catch (IOException exception) {
System.err.println("Problem loading file");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}

}

JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
AdjustmentListener listener = new MyAdjustmentListener();

thePanel.add(scrollBar1);
thePanel.add(button1);
thePanel.add(button2);

frame.add(thePanel);
ListenForMouse lForMouse = new ListenForMouse();
thePlacebo.addMouseListener(lForMouse);
thePlacebo.add(label1);
frame2.add(thePlacebo);

ListenForWindow lForWindow = new ListenForWindow();
frame.addWindowListener(lForWindow);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
}
});
frame.setVisible(true);

moveBallTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBall();
System.out.println("Timer started!");
repaint();
}
});

addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(frame2.isVisible()){ moveBallTimer.start(); }
}
});

}



private class ListenForAcceptButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button1){
Calendar ClCDateTime = Calendar.getInstance();
System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
_closeTime = ClCDateTime.getTimeInMillis() - _openTime;
//frame.getContentPane().remove(thePanel);
//thePlacebo.addKeyListener(lForKeys);
//frame.getContentPane().add(thePlacebo);

//frame.repaint();
//moveBallTimer.start();
frame.setVisible(false);
frame2.setVisible(true);
frame2.revalidate();
frame2.repaint();


}
}
}


private class ListenForDeclineButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button2){
JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}


private class ListenForWindow implements WindowListener{

public void windowActivated(WindowEvent e) {
//textArea1.append("Window is active");

}

// if this.dispose() is called, this is called:
public void windowClosed(WindowEvent arg0) {

}

// When a window is closed from a menu, this is called:
public void windowClosing(WindowEvent arg0) {

}

// Called when the window is no longer the active window:
public void windowDeactivated(WindowEvent arg0) {
//textArea1.append("Window is NOT active");

}

// Window gone from minimized to normal state
public void windowDeiconified(WindowEvent arg0) {
//textArea1.append("Window is in normal state");

}

// Window has been minimized
public void windowIconified(WindowEvent arg0) {
//textArea1.append("Window is minimized");

}

// Called when the Window is originally created
public void windowOpened(WindowEvent arg0) {
//textArea1.append("Let there be Window!");
Calendar OlCDateTime = Calendar.getInstance();
_openTime = OlCDateTime.getTimeInMillis();
//System.out.println(_openTime);

}

}


private class MyAdjustmentListener implements AdjustmentListener {

public void adjustmentValueChanged(AdjustmentEvent arg0) {
AdjustmentEvent scrollBar1;
//System.out.println(scrollBar1.getValue()));

}

}

public void paint(Graphics g) {
//super.paint(g);
frame2.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
System.out.println("Calling fill()");
}

protected void moveBall() {
//System.out.println("I'm in the moveBall() function!");
int width = getWidth();
int height = getHeight();
int min, max, randomX, randomY;
min =200;
max = -200;
randomX = min + (int)(Math.random() * ((max - min)+1));
randomY = min + (int)(Math.random() * ((max - min)+1));
//System.out.println(randomX + ", " + randomY);

Rectangle ballBounds = ball.getBounds();
//System.out.println(ballBounds.x + ", " + ballBounds.y);
if (ballBounds.x + randomX < 0) {
randomX = 200;
} else if (ballBounds.x + ballBounds.width + randomX > width) {
randomX = -200;
}
if (ballBounds.y + randomY < 0) {
randomY = 200;
} else if (ballBounds.y + ballBounds.height + randomY > height) {
randomY = -200;
}

ballBounds.x += randomX;
ballBounds.y += randomY;
_ballXpos = ballBounds.x;
_ballYpos = ballBounds.y;
ball.setFrame(ballBounds);
}



public void start() {
moveBallTimer.start();
}

public void stop() {
moveBallTimer.stop();
}

private class ListenForMouse implements MouseListener{

// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "n");
if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
System.out.println("TRUE");
}
System.out.println("{e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " / " + "_ballYpos: " + _ballYpos);

}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
// System.out.println("e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos);



// Mouse over
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

// Mouse left the mouseover area:
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}



}


Could anyone tell me what I need to do to get repaint() to call the paint() method in the above program? I'm assuming the multiple frames is causing the problem, but that's just a guess. Thanks.










share|improve this question

























  • Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

    – MadProgrammer
    Oct 28 '12 at 22:16











  • Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

    – allocate
    Oct 28 '12 at 22:43














2












2








2








Let me start off by saying I know I've violated some basic Java principles in this messy code, but I'm desperately trying to finish a program by Tuesday for a social science experiment, and I don't know Java, so I'm basically just fumbling through it for now.



With that disclaimer out of the way, I have a separate program working where a circle is moving around the screen and the user must click on it. It works fine when its in its own separate class file, but when I add the code to my main program, it's no longer working. I don't even really understand why repaint() calls my paint() function — as far as I'm concerned, it's magic, but I've noticed that repaint() calls paint() in my test program, but not in the more complicated actual program, and I assume that's why the circle is no longer painting on my program.



Entire code is below:



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

public class Reflexology1 extends JFrame{

private static final long serialVersionUID = -1295261024563143679L;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
private Timer moveBallTimer;
int _ballXpos, _ballYpos;
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");
final JFrame frame2 = new JFrame("The Test");
JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");

public static void main(String args){
new Reflexology1();
}

public Reflexology1(){
frame.setSize(600, 475);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Reflexology 1.0");
frame.setResizable(false);

frame2.setSize(600, 475);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("Reflexology 1.0");
frame2.setResizable(false);

button1 = new JButton("Accept");
button2 = new JButton("Decline");
//movingButton = new JButton("Click Me");

ListenForAcceptButton lForAButton = new ListenForAcceptButton();
ListenForDeclineButton lForDButton = new ListenForDeclineButton();
button1.addActionListener(lForAButton);
button2.addActionListener(lForDButton);
//movingButton.addActionListener(lForMButton);

JTextArea textArea1 = new JTextArea(24, 50);

textArea1.setText("Tracking Eventsn");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setSize(15, 50);
textArea1.setEditable(false);

FileReader reader = null;
try {
reader = new FileReader("EULA.txt");
textArea1.read(reader, "EULA.txt");
} catch (IOException exception) {
System.err.println("Problem loading file");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}

}

JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
AdjustmentListener listener = new MyAdjustmentListener();

thePanel.add(scrollBar1);
thePanel.add(button1);
thePanel.add(button2);

frame.add(thePanel);
ListenForMouse lForMouse = new ListenForMouse();
thePlacebo.addMouseListener(lForMouse);
thePlacebo.add(label1);
frame2.add(thePlacebo);

ListenForWindow lForWindow = new ListenForWindow();
frame.addWindowListener(lForWindow);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
}
});
frame.setVisible(true);

moveBallTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBall();
System.out.println("Timer started!");
repaint();
}
});

addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(frame2.isVisible()){ moveBallTimer.start(); }
}
});

}



private class ListenForAcceptButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button1){
Calendar ClCDateTime = Calendar.getInstance();
System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
_closeTime = ClCDateTime.getTimeInMillis() - _openTime;
//frame.getContentPane().remove(thePanel);
//thePlacebo.addKeyListener(lForKeys);
//frame.getContentPane().add(thePlacebo);

//frame.repaint();
//moveBallTimer.start();
frame.setVisible(false);
frame2.setVisible(true);
frame2.revalidate();
frame2.repaint();


}
}
}


private class ListenForDeclineButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button2){
JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}


private class ListenForWindow implements WindowListener{

public void windowActivated(WindowEvent e) {
//textArea1.append("Window is active");

}

// if this.dispose() is called, this is called:
public void windowClosed(WindowEvent arg0) {

}

// When a window is closed from a menu, this is called:
public void windowClosing(WindowEvent arg0) {

}

// Called when the window is no longer the active window:
public void windowDeactivated(WindowEvent arg0) {
//textArea1.append("Window is NOT active");

}

// Window gone from minimized to normal state
public void windowDeiconified(WindowEvent arg0) {
//textArea1.append("Window is in normal state");

}

// Window has been minimized
public void windowIconified(WindowEvent arg0) {
//textArea1.append("Window is minimized");

}

// Called when the Window is originally created
public void windowOpened(WindowEvent arg0) {
//textArea1.append("Let there be Window!");
Calendar OlCDateTime = Calendar.getInstance();
_openTime = OlCDateTime.getTimeInMillis();
//System.out.println(_openTime);

}

}


private class MyAdjustmentListener implements AdjustmentListener {

public void adjustmentValueChanged(AdjustmentEvent arg0) {
AdjustmentEvent scrollBar1;
//System.out.println(scrollBar1.getValue()));

}

}

public void paint(Graphics g) {
//super.paint(g);
frame2.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
System.out.println("Calling fill()");
}

protected void moveBall() {
//System.out.println("I'm in the moveBall() function!");
int width = getWidth();
int height = getHeight();
int min, max, randomX, randomY;
min =200;
max = -200;
randomX = min + (int)(Math.random() * ((max - min)+1));
randomY = min + (int)(Math.random() * ((max - min)+1));
//System.out.println(randomX + ", " + randomY);

Rectangle ballBounds = ball.getBounds();
//System.out.println(ballBounds.x + ", " + ballBounds.y);
if (ballBounds.x + randomX < 0) {
randomX = 200;
} else if (ballBounds.x + ballBounds.width + randomX > width) {
randomX = -200;
}
if (ballBounds.y + randomY < 0) {
randomY = 200;
} else if (ballBounds.y + ballBounds.height + randomY > height) {
randomY = -200;
}

ballBounds.x += randomX;
ballBounds.y += randomY;
_ballXpos = ballBounds.x;
_ballYpos = ballBounds.y;
ball.setFrame(ballBounds);
}



public void start() {
moveBallTimer.start();
}

public void stop() {
moveBallTimer.stop();
}

private class ListenForMouse implements MouseListener{

// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "n");
if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
System.out.println("TRUE");
}
System.out.println("{e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " / " + "_ballYpos: " + _ballYpos);

}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
// System.out.println("e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos);



// Mouse over
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

// Mouse left the mouseover area:
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}



}


Could anyone tell me what I need to do to get repaint() to call the paint() method in the above program? I'm assuming the multiple frames is causing the problem, but that's just a guess. Thanks.










share|improve this question
















Let me start off by saying I know I've violated some basic Java principles in this messy code, but I'm desperately trying to finish a program by Tuesday for a social science experiment, and I don't know Java, so I'm basically just fumbling through it for now.



With that disclaimer out of the way, I have a separate program working where a circle is moving around the screen and the user must click on it. It works fine when its in its own separate class file, but when I add the code to my main program, it's no longer working. I don't even really understand why repaint() calls my paint() function — as far as I'm concerned, it's magic, but I've noticed that repaint() calls paint() in my test program, but not in the more complicated actual program, and I assume that's why the circle is no longer painting on my program.



Entire code is below:



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

public class Reflexology1 extends JFrame{

private static final long serialVersionUID = -1295261024563143679L;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
private Timer moveBallTimer;
int _ballXpos, _ballYpos;
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");
final JFrame frame2 = new JFrame("The Test");
JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");

public static void main(String args){
new Reflexology1();
}

public Reflexology1(){
frame.setSize(600, 475);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Reflexology 1.0");
frame.setResizable(false);

frame2.setSize(600, 475);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("Reflexology 1.0");
frame2.setResizable(false);

button1 = new JButton("Accept");
button2 = new JButton("Decline");
//movingButton = new JButton("Click Me");

ListenForAcceptButton lForAButton = new ListenForAcceptButton();
ListenForDeclineButton lForDButton = new ListenForDeclineButton();
button1.addActionListener(lForAButton);
button2.addActionListener(lForDButton);
//movingButton.addActionListener(lForMButton);

JTextArea textArea1 = new JTextArea(24, 50);

textArea1.setText("Tracking Eventsn");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setSize(15, 50);
textArea1.setEditable(false);

FileReader reader = null;
try {
reader = new FileReader("EULA.txt");
textArea1.read(reader, "EULA.txt");
} catch (IOException exception) {
System.err.println("Problem loading file");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}

}

JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
AdjustmentListener listener = new MyAdjustmentListener();

thePanel.add(scrollBar1);
thePanel.add(button1);
thePanel.add(button2);

frame.add(thePanel);
ListenForMouse lForMouse = new ListenForMouse();
thePlacebo.addMouseListener(lForMouse);
thePlacebo.add(label1);
frame2.add(thePlacebo);

ListenForWindow lForWindow = new ListenForWindow();
frame.addWindowListener(lForWindow);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
}
});
frame.setVisible(true);

moveBallTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBall();
System.out.println("Timer started!");
repaint();
}
});

addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(frame2.isVisible()){ moveBallTimer.start(); }
}
});

}



private class ListenForAcceptButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button1){
Calendar ClCDateTime = Calendar.getInstance();
System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
_closeTime = ClCDateTime.getTimeInMillis() - _openTime;
//frame.getContentPane().remove(thePanel);
//thePlacebo.addKeyListener(lForKeys);
//frame.getContentPane().add(thePlacebo);

//frame.repaint();
//moveBallTimer.start();
frame.setVisible(false);
frame2.setVisible(true);
frame2.revalidate();
frame2.repaint();


}
}
}


private class ListenForDeclineButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button2){
JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}


private class ListenForWindow implements WindowListener{

public void windowActivated(WindowEvent e) {
//textArea1.append("Window is active");

}

// if this.dispose() is called, this is called:
public void windowClosed(WindowEvent arg0) {

}

// When a window is closed from a menu, this is called:
public void windowClosing(WindowEvent arg0) {

}

// Called when the window is no longer the active window:
public void windowDeactivated(WindowEvent arg0) {
//textArea1.append("Window is NOT active");

}

// Window gone from minimized to normal state
public void windowDeiconified(WindowEvent arg0) {
//textArea1.append("Window is in normal state");

}

// Window has been minimized
public void windowIconified(WindowEvent arg0) {
//textArea1.append("Window is minimized");

}

// Called when the Window is originally created
public void windowOpened(WindowEvent arg0) {
//textArea1.append("Let there be Window!");
Calendar OlCDateTime = Calendar.getInstance();
_openTime = OlCDateTime.getTimeInMillis();
//System.out.println(_openTime);

}

}


private class MyAdjustmentListener implements AdjustmentListener {

public void adjustmentValueChanged(AdjustmentEvent arg0) {
AdjustmentEvent scrollBar1;
//System.out.println(scrollBar1.getValue()));

}

}

public void paint(Graphics g) {
//super.paint(g);
frame2.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
System.out.println("Calling fill()");
}

protected void moveBall() {
//System.out.println("I'm in the moveBall() function!");
int width = getWidth();
int height = getHeight();
int min, max, randomX, randomY;
min =200;
max = -200;
randomX = min + (int)(Math.random() * ((max - min)+1));
randomY = min + (int)(Math.random() * ((max - min)+1));
//System.out.println(randomX + ", " + randomY);

Rectangle ballBounds = ball.getBounds();
//System.out.println(ballBounds.x + ", " + ballBounds.y);
if (ballBounds.x + randomX < 0) {
randomX = 200;
} else if (ballBounds.x + ballBounds.width + randomX > width) {
randomX = -200;
}
if (ballBounds.y + randomY < 0) {
randomY = 200;
} else if (ballBounds.y + ballBounds.height + randomY > height) {
randomY = -200;
}

ballBounds.x += randomX;
ballBounds.y += randomY;
_ballXpos = ballBounds.x;
_ballYpos = ballBounds.y;
ball.setFrame(ballBounds);
}



public void start() {
moveBallTimer.start();
}

public void stop() {
moveBallTimer.stop();
}

private class ListenForMouse implements MouseListener{

// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "n");
if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
System.out.println("TRUE");
}
System.out.println("{e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " / " + "_ballYpos: " + _ballYpos);

}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
// System.out.println("e.getX(): " + e.getX() + " / " + "_ballXpos: " + _ballXpos);



// Mouse over
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

// Mouse left the mouseover area:
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}



}


Could anyone tell me what I need to do to get repaint() to call the paint() method in the above program? I'm assuming the multiple frames is causing the problem, but that's just a guess. Thanks.







java swing jpanel repaint graphics2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 29 '12 at 7:17









mKorbel

104k14106264




104k14106264










asked Oct 28 '12 at 22:08









allocateallocate

82921025




82921025













  • Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

    – MadProgrammer
    Oct 28 '12 at 22:16











  • Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

    – allocate
    Oct 28 '12 at 22:43



















  • Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

    – MadProgrammer
    Oct 28 '12 at 22:16











  • Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

    – allocate
    Oct 28 '12 at 22:43

















Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

– MadProgrammer
Oct 28 '12 at 22:16





Quickly, you are obligated to call super.paint. There's a lot of "magic" going on there that needs to be allowed to run. Having said that, it won't fix your immediate problem, but it will prevent future problems ;)

– MadProgrammer
Oct 28 '12 at 22:16













Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

– allocate
Oct 28 '12 at 22:43





Thanks for that. I had commented it out because I thought it was trying to draw on my first frame.

– allocate
Oct 28 '12 at 22:43












1 Answer
1






active

oldest

votes


















6














You should not paint directly on JFrame. JPanel or extension of JComponent should be used. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);.



Also, keep in mind that repaint() only schedules component update, it does not trigger immediate paint().



For more details see Performing Custom Painting and Painting in AWT and Swing.



For example you can apply the following changes. TestPanel will paint the ball.



class TestPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}
}


Then, update thePlacebo panel:



JPanel thePlacebo = new TestPanel();


Add repaint in moveBall():



thePlacebo.repaint();


Here is an example based on original code:



package so;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.*;

public class TestBall {
private static void createAndShowUI() {
final TestPanel panel = new TestPanel();
panel.validate();
JFrame frame = new JFrame("TestBall");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.moveBall();
}
};
Timer timer = new Timer(1000, timerAction);
timer.setRepeats(true);
timer.start();
}

static class TestPanel extends JPanel {
public static int BALL_SIZE = 25;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, BALL_SIZE,
BALL_SIZE);
Random rand = new Random();

public TestPanel() {
this.addMouseListener(new MouseAdapter() {
// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
if (ball.contains(e.getPoint())) {
System.out.println("hit the ball");
}
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}

public void moveBall() {
Rectangle ballBounds = ball.getBounds();
ball.setFrame(ballBounds);
ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1;
ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1;
ball.setFrame(ballBounds);
repaint();
}
}

public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}





share|improve this answer


























  • Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

    – allocate
    Oct 28 '12 at 23:05











  • are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

    – tenorsax
    Oct 28 '12 at 23:10











  • Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

    – allocate
    Oct 28 '12 at 23:15








  • 1





    @JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

    – tenorsax
    Oct 28 '12 at 23:26








  • 1





    oh, you are my new favorite person. Thanks so much for the help.

    – allocate
    Oct 28 '12 at 23:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13113498%2frepaint-not-calling-paint-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














You should not paint directly on JFrame. JPanel or extension of JComponent should be used. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);.



Also, keep in mind that repaint() only schedules component update, it does not trigger immediate paint().



For more details see Performing Custom Painting and Painting in AWT and Swing.



For example you can apply the following changes. TestPanel will paint the ball.



class TestPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}
}


Then, update thePlacebo panel:



JPanel thePlacebo = new TestPanel();


Add repaint in moveBall():



thePlacebo.repaint();


Here is an example based on original code:



package so;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.*;

public class TestBall {
private static void createAndShowUI() {
final TestPanel panel = new TestPanel();
panel.validate();
JFrame frame = new JFrame("TestBall");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.moveBall();
}
};
Timer timer = new Timer(1000, timerAction);
timer.setRepeats(true);
timer.start();
}

static class TestPanel extends JPanel {
public static int BALL_SIZE = 25;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, BALL_SIZE,
BALL_SIZE);
Random rand = new Random();

public TestPanel() {
this.addMouseListener(new MouseAdapter() {
// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
if (ball.contains(e.getPoint())) {
System.out.println("hit the ball");
}
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}

public void moveBall() {
Rectangle ballBounds = ball.getBounds();
ball.setFrame(ballBounds);
ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1;
ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1;
ball.setFrame(ballBounds);
repaint();
}
}

public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}





share|improve this answer


























  • Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

    – allocate
    Oct 28 '12 at 23:05











  • are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

    – tenorsax
    Oct 28 '12 at 23:10











  • Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

    – allocate
    Oct 28 '12 at 23:15








  • 1





    @JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

    – tenorsax
    Oct 28 '12 at 23:26








  • 1





    oh, you are my new favorite person. Thanks so much for the help.

    – allocate
    Oct 28 '12 at 23:31
















6














You should not paint directly on JFrame. JPanel or extension of JComponent should be used. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);.



Also, keep in mind that repaint() only schedules component update, it does not trigger immediate paint().



For more details see Performing Custom Painting and Painting in AWT and Swing.



For example you can apply the following changes. TestPanel will paint the ball.



class TestPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}
}


Then, update thePlacebo panel:



JPanel thePlacebo = new TestPanel();


Add repaint in moveBall():



thePlacebo.repaint();


Here is an example based on original code:



package so;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.*;

public class TestBall {
private static void createAndShowUI() {
final TestPanel panel = new TestPanel();
panel.validate();
JFrame frame = new JFrame("TestBall");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.moveBall();
}
};
Timer timer = new Timer(1000, timerAction);
timer.setRepeats(true);
timer.start();
}

static class TestPanel extends JPanel {
public static int BALL_SIZE = 25;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, BALL_SIZE,
BALL_SIZE);
Random rand = new Random();

public TestPanel() {
this.addMouseListener(new MouseAdapter() {
// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
if (ball.contains(e.getPoint())) {
System.out.println("hit the ball");
}
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}

public void moveBall() {
Rectangle ballBounds = ball.getBounds();
ball.setFrame(ballBounds);
ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1;
ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1;
ball.setFrame(ballBounds);
repaint();
}
}

public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}





share|improve this answer


























  • Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

    – allocate
    Oct 28 '12 at 23:05











  • are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

    – tenorsax
    Oct 28 '12 at 23:10











  • Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

    – allocate
    Oct 28 '12 at 23:15








  • 1





    @JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

    – tenorsax
    Oct 28 '12 at 23:26








  • 1





    oh, you are my new favorite person. Thanks so much for the help.

    – allocate
    Oct 28 '12 at 23:31














6












6








6







You should not paint directly on JFrame. JPanel or extension of JComponent should be used. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);.



Also, keep in mind that repaint() only schedules component update, it does not trigger immediate paint().



For more details see Performing Custom Painting and Painting in AWT and Swing.



For example you can apply the following changes. TestPanel will paint the ball.



class TestPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}
}


Then, update thePlacebo panel:



JPanel thePlacebo = new TestPanel();


Add repaint in moveBall():



thePlacebo.repaint();


Here is an example based on original code:



package so;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.*;

public class TestBall {
private static void createAndShowUI() {
final TestPanel panel = new TestPanel();
panel.validate();
JFrame frame = new JFrame("TestBall");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.moveBall();
}
};
Timer timer = new Timer(1000, timerAction);
timer.setRepeats(true);
timer.start();
}

static class TestPanel extends JPanel {
public static int BALL_SIZE = 25;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, BALL_SIZE,
BALL_SIZE);
Random rand = new Random();

public TestPanel() {
this.addMouseListener(new MouseAdapter() {
// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
if (ball.contains(e.getPoint())) {
System.out.println("hit the ball");
}
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}

public void moveBall() {
Rectangle ballBounds = ball.getBounds();
ball.setFrame(ballBounds);
ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1;
ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1;
ball.setFrame(ballBounds);
repaint();
}
}

public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}





share|improve this answer















You should not paint directly on JFrame. JPanel or extension of JComponent should be used. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);.



Also, keep in mind that repaint() only schedules component update, it does not trigger immediate paint().



For more details see Performing Custom Painting and Painting in AWT and Swing.



For example you can apply the following changes. TestPanel will paint the ball.



class TestPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}
}


Then, update thePlacebo panel:



JPanel thePlacebo = new TestPanel();


Add repaint in moveBall():



thePlacebo.repaint();


Here is an example based on original code:



package so;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.*;

public class TestBall {
private static void createAndShowUI() {
final TestPanel panel = new TestPanel();
panel.validate();
JFrame frame = new JFrame("TestBall");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300, 300));
frame.setLocationRelativeTo(null);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.moveBall();
}
};
Timer timer = new Timer(1000, timerAction);
timer.setRepeats(true);
timer.start();
}

static class TestPanel extends JPanel {
public static int BALL_SIZE = 25;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, BALL_SIZE,
BALL_SIZE);
Random rand = new Random();

public TestPanel() {
this.addMouseListener(new MouseAdapter() {
// Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
if (ball.contains(e.getPoint())) {
System.out.println("hit the ball");
}
}
});
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
}

public void moveBall() {
Rectangle ballBounds = ball.getBounds();
ball.setFrame(ballBounds);
ballBounds.x = rand.nextInt(getWidth() - BALL_SIZE + 1) + 1;
ballBounds.y = rand.nextInt(getHeight() - BALL_SIZE + 1) + 1;
ball.setFrame(ballBounds);
repaint();
}
}

public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 28 '12 at 22:57

























answered Oct 28 '12 at 22:16









tenorsaxtenorsax

19.6k94286




19.6k94286













  • Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

    – allocate
    Oct 28 '12 at 23:05











  • are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

    – tenorsax
    Oct 28 '12 at 23:10











  • Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

    – allocate
    Oct 28 '12 at 23:15








  • 1





    @JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

    – tenorsax
    Oct 28 '12 at 23:26








  • 1





    oh, you are my new favorite person. Thanks so much for the help.

    – allocate
    Oct 28 '12 at 23:31



















  • Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

    – allocate
    Oct 28 '12 at 23:05











  • are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

    – tenorsax
    Oct 28 '12 at 23:10











  • Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

    – allocate
    Oct 28 '12 at 23:15








  • 1





    @JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

    – tenorsax
    Oct 28 '12 at 23:26








  • 1





    oh, you are my new favorite person. Thanks so much for the help.

    – allocate
    Oct 28 '12 at 23:31

















Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

– allocate
Oct 28 '12 at 23:05





Thanks for your response. It's helped me get the circle to show up on the first form, but even with your recommended changes, when thePlacebo.repaint(); is called in moveBall, your function isn't called. It's called when the first frame loads, but never again after that :

– allocate
Oct 28 '12 at 23:05













are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

– tenorsax
Oct 28 '12 at 23:10





are you certain that paintComponent is not called? There might be a mix up with coordinates calculation in moveBall. Did you try to run a sample from my last edit?

– tenorsax
Oct 28 '12 at 23:10













Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

– allocate
Oct 28 '12 at 23:15







Yes, the sample works fine. And I've actually had my code working when it wasn't integrated into this big mess, but the problems seem to arise when working with multiple JFrames — it seems to me that the focus is somehow always on the main JFrame (and unfortunately I need this to work on the one I show after the user presses a button). I am certain that paintComponent is not called once the second JFrame is visible though — I used println to make sure.

– allocate
Oct 28 '12 at 23:15






1




1





@JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

– tenorsax
Oct 28 '12 at 23:26







@JoshuaAuriemma the coordinates in moveBall fall out of screen. Here is an updated version with minimal changes to make the ball appear on screen

– tenorsax
Oct 28 '12 at 23:26






1




1





oh, you are my new favorite person. Thanks so much for the help.

– allocate
Oct 28 '12 at 23:31





oh, you are my new favorite person. Thanks so much for the help.

– allocate
Oct 28 '12 at 23:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13113498%2frepaint-not-calling-paint-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules