Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Help with graphics operations

Thread view: 
Daniele Simonelli - 14 Sep 2004 19:06 GMT
My problem is as follows: in a method that receives as parameters a Shape
object and a Graphics object,
I must draw two copies of the Shape, with an offsrt of a few pixels between
the two copies on the x axis.
The first copy of the Shape is red, the second is green, and I desire that
the pixels were black in the zones where
the two copies overlap.
In the implementation a draw the first Shape with the Graphics2D.draw(Shape)
method, the I move the origin
of the graphics system with the Graphics2D.translate(int,int) method.
Then, my idea was to use AlphaComposite objects to draw two times the second
copy of the Shape,
one time in black only inside the zones already covered by the red Shape and
the other time in green
only in the zones not covered by the red Shape.
Unfortunately, whatever thing I try to do the best result I score is to draw
a black Shape next to the red one,
with black in the overlapping zones (or ever worst results).
Can someone give me some advice?
Thanks to all.
Daniele Simonelli
Paul Lutus - 14 Sep 2004 19:45 GMT
> My problem is as follows: in a method that receives as parameters a Shape
> object and a Graphics object,
[quoted text clipped - 15 lines]
> with black in the overlapping zones (or ever worst results).
> Can someone give me some advice?

I ask you to submit the text of your post to your Java compiler. What
happens? Can the Java compiler make sense of it? For contrast, can your
Java compiler make sense of your source code? Please apply the same rules
here.

Signature

Paul Lutus
http://www.arachnoid.com

Daniele Simonelli - 15 Sep 2004 21:36 GMT
Sorry, here is an idea of the code:

// imports omitted for brevity
public class Image3D {
       private Shape shape;

       public Image3D(Shape sh) {
               shape = sh;
       }

       public void disegna(Graphics gra) {

               Graphics2D gra2 = (Graphics2D)gra;
               gra2.setColor(Color.RED);
               gra2.draw(shape);
               gra2.translate(2,0);
               gra2.setColor(Color.BLACK);

gra2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
               gra2.draw(shape);
               gra2.setColor(Color.GREEN);

gra2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER));
               gra2.draw(shape);
       }
}

public class ComponenteDiProva extends JComponent {
       private Image3D imma;
       public ComponenteDiProva() {
               setOpaque(true);
               setBackGround(Color.WHITE);
               setPreferredSize(new Dimension(40,20));
               imma = new Image3D(new Ellipse2D.Float(0,0,30,20));
       }
       public void paintComponent(Graphics gr) {
               Graphics2D gr2 = (Graphics2D)gr.create();
               if (isOpaque()) {
                       gr2.setColor(getBackground());
                       gr2.fillRect(0,0,getWidth(),getHeight());
               }

               gr2.dispose();
       }

}

public class Prova extends JFrame {
       public Prova() {
           super();
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Container contents = getContentPane();
           contents.add(new ComponenteDiProva());
           pack();
       }
       public static void main(String[] args) {
           Prova test = new Prova();
           test.show();
       }
}

The result is a black Shape over a red Shape.
Where am I wrong?
Thank you.
Daniele Simonelli

> > My problem is as follows: in a method that receives as parameters a Shape
> > object and a Graphics object,
[quoted text clipped - 24 lines]
> Paul Lutus
> http://www.arachnoid.com
Daniele Simonelli - 15 Sep 2004 22:01 GMT
>         public void paintComponent(Graphics gr) {
>                 Graphics2D gr2 = (Graphics2D)gr.create();
[quoted text clipped - 5 lines]
>                 gr2.dispose();
>         }

SORRY, I forgot to write the most important line!
Here is the correct code:

        public void paintComponent(Graphics gr) {
                Graphics2D gr2 = (Graphics2D)gr.create();
                if (isOpaque()) {
                        gr2.setColor(getBackground());
                        gr2.fillRect(0,0,getWidth(),getHeight());
                }
                imma.disegna(g2);
                gr2.dispose();
        }

I apologize for my absent-mindedness!

Daniele Simonelli
Andrew Thompson - 16 Sep 2004 02:11 GMT
> Sorry, here is an idea of the code:

try this instead..  note that this example
is 'self-contained' in that you should be able
to copy/paste it into a Java file, compile it
and run it.  The reason I mention that particularly
is that I would appreciate if you added the extra
lines/changes to makes your examples the same (it
would have saved me some time..)

<sscce>
// imports omitted for brevity
// why, they only add three lines!
import java.awt.*;
import javax.swing.*;
import java.awt.geom.Ellipse2D;

class Image3D {
       private Shape shape;
       BasicStroke stroke;

       public Image3D(Shape sh) {
               shape = sh;
               // make the stroke *huge* so we
               // can see what's going on
               stroke = new BasicStroke(10);
       }

       public void disegna(Graphics gra) {

               Graphics2D gra2 = (Graphics2D)gra;
               gra2.setStroke( stroke );

               gra2.setColor(Color.RED);
               gra2.draw(shape);
               gra2.translate(6,0);
               gra2.setColor(Color.GREEN);

gra2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.5f));
               gra2.draw(shape);
       }
}

class ComponenteDiProva extends JComponent {
       private Image3D imma;
       public ComponenteDiProva() {
               setOpaque(true);
               //are you just making this up Danielle?
               //  setBackGround(Color.WHITE);
               setBackground(Color.WHITE);
               setPreferredSize(new Dimension(40,20));
               imma = new Image3D(new Ellipse2D.Float(5,5,50,40));
       }
        public void paintComponent(Graphics gr) {
                Graphics2D gr2 = (Graphics2D)gr.create();
                if (isOpaque()) {
                        gr2.setColor(getBackground());
                        gr2.fillRect(0,0,getWidth(),getHeight());
                }
                // and you miss-spelt g2 here..
                imma.disegna(gr2);
                gr2.dispose();
        }
}

class Prova extends JFrame {
       public Prova() {
           super();
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Container contents = getContentPane();
           contents.add(new ComponenteDiProva());
           pack();
           setSize(100,100);
       }

       public static void main(String[] args) {
           Prova test = new Prova();
           //test.show();  //deprecated in 1.5, use..
           test.setVisible(true);
       }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Daniele Simonelli - 16 Sep 2004 17:36 GMT
> try this instead..  note that this example
> is 'self-contained' in that you should be able
[quoted text clipped - 3 lines]
> lines/changes to makes your examples the same (it
> would have saved me some time..)

Thank you very much, the result is exactly what I was looking for!
Sorry for the problems, I did actually try to write the code on the moment,
but I was too much in a hurry. Next time I'll be more accurate.

Daniele Simonelli
Andrew Thompson - 16 Sep 2004 17:54 GMT
> Thank you very much, the result is exactly what I was looking for!

You're welcome.

> Sorry for the problems, I did actually try to write the code on the moment,
> but I was too much in a hurry. Next time I'll be more accurate.

Now that makes me even happier.   :-)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.