Re: Has working with raster graphics always been this complicated?
jsalzman@gmail.com wrote:
Hello,
I'm not new to programming, but I'm new to Java, at least as far as
programming an entire applet. I'm familiar with looking through a
language's API to learn more about developing an application using
available methods. I've done it plenty of times.
When I learn a new programming language, I do it by having a goal in
mind. Something that I want to program to achieve a task. in doing so,
I develop an understanding of the language.
I just can't, for the life of me, figure out how to manipulate raster
images the way I want. I find references to Image objects, Graphics
objects, BufferedImage objects and such. You can do things with Image
objects that you can't do with Graphics objects. There's plenty of
online help for Java, but help through API docs and tutorials don't
seem to spell out many details. There seems to be more useful
tutorials, describing the theory behind an API call, for Visual Basic
than Java out there.
I'm trying to figure out the true differences between an Image and
Graphics object. From what I can find, You can draw primitives on
Graphics, but not Image. If you can draw primitives on Images, I can't
figure out the details.
Here's what I'm trying to do. This is the "demo" project I'm making to
help me learn graphics handling concepts in Java. I wanted to make a
mouse driven application that reveals a hidden picture. I have the
picture graphics to be revealed, in the form of GIF files. I have a
series of "covering" images that I want to use to cover the pictures
in the form of GIF files (in the shape of doors, curtains, etc.).
Here's my concept. I have code working that can get the door image to
draw over the picture image. I'm having trouble with the code that
will do the "reveal". I want to animate a box that widens from the
middle of the cover picture to the edges (simulating a barn door
wipe). My first thought would be to paint 1px wide box in a
transparent color and redraw the box to as wide as the graphic. Sounds
easy enough in a drawing program like Photoshop. I can't figure out
how to paint in a transparent color on a raster image object and then
superimpose that image on the picture making the picture visible under
those transparent pixels.
I'm not asking anyone to write the entire program for me. The way I'm
trying to accomplish the task may be overkill, but I'm doing it this
way to become familiar with certain concepts. I'm looking for working
examples of code that'll show me how to draw transparent pixels on a
raster image, overlay the raster on another image with the image
showing through the transparent spot, and draw primitives on a raster
image (if different from other drawing methods using rasters). Double
buffering the drawing process would be a plus, is that what I would
use a BufferedImage for?
Thanks,
Jeff
Graphics and Graphics2D should be thought of as the paint brush with
which you draw on Images, BufferedImages, Printers, etc. If an object
is something to draw on, you will be able to get a Graphics/2D object
for it.
If you load an image that has transparent pixels, when you draw that
image on top of another image, the first image will show through the
transparent pixels. There are numerous other ways to make drawings or
images transparent (see AlphaComposite). Color objects can be created
with transparency too. Below I create a BufferedImage, draw translucent
yellow pixels on the entire image. In the paintComponent(), where all
drawing on the component must take place, I draw a red and blue X on the
component and then draw the BufferedImage with its transparent pixels
over the top. The is just one simple way to do this sort of drawing.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel {
BufferedImage bi = new
BufferedImage(400,300,BufferedImage.TYPE_INT_ARGB);
public test() {
setPreferredSize(new Dimension(400,300));
// draw translucent yellow on the buffered image
Graphics2D g = bi.createGraphics();
g.setColor(new Color(255,255,0,180));
g.fillRect(0,0,bi.getWidth(),bi.getHeight());
}
public void paintComponent(Graphics g2D) {
Graphics2D g = (Graphics2D)g2D;
// draw blue and red X first
g.setColor(Color.BLUE);
BasicStroke bs = new BasicStroke(20.0f);
g.setStroke(bs);
g.drawLine(0,0,getWidth(),getHeight());
g.setColor(Color.RED);
g.drawLine(getWidth(),0,0,getHeight());
// draw translucent image
g.drawImage(bi,0,0,null);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}
--
Knute Johnson
email s/nospam/knute/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access