1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package experiments;
26 
27 import java.awt.BorderLayout;
28 import java.awt.Canvas;
29 import java.awt.Color;
30 import java.awt.Frame;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.Transparency;
34 import java.awt.color.ColorSpace;
35 import java.awt.event.WindowAdapter;
36 import java.awt.event.WindowEvent;
37 import java.awt.image.BufferedImage;
38 import java.awt.image.ColorModel;
39 import java.awt.image.ComponentColorModel;
40 import java.awt.image.ComponentSampleModel;
41 import java.awt.image.DataBuffer;
42 import java.awt.image.DataBufferFloat;
43 import java.awt.image.Raster;
44 import java.awt.image.SampleModel;
45 import java.awt.image.WritableRaster;
46 
47 public class GrayFloatFrame {
48 
49 
50      static void main(String[] args) {
51         final int width = 1024;
52         final int height = 1024;
53 
54         SampleModel floatSampleModel = new ComponentSampleModel(DataBuffer.TYPE_FLOAT, width, height, 1, width, new int[]{0});
55         DataBuffer floatDataBuffer = new DataBufferFloat(width * height);
56         WritableRaster floatWritableRaster = Raster.createWritableRaster(floatSampleModel, floatDataBuffer, null);
57         ColorSpace grayColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
58         ColorModel grayColorModel = new ComponentColorModel(grayColorSpace, false, true, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
59         final BufferedImage image = new BufferedImage(grayColorModel, floatWritableRaster, true, null);
60 
61         Graphics2D g2 = image.createGraphics();
62                 g2.setColor(Color.BLACK);
63         g2.fillRect(0, 0, width, height);
64         g2.setColor(Color.RED);
65         g2.drawLine(0, 0, width, height);
66         g2.drawLine(width, 0, 0, height);
67         g2.drawOval(width / 4, height / 4, width / 2, height / 2);
68         g2.dispose();
69 
70         Frame f = new Frame("GrayFloatFrame");
71         f.addWindowListener(new WindowAdapter() {
72             public void windowClosing(WindowEvent we) {
73                 System.exit(0);
74             }
75         });
76         f.setLayout(new BorderLayout());
77         Canvas c = new Canvas() {
78             public void paint(Graphics g) {
79                 g.drawImage(image, 0, 0, null);
80             }
81         };
82         c.setSize(width, height);
83         f.add(c, BorderLayout.CENTER);
84         f.pack();
85         f.show();
86     }
87 }