Reputation: 538
This is my program:
sub class:
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Scanner;
public class abc extends JPanel {
public void paintComponent(Graphics g) {
Scanner input = new Scanner(System.in);
super.paintComponent(g);
this.setBackground(Color.WHITE);
int a, b;
System.out.print("input a: ");
a = input.nextInt();
a = b;
g.setColor(Color.BLUE);
g.fillOval(150, 40, a, b);
}
}
main class:
import java.awt.Color;
import javax.swing.JFrame;
public class abcd {
public static void main(String args[]) {
JFrame frame = new JFrame("Draw");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
abc panel = new abc();
panel.setBackground(Color.WHITE);
frame.add(panel);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
Output always repeat; I have to input 3-4 times. I make this with sub class and main class. Please help with this problem, and explain why it repeats?
Upvotes: 0
Views: 106
Reputation: 8268
I noticed few things in your code that might be causing the problem :
public class abc extends JPanel{
public void paintComponent(Graphics g){
Scanner input=new Scanner(System.in);
super.paintComponent(g);
this.setBackground(Color.WHITE);
int a,b;
System.out.print("input a: ");
a=input.nextInt();
a=b;shouldn't this be b=a , maybe you did it by mistake?
g.setColor(Color.BLUE);
g.fillOval(150,40,a,b);
}
Secondly, why are you painting the panel twice, first in the abcd
class and also in abc
class?
Upvotes: 3