Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   2D Graphics in Java (http://www.programmingforums.org/showthread.php?t=15107)

truBlu Feb 2nd, 2008 7:01 PM

2D Graphics in Java
 
Here's my situation. I've been thrown into an assignment involving 2D graphics. My assignment is to draw a line. I've been reading the java docs and staring at example code and I just don't get any of it. I know that I have to use Point2D to select different points and then Line2D to draw a line segment and Graphics2D to draw it to the screen but I just don't understand how I'm to use any of the classes. I need a Graphics2D object but I can't instantialize an object because it's an abstract class. Can anyone offer me some advice or point me to a nice tutorial explaining everything?

I'm coding in Java 6.

Wizard1988 Feb 2nd, 2008 9:28 PM

Re: 2D Graphics in Java
 
What do you have so far?

This might help a bit:
http://java.sun.com/docs/books/tutor...swing/TOC.html

truBlu Feb 3rd, 2008 1:53 AM

Re: 2D Graphics in Java
 
That's the hard part. I can't accept any help from anyone on the assignment directly. It includes generous users on a programming forum. I'm just looking for a simple example of setting up a Jpanel and using Java classes to draw simple 2D objects on it.

Wizard1988 Feb 3rd, 2008 12:31 PM

Re: 2D Graphics in Java
 
Hopefully this helps a bit...

:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class JFrameExample extends JFrame{
   
    public JFrameExample()        //Constructor
    {
        super("JFrameExample");    //Set title
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400,400);    //Set size to 400 by 400
        setVisible(true);    //Make the JFrame visible
    }
   
    public void paint(Graphics g)    //This is where the painting is done
    {
        super.paintComponents(g);    //Paint components
        g.setColor(Color.GREEN);    //Change the color to green
        g.drawRect(10,50,100,100);    //Draw a rectangle
    }
   
    public static void main (String[] args){
        new JFrameExample();    //Create an instance of JFrameExample
    }
}


Also this might help you a bit: http://www.corewebprogramming.com/PDF/ch10.pdf

mrynit Feb 3rd, 2008 2:32 PM

Re: 2D Graphics in Java
 
@Wizard1988: the Window is empty for me. java 1.5, ubuntu 7.10. used command line tools to compile and run.

Wizard1988 Feb 3rd, 2008 3:49 PM

Re: 2D Graphics in Java
 
1 Attachment(s)
@mrynit: You might need a new computer... Just kidding. I am not sure why it isn't working for you. There should be a bright green(unfilled) rectangle in the top right corner.

Try adding "g.fillRect(200,250,20,20);" at the end of the paint method, it might be easier to notice, or change the color to RED "g.setColor(Color.RED);"

This is what I get:
Attachment 611


All times are GMT -5. The time now is 3:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC