![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#3 |
|
Newbie
|
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.
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#6 |
|
Professional Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |