Results 1 to 2 of 2
  1. #1
    waker3210 is offline Member
    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Default Need help painting to a Canvas

    Im having a problem painting something that i feel should work. Since i've never really done something like this before I'm not sure whats going wrong. Here's my code, i have 2 classes, 1 named Board, the other named Boulder:

    Java Code:
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class Board extends Canvas implements Runnable
    {
    	static JFrame f;
    	int ballNum = 0;
    	Graphics2D g;
    	Boulder b = new Boulder();
    	int flag = 1;
    	
    	public static void main(String[] args)
    	{
    		f = new JFrame("DODGE!!");
    		f.setSize(800,600);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setLocationRelativeTo(null);
    		Board canvas = new Board();
    		f.add(canvas);
    		f.setVisible(true);
    	}
    	
    	public void run()
    	{
    		if(flag == 1)
    		{
    			setCoordinates();
    			Draw(g);
    			flag = 2;
    		}
    	}
    	
    	public void Draw(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		g2.fill(b.balls.get(ballNum));
    	}
    	
    	public void setCoordinates()
    	{
    		b.setX(1+(int)(Math.random()*800));
    		b.setY(400);
    		b.setHeight(10+(int)(Math.random()*35));
    		b.setWidth(b.getHeight());
    		b.makeBoulder();
    	}
    }
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    
    import javax.swing.*;
    import java.util.ArrayList;
    
    public class Boulder
    {
    	int x;
    	int y;
    	int w;
    	int h;
    	
    	ArrayList<Ellipse2D.Double> balls = new ArrayList<Ellipse2D.Double>();
    	
    	public void makeBoulder()
    	{
    		Ellipse2D.Double ball = new Ellipse2D.Double();
    		ball.x = getX();
    		ball.y = getY();
    		ball.height = getHeight();
    		ball.width = getWidth();
    		
    		balls.add(ball);
    	}
    	
    	//sets x position of boulder
    	public void setX(int Xpos)
    	{
    		x = Xpos;
    	}
    	
    	//sets y pos
    	public void setY(int Ypos)
    	{
    		y = Ypos;
    	}
    	
    	//sets height of boulder
    	public void setHeight(int height)
    	{
    		h = height;
    	}
    	
    	//sets width
    	public void setWidth(int width)
    	{
    		w = width;
    	}
    	
    	//gets x position
    	public int getX()
    	{
    		return x;
    	}
    	
    	//gets y position
    	public int getY()
    	{
    		return y;
    	}
    	
    	//gets height
    	public int getHeight()
    	{
    		return h;
    	}
    	
    	//gets width
    	public int getWidth()
    	{
    		return w;
    	}
    }
    I dont get any compile errors. The only thing is that it doesnt paint the object. Any ideas how to fix this? (Sorry for the somewhat long code).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help painting to a Canvas

    Why are you mixing Swing with AWT components? Why even use AWT components at all when they've been out of date for over 10 years? You also appear to be trying to do Java graphics without having read a tutorial or API (Draw? You can't make up methods and expect them to magically work).

    My suggestions:
    • Use only Swing components including JFrames, JPanels, etc.
    • Read some tutorials on drawing with Swing. Start here: Lesson: Performing Custom Painting
    • Then try to do what you're attempting here.
    Last edited by Fubarable; 01-29-2012 at 04:59 PM.

Similar Threads

  1. painting whole label
    By simprepol in forum AWT / Swing
    Replies: 9
    Last Post: 01-27-2011, 09:10 AM
  2. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 07:52 AM
  3. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 06:10 AM
  4. AWT: Painting buttons and text areas in a canvas
    By chappa in forum AWT / Swing
    Replies: 6
    Last Post: 01-09-2010, 03:37 PM
  5. Replies: 0
    Last Post: 07-05-2009, 01:31 PM

Posting Permissions

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