
Signature
Paul Lutus
http://www.arachnoid.com
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