Results 1 to 4 of 4

Thread: Paint????

  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Paint????

    Hey guys its me again. So I'm having an issue.

    Java Code:
    package pr1;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class SemiPongPanel extends JPanel { // start of class
    	public static void main(String[] args) { // start of main class
    
    		JFrame frame1 = new JFrame("SemiPong"); // new jframe called frame1
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // default close
    																// jframe on
    																// exit
    
    		JPanel panel1 = new JPanel(); // New Jpanel
    		frame1.getContentPane().add(panel1); // Add panel1 to frame1
    		panel1.setPreferredSize(new Dimension(600, 400)); // size of panel
    		frame1.pack(); // Packs the panel size into the frame
    		frame1.setVisible(true); // Make frame visible
    		
    		Ball singleBall = new Ball(30, 40, 10, Color.RED);
    		
    		
    		
    	//	@Override
    	//	public void paint(Graphics g){
    			
    	//	}
    		
    		
    	}
    }

    when placing
    Java Code:
     public void paint(Graphics g)
    I get an error that states. Void is an invalid type for the variable paint. But in my other program......

    Java Code:
    package balls;
    
    
    
    import java.awt.Color;
    
    import java.awt.Graphics;
    
    import java.awt.Rectangle;
    
    import java.util.Random;
    
    
    
    import javax.swing.JApplet;
    
    
    
    
    
    
    
    // Chantae M. Ross
    
    @SuppressWarnings("serial")						// This got rid of a weird error i have never seen....
    
    public class BallsApplet extends JApplet {
    
    	//private Ball ball;
    
    	private Ball[] testBall;
    
    	private static Physics physics;
    
    
    
    	
    
    	
    
    	public void init() {
    
    		super.setSize(400, 500);
    
    		//ball = new Ball(30, 30, 10, Color.green);
    
    		physics = new Physics(this, this, 10);
    
    		//physics.addBall(ball);
    
    		testBall = new Ball[20];
    
    		for (int index = 0; index < testBall.length; index++) {
    
    			testBall[index] = makeRandomBall();
    
    			physics.addBall(testBall[index]);
    
    		}
    
    	}
    
    
    
    	@Override
    
    	public void paint(Graphics g) {
    
    
    
    		int redBalls = 0;
    
    		int greenBalls = 0;
    
    		Rectangle bounds = getBounds();
    
    		g.setColor(Color.lightGray);
    
    		g.fillRect(0, 0, bounds.width, bounds.height);
    
    		//ball.draw(g);
    
    		for (int index = 0; index < testBall.length; index++) {
    
    			testBall[index].draw(g);
    
    			if (testBall[index].getColor().equals(Color.red))
    
    				redBalls++;
    
    			else
    
    				greenBalls++;
    
    		}
    
    		g.setColor(Color.BLACK);
    
    		g.drawString("Chantae M. Ross", 10, 15);
    
    		g.drawString("RedBalls: " + redBalls, 10, 30);
    
    		g.drawString("Green Balls: " + greenBalls, 10, 45);
    
    		if (redBalls == 20)
    
    			physics.stopIt();
    
    	}
    
    
    
    	
    
    	
    
    	private int randomRange(int low, int high) {
    
    		Random rand = new Random();
    
    
    
    		int range = high - low;
    
    
    
    		
    
    
    
    
    
    
    
    
    
    		int number = low + rand.nextInt(range);
    
    		return number;
    
    	}
    
    
    
    	private Ball makeRandomBall() { // What the hell is going on here???
    
    		// randomBalls = new Ball(randomRange(20, 200), randomRange(20, 200),
    
    		// randomRange(2, 10), Color.GREEN);
    
    		// return randomBalls;
    
    		Ball randomBalls = new Ball(randomRange(20, 200), randomRange(20, 200),
    
    				randomRange(2, 10), Color.GREEN);
    
    
    
    		randomBalls.setVelocityX(randomRange(-40, 40));
    
    		randomBalls.setVelocityY(randomRange(-40, 40));
    
    
    
    		// for (int index = 0; index < randomBalls.length; index++)
    
    		return randomBalls;
    
    
    
    	}
    
    
    
    	/*
    
    	 * private Ball fBall1;
    
    	 * 
    
    	 * @Override public void init() { super.setSize(400, 500);
    
    	 * 
    
    	 * fBall1 = new Ball(30, 30, 10, Color.GREEN); }
    
    	 * 
    
    	 * @Override public void paint(Graphics g) { fBall1.draw(g); } }
    
    	 */
    
    
    
    }

    It works perfectly fine????

    I've attached a screenshot of the error also just to clear up any confusion.
    Attached Thumbnails Attached Thumbnails Paint????-sample.jpg  

  2. #2
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default

    refer the reply of Fubarable in the thread "an error in paint method"

  3. #3
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Entering super.paint(g), or paintComponent, etc doesn't work. Are you talking about the post that he is stating, the user is drawing directly to the JFrame??

  4. #4
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Ok I got it, I had to declare the paint method outside the main method.

Similar Threads

  1. Paint problem =P
    By santa in forum New To Java
    Replies: 7
    Last Post: 02-08-2011, 09:29 AM
  2. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 04:24 PM
  3. Some confusions with paint()
    By kendaop in forum Java Applets
    Replies: 1
    Last Post: 01-24-2009, 12:23 AM
  4. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 10:33 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •