Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Baffled by simple graphics

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Baffled by simple graphics

    What ever happened to good old drawPixel(x,y,col)?

    I'm having a hard time finding out how to make graphics that move on the screen. Basically, just two round ovals that move together based on the effects of gravity computations.

    I have a CelestObject class extending JPanel which defines parameters for a ball (sun and planet) and includes a paint(g) and repaint(g) method. Then there is a Core class with main(args) that runs a for loop, which performs calculations and alters the coordinates of the two balls and instructs to repaint the two images.

    When I run the program, only the planet shows up. It does appear animated, but is very choppy and sporatic. The sun doesn't show up at all, but it does still have an effect on the gravity of the planet.

    I'd like for both ovals to show up and for the movement between them be a lot smoother. I've tried so many places online and texts, but they're all either way too simple, or way over my head. Please tell me what I'm doing wrong!

    Here's the code (2 classes):
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Core {
     
    	public static double deltaX; // to be used in grav calculations
    	public static double deltaY;
    	public static double deltaD;
    	public static double fGravSP;
     
     
    	public static void main(String[] args) { // create the two ovals
    		CelestObject sun = new CelestObject(250, 250);
    		sun.mass = 10000;
    		sun.radius = 60;
    		sun.vVelocity.size = 0.0;
    		sun.vVelocity.setAngleD(0.0);
     
     
    		CelestObject planet = new CelestObject(450, 450);
    		planet.mass = 100;
    		planet.radius = 6;
    		planet.vVelocity.size = 0.0075;
    		planet.vVelocity.setAngleD(-35.0);
     
    // make a new frame to hold the graphics
    		JFrame f = new JFrame("Orbits");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(640, 960);
    		f.setVisible(true);
     
    		// add the two ovals to the frame (which should trigger paint(g))
    		f.add(sun);
    		f.add(planet);
     
     
    		for (int i = 1; i < 500; i++) {
    			// run through the loop 
     
    // do a lot of calculations (tested with dialog boxes and all seem to give the right answers when tested so no problems assumed here)
    ...
    ...
     
    			f.setVisible(true); // if i move this, the planet doesn't appear at all!
    			f.repaint();
            }
        }
     
    }
     
    NEXT CLASS:
     
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    public class CelestObject extends JPanel {
     
    	public static final double bigG = 6.67384 * (Math.pow(10, -11)); // gravitational constant
     
    	public double mass; // in kgs
    	public double radius; // in pixels (1 pixel = 1 mm, 1000 pixels = 1 m) for now
    	public double x, y; // as pixels; (0,0) is top left corner; converted to int when drawn
    	public Vector vVelocity; // the speed and direction of the object (speed in pixels/sec, direction in degrees)
    	public Vector vAccel; // the acceleration and direction of the object
     
    	public CelestObject(int xNew, int yNew) {
    		x = (double)xNew;
    		y = (double)yNew;
    		mass = 0;
    		radius = 1;
     
    		Vector tempV = new Vector(0, 0);
    		vVelocity = tempV;
    		vAccel = tempV;
    	}
     
    	//public class CelestObjectImage extends JPanel { // failed attempt at sub-class
     
    		public void paint(Graphics g) {
    			super.paint(g);
    			g.setColor(Color.BLACK);
    			g.drawOval((int)x, (int)y, (int)radius, (int)radius);
    		}
     
    		public void repaint(Graphics g) {
    			super.paint(g);
    			g.drawOval((int)x, (int)y, (int)radius, (int)radius);
    		}
    }

    Any help or insights anyone can give me would be greatly appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baffled by simple graphics

    Some things I see that are bad:
    You should override the paintComponent method not the paint
    You should NOT override repaint

    Your code does not compile. I get the following errors:
    MovingObjects.java:30: cannot find symbol
    symbol  : variable size
    location: class java.util.Vector
    		sun.vVelocity.size = 0.0;
    		             ^
    MovingObjects.java:31: cannot find symbol
    symbol  : method setAngleD(double)
    location: class java.util.Vector
    		sun.vVelocity.setAngleD(0.0);
    		             ^
    MovingObjects.java:37: cannot find symbol
    symbol  : variable size
    location: class java.util.Vector
    		planet.vVelocity.size = 0.0075;
    		                ^
    MovingObjects.java:38: cannot find symbol
    symbol  : method setAngleD(double)
    location: class java.util.Vector
    		planet.vVelocity.setAngleD(-35.0);
    Last edited by Norm; August 3rd, 2011 at 06:52 AM.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Baffled by simple graphics

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    How would a drawPixel() method help you? The logic would be the same, just more annoying to draw. You can use any of the following if you really think that drawing pixel by pixel would help you:

    fillOval(x, y, 1, 1);
    fillRectangle(x, y, 1, 1);
    drawLine(x, y, x, y);

    You could even use a BufferedImage and use setRGB().

    But yeah, I'm mostly kidding with those. Read through the tutorial I linked, throw together an SSCCE, and we'll be happy to help. Painting is really simple once you get used to it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Baffled by simple graphics

    Thank you both! I'll check out these hints and see what I can do

    @norm - I didn't include my non-graphics class (Vector) so it won't compile for you. Tried to keep the code minimal. But I realize now it's probably easier to help if you can see (and copy) everything

    @Kevin - thanks for the link! I've found it hard finding something similar online. I'd never dream of using drawPixel - just meant as a joke. I used to find graphics like this easy (think BASIC or old Turing coding) and would find the calculations the hard part. Thats reversed now; learning graphics in a OOP context isn't as straight-forward as my mind would like, but time for me to get with the times!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baffled by simple graphics

    my non-graphics class (Vector)
    Suggestion: Don't use an existing Java class's name for your class.

    A suggested approach:
    Create a JPanel and override the paintComponent method. Have that method call a method in your celestial object classes that do NOT extend JPanel with a reference to the Graphics object. They can then draw themselves how they want.
    Add the JPanel to the JFrame. It will be where the objects draw themselves.

    In another method possibly on a Timer thread, compute the next location for the bodies and call repaint which will cause a call paintComponent which will call your objects' methods to draw themselves at the new locations.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Baffled by simple graphics

    Awesome, I got it to work finally and looks perfect! Had to combine basically all the resources you both gave me, but definitely learned a lot.

    I didn't even realize there was already a Vector class haha

    Thanks again!

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Baffled by simple graphics

    Quote Originally Posted by macantas View Post
    Awesome, I got it to work finally and looks perfect! Had to combine basically all the resources you both gave me, but definitely learned a lot.

    I didn't even realize there was already a Vector class haha

    Thanks again!
    I'm glad you got it working. For what it's worth, it's pretty much universally accepted that ArrayLists should be used instead of Vectors.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Baffled by simple graphics

    The OPs Vector class was a custom class ala a physics vector: direction and speed

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Baffled by simple graphics

    Quote Originally Posted by Norm View Post
    The OPs Vector class was a custom class ala a physics vector: direction and speed
    Ohh snap, good catch. Yeah, ArrayList probably wouldn't work for you too well then. :p

    Although, this does point out why you shouldn't use pre-existing names for your classes! Sure, that's what I meant to do the whole time...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  2. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  3. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  4. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM