Reputation: 1
package work;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.util.Timer;
import java.util.TimerTask;
class NewJPanel extends JPanel{
//private MoveButton button;
static MoveButton button;
JTextField textfield;
NewJPanel(){
setLayout(null);
textfield=new JTextField();
textfield.setBounds(600, 50, 100, 40);
button=new MoveButton();
//button.setBounds(0, 0, 50, 50);
//add(button);
add(textfield);
//repaint();
System.out.println("Newpanel construct");
//System.out.println("1");
//button=new MoveButton();
//button.addActionListener(button);
//add(button);
}
public static void main(String[] args){
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
}
@Override public void paintComponent(Graphics g){
System.out.println("drawing");
super.paintComponent(g);
for(int i=0;i<400;i+=100)
for(int j=0;j<400;j+=100)
g.drawOval(i, j, 100, 100);
//button.setBounds(button.getx(),button.gety(),50,50);
textfield.setText(Integer.toString(button.get()));
}
}
class MoveButton extends JButton implements ActionListener{
private Random randomgenerator=new Random();
private int indexX=0;
private int indexY=0;
private int score=0;
public MoveButton(){
setBounds(0, 0, 50, 50);
addActionListener(this);
System.out.println("movebutton construct");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//setBounds(200,0,100,100);
indexX=100*randomgenerator.nextInt(4);
indexY=100*randomgenerator.nextInt(4);
score+=1;
//System.out.println(indexX);
setBounds(indexX, indexY, 50, 50);
repaint();
}
public int getx(){
return indexX;
}
public int gety(){
return indexY;
}
public int get(){
return score;
}
}
class TimerTaskTest extends java.util.TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Start!");
}
}
Here is my code, I try to draw circles in a JPanel, I really don't understand why paintComponent doesn't work. Could anyone please help me?
Upvotes: 0
Views: 225
Reputation: 21435
Your code would work - if you could trigger a repaint()
event on your panel.
The problem is that you are initializing your GUI on the main thread instead of the EDT. Therefore the JFrame is shown before you can add the NewJPanel
to it. And just adding the NewJPanel
with a fixed layout does not trigger a repaint event.
You should change your main method to
public static void main(String[] args){
SwingUtilities.invokeLater(() -> {
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
});
}
Or, if you cannot use Java8:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
}
});
}
Upvotes: 1