Results 1 to 8 of 8
Thread: n00b help with java2D api
- 08-17-2008, 02:13 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 22
- Rep Power
- 0
-
Take a deep breath and relax. This beast is definitely beatable, but it's going to take an organized plan of attack. I recommend that you hold off on your current project and start learning 2D graphics from the ground up, from the source, the Sun Graphics2D tutorials which you can find here:
Trail: 2D Graphics (The Java™ Tutorials)
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI with JFC/Swing)
If you go through these tutorials in order you will in a short period of time gain all the knowledge you need to get started.
Good luck.
- 08-18-2008, 12:46 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 22
- Rep Power
- 0
thank you for your answer!!:)
ok so I have read throug this two tutorials now but I still don't understand a few things...At the fourth page in the second tutorial there is written:
I don't understand what is the "g" variable? and why should I make this method at all? Shall my painting code come where the three dots are placed? ...Java Code:To employ Java 2D API features in the application, cast the Graphics object passed into a component’s rendering method to a Graphics2D object. For example: public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... }
Then at the fifth page there is written about the point:
But I still don't understand how to output a point :( its written "and cannot be rendered." So I can not output it at all? :OJava Code:The Point2D class defines a point representing a location in (x, y) coordinate space. The term “point” in the Java 2D API is not the same as a pixel. A point has no area, does not contain a color, and cannot be rendered. Points are used to create other shapes. ThePoint2D class also includes a method for calculating the distance between two points.
Then from the other trail I now understand that I have to make a JFrame where I can output my points at but how do I output them? :confused:
cheers
michcioLast edited by michcio; 08-18-2008 at 12:49 PM.
- 08-18-2008, 09:01 PM #4
I don't understand what is the "g" variable?
Supplied by java for rendering components to screen.
and why should I make this method at all?
You override the method to do custom drawing/rendering in the enclosing class.
Shall my painting code come where the three dots are placed? ...
Yes.
how to output a point its written "and cannot be rendered." So I can not output it at all?
You may not be able to show a geometric point as such but you can draw its location on your graphic component.
I have to make a JFrame where I can output my points at but how do I output them?
Like this:
Java Code:import java.awt.*; import java.awt.geom.Ellipse2D; import javax.swing.*; public class GettingStarted extends JPanel { Point[] points = { new Point(100, 100), new Point(150, 200), new Point(300, 250), new Point(225, 350) }; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.red); for(int i = 0; i < points.length; i++) { g2.fill(new Ellipse2D.Double(points[i].x-2, points[i].y-2, 4, 4)); } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new GettingStarted()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
- 08-18-2008, 09:54 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 22
- Rep Power
- 0
thank you for your response and this code it made me understand some things much better but I still got 2 questions :o...
why do you place the points (you make this points by outputting small elipses really smart! :)) in this class "public class point extends JPanel" and what does extends JPanel stand for? SRY have never seen/used it before. I am making a program where I get a point coordinate after some counting...and now I can't place my for-loops and some another code in the "public class point extends JPanel" - class :( how do I solve that?
also what does "protected" stand for and why do you use "protected" instead of public static?
regards and thanks
michcio
- 08-18-2008, 11:14 PM #6
in this class "public class point extends JPanel" and what does extends JPanel stand for?
You are extending JPanel and overriding its paintComponent method to do custom drawing. You put this component (your graphic component) in a gui by adding it to a container. This is how we draw in java.
I am making a program where I get a point coordinate after some counting...and now I can't place my for-loops and some another code in the "public class point extends JPanel" - class how do I solve that?
You can do it in a class constructor or in a method in the class. It's your class so you can build it the way you want.
also what does "protected" stand for and why do you use "protected" instead of public static?Java Code:class MyGraphicClass extends JPanel { Point[] points; public MyGraphicClass() { points = new Point[yourNumber]; for(int i = 0; i < asYouLike; i++) { points[i] = new Point(whatEver... } protected void paintComponent(... ... } private void setPoints(Point[] points) { this.points = points; repaint(); } private void addPoint(Point p) { // add p to points array repaint(); } }
Simple answer: when overriding methods you copy the exact method signature from the api. Look up the paintComponent method in the JComponent api under the Methods Summary section.
Longer answer, when you're ready:
Defining Methods
Controlling Access to Members of a Class.
- 08-19-2008, 05:14 AM #7
Member
- Join Date
- Nov 2007
- Posts
- 22
- Rep Power
- 0
yay! I have written my program the whole night .. :D it's 5.05 in the morning atm and I'm finally done :D I will here post my code + a picture:
Java Code:import java.awt.*; import java.awt.geom.Ellipse2D; import java.math.BigInteger; import javax.swing.*; class sierpinski extends JPanel { Point[] points; public sierpinski() { BigInteger ett = new BigInteger("1"); BigInteger noll = new BigInteger("0"); int b = 0; int a = 750; BigInteger c = noll; int nr = 0; points = new Point[100000]; int stor = 100; for(int k=1; k<=stor; k++) { BigInteger d = noll; int s = a; while(c.compareTo(d)>=0){ BigInteger över = över(c,d); BigInteger[] z = new BigInteger[2]; z = över.divideAndRemainder(new BigInteger("2")); if(z[1].compareTo(noll)>0||c==noll&&b==0){ points[nr] = new Point(a,b); nr++; } if(c!=d) a = a + 2; d = d.add(ett); } c = c.add(ett); b++; a = s - 1; System.out.println(k + "/" + stor); } } public static BigInteger över(BigInteger a, BigInteger b){ BigInteger t = new BigInteger("1"); BigInteger u = a; BigInteger l = new BigInteger("1"); BigInteger k = l; while(k.compareTo(b)<=0){ t = t.multiply(k); l = l.multiply(u); u = u.subtract(new BigInteger("1")); k = k.add(new BigInteger("1")); } return l.divide(t); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.black); for(int i = 0; points[i]!=null; i++) { g2.fill(new Ellipse2D.Double(points[i].x-2, points[i].y-2, 1, 1)); } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new sierpinski()); f.setSize(1500,1020); f.setLocation(100,0); f.setVisible(true); } }
this program uses the pascal triangel to paint the sierpinski triangle :D..for each odd number in pascal's triangel it outs a black "dot" (and for each even a white one)
there was some extra work in this program because I was forced to use BigIntegers instead of normal Ints
to paint a Sierpinski triangle change the int stor = 100; to how many lines you want to paint of the triangle
and here is my result (when painting the triangel with 1000 lines ... took me 8 min. :P):

thank you for all your help!! I managed to create this program mostly thanks to all of you that helped me!!!!
REGARDS AND BIIIIG THANKS
michcio:):)
- 10-30-2008, 06:18 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Use of Java2D on SWT or Draw2D graphical context
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:20 PM -
Java2D Line Styles
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:05 PM -
A demonstration of Java2D transformations
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:50 PM -
n00b: confused about java's RIA/Rich Client Story
By bryancostanich in forum New To JavaReplies: 2Last Post: 05-02-2008, 01:16 AM -
n00b at JSP - Need help
By sylo18 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-04-2008, 07:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks