How can I draw individual pixels in Java without using graphics classes? - java

I don't want to use the graphics/drawing libraries. The only solution I can think of is creating thousands of 1x1 jButtons (which would obviously be impractical). Is there a better solution?

The only way to draw "without using graphics functions" is populate a raw Raster with the byte values appropriate to the color map for the actual image. Then you can use a graphics primitive to render the resulting image in to a window.
Take a look BufferedImage, and work your way back from there.

You could do this by creating a own component by extending for example JPanel and overriding the paintComponent method.
Why don't you want to use the graphics functions provided by Java?

How do you expect displaying the picture onto the screen without using a graphics library? Are you going to write a native method to display pixels onto the computer screen?

Related

Is there a function to find color of point in AWT/SWING

So, I've created this little maze building algorithm (trying to understand how Java AWT & Swing works).
Now, I've used the Graphics (paint method) to create the maze itself, meaning I didn't store it anywhere.
Now I wanted to know if there is a way to know if at given a specific point on the JFrame, is there a way to tell which color it is?
If so, how can I do it?
Let's say for example in my maze I want to see what color is on the PURPLE dot (seeing if it's a wall there basically).
Is there a way to do it, or do I have to do a work-around that?
Thanks.
There is no direct way to a Component's image buffer. But there are two indirect ways.
(Better) Create an Image (Either BufferedImage or Component.createImage) with the same height and width as your Component. You can then run myComponent.paint(myBuffer.getGraphics());. This will draw the component on the image, and from there you can get the pixel Color you are interested in
(Alternative) You can use java.awt.Robot to capture the screen in an Image. Use this image similar as described above

Why use ImageIcon rather than Image?

In what instance would I want to use ImageIcon to represent a picture file rather than an Image object? I've been searching and I've seen people say that you would use an ImageIcon object when dealing with images that will be part of the GUI, but I still don't understand the implications of this. In other words, what is the actual difference between the two object types and what situations are they each suited for?
Image is an object that represents a bitmap: an array of pixels of different colors.
Icon is an object that can draw a rectangular piece of graphics. Since it is an interface (and a simple one too), you can imagine many different types of icons: drawing predefined vector graphics, generating images on the fly etc. Therefore it is a useful abstraction and is used by Swing components (buttons, labels).
ImageIcon is an object that IS an Icon, but HAS-A Image. That is - it draws graphics based on a specific image.
When you say "why should I be using an ImageIcon instead of Image" you miss the point: in fact you are using an Image either way.
An Image is an object representing the data model of a picture. An ImageIcon is a Swing component that draws an Image on the screen, and you have to provide it with the appropriate Image to draw (either by passing in an existing Image or by giving it enough information to find and load the image).
The relationship is similar to that between a String and a JTextField; one is the representation of the data, and the other is the graphical component that draws it on the screen.
The implementation is to not hold up the Swing thread.
Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.
Basically, then you can set an ImageIcon for a button without actually forcing it to be loaded beforehand.
This can be seen by having a very large icon and setting the Frame's icon to this image. Once set visible, it may take a few seconds to actually appear.

Java Graphics2D Transparent Rendering

I am making a simple 2D game in java... I am using java's Graphics2D to output to my screen. I want to be able to draw things that are partially transparent, can I do this? Or will I have to change my entire project to rendering using some other method
Set Graphics2D.setComposite(AlphaComposite) prior to drawing the Image.

Java backbuffer and animations

I'm creating a simple 2D game in java. I've only done this in C++ with the Windows API so far. In java, I'm getting a Graphics object by extending JFrame and calling this.getContentPane().getGraphics(). With this object, I'm drawing everything to the screen, every frame. This raises a few questions:
Backbuffer: I'm not getting any flickering effects, while I'm not using a backbuffer (I'm drawing directly on the Graphics object). Why is this? Does java has a built-in backbuffer or something?
Animations: I'm used to put all animation parts in a single sprite sheet, like in this example:
http://www.envygames.com/share/sample_animation.jpg
Now, someone has told me that you can just draw animated .gif's and java will draw these independent of the game loop. I've tried this out and it doesn't seem to work. Is this true or am I also supposed to use these sprite sheets in java?
Thanks
Yes, Java has a double buffer rendering strategy that can be switched on and off...
Java: how to do double-buffering in Swing?
About the animated gifs, I think it is right, but you may have to put them in the appropriate container (maybe as the icon of a JLabel?).
getting a Graphics object by extending JFrame and
calling this.getContentPane().getGraphics().
don't painting directly to the JFrame for Custom Painting you have to look for JLabel that allows painting everything, another choise will be extending JCompoments, or JPanel for that
for painting in the Swing you have to look for paintComponent(Graphics g), not paint(Graphics g), because this method are lots of time used in examples and ditributed on some of Java ExamplesDepots, that's wrong method with possible lacks

Generate icon images in Java

I have a set of icon images of various sizes (16x16, 24x24, 32x32) and base colours (red, green, blue, cyan, magenta, yellow). The images are pretty basic geometric patterns + drop shadow, so my gut feeling is that it should be pretty straightforward to replace the files with an icon factory that can generate images given a base colour.
However, subclassing the Image class seems to be a lot of work - is there a better way? Just to clarify - I'm not interested in generating image files, I intend to use the Image objects directly.
What’s wrong with BufferedImage? It will give you a WritableRaster if you ask nicely. :)
If you intend to paint the images to the screen, there is a better way. Write your class to extend the Icon interface, and use the paint method to actually use the Graphics2D APIs to draw the icon. You can pass the color to the constructor. I've done this before, and it works beautifully.

Categories