Results 1 to 2 of 2

Thread: Graphics

  1. #1
    Joe2003 is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default Graphics

    Hey

    Im trying to add some graphics to an JFrame. I have already made a jframe with loads of panels that hold seperate content. Anyway I want to add a picture, eg this smile app I riped off the net :p :-

    Java Code:
        public TestDraw()
        {        
            setLayout(new FlowLayout());            
            setTitle("Changing Face");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(250,220);
            setLocation(300,300);
            getContentPane().setBackground(Color.yellow);
            setVisible(true);
        }
        
           
        public void paint(Graphics g)
        {
            super.paint(g);   
            g.setColor(Color.red);
            g.drawOval(85,75,75,75);   
            g.setColor(Color.blue);
            g.drawOval(100,95,10,10);  
            g.drawOval(135,95,10,10); 
           
            g.drawArc(102,115,40,25,0,-180); 
            g.drawString("Changing Face", 80,175);
        }
    So the class holding the above functions must extend the JFrame, fair enough, but is it possible to add this kind of content to a JPanel? Main problem I have, is that the area im drawing "things" in is a 660 x 430 space within a rather crowded window, and the size of the image may vary (its mapping software) so I dont want to start drawing over other widgets?

    any pointers?

    thanks

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    This could be one of the way that you define your own JComponent for your graphics object.
    Java Code:
    public class SmilePanel extends JComponent {
    	
    	
    	public void paint(Graphics g) {
    		super.paint(g);
    		g.setColor(Color.red);
    		g.drawOval(85, 75, 75, 75);
    		g.setColor(Color.blue);
    		g.drawOval(100, 95, 10, 10);
    		g.drawOval(135, 95, 10, 10);
    
    		g.drawArc(102, 115, 40, 25, 0, -180);
    		g.drawString("Changing Face", 80, 175);
    	}
    	
    	@Override
    	public Dimension getPreferredSize() {
    		Dimension dimension = new Dimension(200,200); // Change to required size
    		return dimension;
    	}	
    }
    ...
    getContentPane().add(new SmilePanel());
    dont worry newbie, we got you covered.

Similar Threads

  1. Classes in graphics
    By CyberFrog in forum New To Java
    Replies: 0
    Last Post: 04-02-2008, 09:11 PM
  2. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  3. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM
  4. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 01:45 AM
  5. Updating Graphics
    By Greedful in forum Java 2D
    Replies: 2
    Last Post: 07-20-2007, 07:12 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
  •