Results 1 to 8 of 8
  1. #1
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Unhappy n00b help with java2D api

    hi!

    I am new to java2D api and I don't really understand how to start...

    I want to make a easy program wich draw some points ... is this hard to make?

    please help me how do I draw points in java?!?! :confused::confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    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:

    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;
            ...
        }
    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? ...

    Then at the fifth page there is written about the point:

    Java 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.
    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? :O

    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

    michcio
    Last edited by michcio; 08-18-2008 at 01:49 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
        }
    }

  5. #5
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    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);
        }
    }
    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

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    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();
        }
    }
    also what does "protected" stand for and why do you use "protected" instead of public static?
    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.

  7. #7
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    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:):)

  8. #8
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

Similar Threads

  1. Use of Java2D on SWT or Draw2D graphical context
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:20 PM
  2. Java2D Line Styles
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:05 AM
  3. A demonstration of Java2D transformations
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:50 PM
  4. n00b: confused about java's RIA/Rich Client Story
    By bryancostanich in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 02:16 AM
  5. n00b at JSP - Need help
    By sylo18 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-04-2008, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •