Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 2nd, 2008, 6:01 PM   #1
truBlu
Newbie
 
Join Date: Sep 2007
Location: New Jersey
Posts: 13
Rep Power: 0 truBlu is on a distinguished road
Send a message via MSN to truBlu
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.
truBlu is offline   Reply With Quote
Old Feb 2nd, 2008, 8:28 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Feb 3rd, 2008, 12:53 AM   #3
truBlu
Newbie
 
Join Date: Sep 2007
Location: New Jersey
Posts: 13
Rep Power: 0 truBlu is on a distinguished road
Send a message via MSN to truBlu
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.
truBlu is offline   Reply With Quote
Old Feb 3rd, 2008, 11:31 AM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Feb 3rd, 2008, 1:32 PM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: olympia,WA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Feb 3rd, 2008, 2:49 PM   #6
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: 2D Graphics in Java

@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:
result.PNG
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
First Java Program duale2005 Java 3 May 22nd, 2006 5:17 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:28 AM.

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