I'm having trouble adding Graphics to the JPanel. I've looked at lots of different examples online and tried to put them together. I omitted some parts of the code that don't seem that pertinent to me (the JPanel for data, alignment of components, border and background colors) but let me know if they could be causing the errors and I'll post the omitted parts. A description of my problem is beneath the code.
import java.awt.*; import javax.swing.*; public class Projectile extends JPanel { public void paint(Graphics g1) { super.paint(g1); Graphics2D g=(Graphics2D) g1; g.setColor(Color.RED); g.fillOval(0, 0, 25, 25); g.drawLine(0, 0, 100, 100); } public static void main (String [] args) { JPanel all=new JPanel(); JPanel space=new JPanel(); all.add(space); space.add(new Projectile()); JFrame frame=new JFrame("Projectile"); frame.setResizable(false); frame.setSize (650, 500); frame.setContentPane(all); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
So far, this is the only code that has shown anything at all on the JPanel. It shows a quarter of my circle (the top left quarter), but it's very small, and placed somewhere near (250, 25). The line from drawLine is there as well. It does point in the right direction (down and to the right), but seems to go from (235, 10) to (250, 25). So essentially, it is displaying the drawLine and drawOval, just really small and in the wrong place. There are no compiling errors.
P.S. If anyone can explain what "super" does, or post a link, that would be very appreciated. And I'm also a bit lost about how to make the circle move. I'm assuming that I will make circles appear, one after another, very quickly in an arc, to simulate the projectile "moving." Any tips on how I could do that? A friend told me to try "Thread.sleep" but I have no idea what it does. A link to a simple tutorial would be great.