diff options
author | Kenneth Russel <[email protected]> | 2005-06-30 19:30:25 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2005-06-30 19:30:25 +0000 |
commit | 0afd53cb292d371c562aac7bb0e162ee2ff40bac (patch) | |
tree | f1c260f62b6659b9fb5fa6d96e4c8723e4fc55d7 /src/demos | |
parent | bfef7ff416742912f7859792475f202e257b3bce (diff) |
Fixed Issue 168: Add support for transparency in GLJPanel
Obey setOpaque() when selecting buffered image type. User must still
request alpha bits in the GLCapabilities. Not supported on all
back-end renderers; for example, Microsoft GDI renderer does not
implement alpha bits when rendering to DIB sections. Added examples of
use in JGears and JRefract demos.
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/trunk@96 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4
Diffstat (limited to 'src/demos')
-rw-r--r-- | src/demos/jgears/JGears.java | 31 | ||||
-rwxr-xr-x | src/demos/jrefract/JRefract.java | 43 |
2 files changed, 62 insertions, 12 deletions
diff --git a/src/demos/jgears/JGears.java b/src/demos/jgears/JGears.java index ee0690c..59e6c79 100644 --- a/src/demos/jgears/JGears.java +++ b/src/demos/jgears/JGears.java @@ -2,6 +2,7 @@ package demos.jgears; import java.awt.*; import java.awt.event.*; +import javax.swing.*; import net.java.games.jogl.*; @@ -14,11 +15,33 @@ import net.java.games.jogl.*; public class JGears { public static void main(String[] args) { - Frame frame = new Frame("Gear Demo"); - GLJPanel drawable = GLDrawableFactory.getFactory().createGLJPanel(new GLCapabilities()); - + JFrame frame = new JFrame("Gear Demo"); + frame.getContentPane().setLayout(new BorderLayout()); + GLCapabilities caps = new GLCapabilities(); + caps.setAlphaBits(8); + final GLJPanel drawable = GLDrawableFactory.getFactory().createGLJPanel(caps); + drawable.setOpaque(false); drawable.addGLEventListener(new GearRenderer()); - frame.add(drawable); + + JPanel gradientPanel = new JPanel() { + public void paintComponent(Graphics g) { + ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE, + getWidth(), getHeight(), Color.DARK_GRAY)); + g.fillRect(0, 0, getWidth(), getHeight()); + } + }; + gradientPanel.setLayout(new BorderLayout()); + frame.getContentPane().add(gradientPanel, BorderLayout.CENTER); + gradientPanel.add(drawable, BorderLayout.CENTER); + + final JCheckBox checkBox = new JCheckBox("Transparent", true); + checkBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + drawable.setOpaque(!checkBox.isSelected()); + } + }); + frame.getContentPane().add(checkBox, BorderLayout.SOUTH); + frame.setSize(300, 300); final Animator animator = new Animator(drawable); frame.addWindowListener(new WindowAdapter() { diff --git a/src/demos/jrefract/JRefract.java b/src/demos/jrefract/JRefract.java index f47cfda..e842eba 100755 --- a/src/demos/jrefract/JRefract.java +++ b/src/demos/jrefract/JRefract.java @@ -83,13 +83,16 @@ public class JRefract { inner.setClosable(true); inner.setVisible(true); - final GLJPanel canvas = GLDrawableFactory.getFactory().createGLJPanel(new GLCapabilities()); + GLCapabilities caps = new GLCapabilities(); + if (!bunny) { + caps.setAlphaBits(8); + } + final GLJPanel canvas = GLDrawableFactory.getFactory().createGLJPanel(caps); if (bunny) { canvas.addGLEventListener(new Listener()); } else { canvas.addGLEventListener(new GearRenderer()); } - canvas.setSize(512, 512); canvas.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { canvas.requestFocus(); @@ -106,12 +109,36 @@ public class JRefract { }); inner.getContentPane().setLayout(new BorderLayout()); - inner.getContentPane().add(canvas, BorderLayout.CENTER); - inner.getContentPane().add(new JButton("West"), BorderLayout.WEST); - inner.getContentPane().add(new JButton("East"), BorderLayout.EAST); - inner.getContentPane().add(new JButton("North"), BorderLayout.NORTH); - inner.getContentPane().add(new JButton("South"), BorderLayout.SOUTH); - inner.setSize(canvas.getSize()); + if (bunny) { + inner.getContentPane().add(canvas, BorderLayout.CENTER); + inner.getContentPane().add(new JButton("West"), BorderLayout.WEST); + inner.getContentPane().add(new JButton("East"), BorderLayout.EAST); + inner.getContentPane().add(new JButton("North"), BorderLayout.NORTH); + inner.getContentPane().add(new JButton("South"), BorderLayout.SOUTH); + } else { + // Provide control over transparency of gears background + canvas.setOpaque(false); + JPanel gradientPanel = new JPanel() { + public void paintComponent(Graphics g) { + ((Graphics2D) g).setPaint(new GradientPaint(0, 0, Color.WHITE, + getWidth(), getHeight(), Color.DARK_GRAY)); + g.fillRect(0, 0, getWidth(), getHeight()); + } + }; + gradientPanel.setLayout(new BorderLayout()); + inner.getContentPane().add(gradientPanel, BorderLayout.CENTER); + gradientPanel.add(canvas, BorderLayout.CENTER); + + final JCheckBox checkBox = new JCheckBox("Transparent", true); + checkBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + canvas.setOpaque(!checkBox.isSelected()); + } + }); + inner.getContentPane().add(checkBox, BorderLayout.SOUTH); + } + + inner.setSize(512, 512); desktop.add(inner); return inner; |