Issue
I'm having a problem with my program that is trying to draw things using the Java JFrame, and FPanel, I also looked at the other question labeled in a simular fashio ( Shapes not drawing in Java ), but from that question I was unable to ascertain what was wrong with my program. So now, even though this is in a sense a copy, I am asking for help in pointing out what I have a wrong. I am also using netbeans at my medium.
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;
public class BullsEye extends JPanel{
@Override
public void printComponent(Graphics g)
{
super.paintComponent(g);
//for(int x =0; x>10;x++)
//{
int x=10;
int y =(100-10*(x-1));
//if((x%2)==0)
//{
g.setColor(Color.RED);//setting color
g.drawRect(100, 10, 10, 15);//drawing
g.drawOval(0, 0, 100, 100);//drawing
//}
// else
//{
g.setColor(Color.GREEN);//setting color
g.fillOval(10, 10, 50, 50);//drawing
//}
//}
}
public static void main(String[] args)
{
BullsEye b = new BullsEye();//creating b varaible for drawings
JFrame jf = new JFrame();//frame varaible for the frame
jf.setTitle("BullsEye");//setting title
jf.setSize(500,400);//setting size
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//opertion close
jf.add(b);//adding to the frame
jf.setVisible(true);//setting it to visible
}
}
Solution
You are overriding the wrong method. Override paintComponent instead of printComponent as it is only for printing. Good tutorial for more info: https://docs.oracle.com/javase/tutorial/uiswing/painting/
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Font;
public class BullsEye extends JPanel{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//for(int x =0; x>10;x++)
//{
int x=10;
int y =(100-10*(x-1));
//if((x%2)==0)
//{
g.setColor(Color.RED);//setting color
g.drawRect(100, 10, 10, 15);//drawing
g.drawOval(0, 0, 100, 100);//drawing
//}
// else
//{
g.setColor(Color.GREEN);//setting color
g.fillOval(10, 10, 50, 50);//drawing
//}
//}
}
public static void main(String[] args)
{
BullsEye b = new BullsEye();//creating b varaible for drawings
JFrame jf = new JFrame();//frame varaible for the frame
jf.setTitle("BullsEye");//setting title
jf.setSize(500,400);//setting size
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//opertion close
jf.add(b);//adding to the frame
jf.setVisible(true);//setting it to visible
}
}
Answered By - lwi