Reputation: 8292
I am getting a problem in java program for moving a circle. The program has 2 basic buttons:
START: used for moving the circle
STOP: used for exiting the program
My problem is that the circle appears before I press the start button. However I make the call to move only when START Button is pressed which makes the call to repaint and then the circle should appear. But it appears by default. I have no problem in moving the circle. Here is my source code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Moving extends JFrame implements ActionListener
{
JButton start,stop;Move mypanel;
Moving()
{
setTitle("Moving circle");
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
start=new JButton("START");
stop=new JButton("STOP");
start.addActionListener(this);stop.addActionListener(this);
JPanel p=new JPanel();
p.setLayout(new FlowLayout());
p.add(start);p.add(stop);
getContentPane().add(p,BorderLayout.SOUTH);
mypanel=new Move();
getContentPane().add(mypanel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("STOP"))
System.exit(0);
else if(s.equals("START"))
mypanel.move();
}
public static void main(String []args)
{
Moving obj=new Moving();
obj.setVisible(true);
}
}
class Move extends JPanel
{ static int x=80, cv=0;
public void move()
{
repaint();
}
public void paintComponent(Graphics g)
{
setBackground(Color.green);
super.paintComponent(g);
g.setColor(Color.red);
if(x<500&&cv==0)
{g.fillOval(x, 80, 100, 100);this.inc();}
else if((x>=500||cv==1)&&x>=80)
{
cv=1;g.fillOval(x, 80, 100, 100);this.dec();
}
else cv=0;
}
public void inc()
{
x+=10;
}
public void dec()
{
x-=10;
}
}
Upvotes: 1
Views: 50
Reputation: 3171
My suggestion is that if you do not want to show your circle before the pressing of the start button you have to remove drawing of the circle from the paintComponent
method.
You can achieve the above by declaring a new method in your Move
class say drawCircle
like this:
public void drawCircle(Graphics g){
g.setColor(Color.red);
if(x<500&&cv==0)
{g.fillOval(x, 80, 100, 100);this.inc();}
else if((x>=500||cv==1)&&x>=80)
{
cv=1;g.fillOval(x, 80, 100, 100);this.dec();
}
else cv=0;
}
now in your Move
class introduce a new boolean
variable say circleShowing
and intialise to false
. This is declared so that the paintComponent
method of the Move
class can decide whether the circle should be drawn or not. You can do it like this:
public void paintComponent(Graphics g){
setBackground(Color.green);
super.paintComponent(g);
if(circleShowing){
drawCircle(g);
}
}
Now finally in the actionPerformed
method of your Moving
class you in the else if
block you could write the following:
mypanel.circleShowing = true;
mypanel.move();
I hope if you make these changes this will work as you wish.
Upvotes: 0
Reputation: 11648
By default setVisible
would be true. So, if you don't won't to show panel on load then make it false
on constructor.
And then make it visible when you hit Start.
See below:
class Move extends JPanel {
static int x = 80, cv = 0;
Move(){
setVisible(false); <-- Make it false here
}
public void move() {
this.setVisible(true); <----- make it visible here when you click start
repaint();
}
public void paintComponent(Graphics g) {
setBackground(Color.green);
..... REMAINING CODE
}
}
Upvotes: 1