1 Replies - 1868 Views - Last Post: 15 April 2011 - 01:36 PM Rate Topic: -----

#1 kiarar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-April 11

Problem adding Graphics to JPanel

Posted 15 April 2011 - 12:44 PM

Hi all. This is my first time using Graphics and I'm writing a program for a projectile. The user inputs a speed and an angle from the horizontal, and then clicks a button for the object to launch. There are no requirements for what to include or exclude in the code, but I am supposed to be able to explain what each line does (in theory though, as my teacher doesn't often actually require us to do so.) I plan to have a circular shape drawn with fillOval as my projectile, and two separate JPanels: one for the user's input and another where the projectile will be displayed and launched.
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)
		Graphics2D g=(Graphics2D) g1;
		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();		

		space.add(new Projectile());
		JFrame frame=new JFrame("Projectile");
		frame.setSize (650, 500);
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem adding Graphics to JPanel

#2 MrLuke187   User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Problem adding Graphics to JPanel

Posted 15 April 2011 - 01:36 PM

First, what is that:
Graphics2D g=(Graphics2D) g1;

Simply draw on the Graphics Object if you want to do something in 2D...

The super-keyword calls the specified method from the class, which is extended by your class. In your example, it calls the paint()-Method from the JPanel-Class.

To move something you painted, you'll need to redraw it on some other coordinates. For this, you'll need the update()-method and a Thread. See here.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1