use of java.awt.Graphics in project jdk8u_jdk by JetBrains.
the class ValidWbmpTest method main.
public static void main(String[] args) {
try {
String[] formats = { "JPEG", "PNG", "BMP" };
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = img.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.black);
g.fillRect(10, 10, 80, 80);
ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("WBMP").next();
if (ir == null) {
throw new RuntimeException("No readers for WBMP format!");
}
for (int i = 0; i < formats.length; i++) {
System.out.println("Test " + formats[i] + " stream...");
boolean passed = false;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, formats[i], baos);
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = null;
iis = ImageIO.createImageInputStream(bais);
ir.setInput(iis);
try {
BufferedImage res = ir.read(0);
} catch (IIOException e) {
StackTraceElement[] stack = e.getStackTrace();
if (ir.getClass().getName().equals(stack[0].getClassName()) && "readHeader".equals(stack[0].getMethodName())) {
passed = true;
}
} catch (Throwable t) {
t.printStackTrace();
}
if (!passed) {
throw new RuntimeException("Test failed!");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unexpected exception. Test failed.");
}
}
use of java.awt.Graphics in project jdk8u_jdk by JetBrains.
the class bug8057791 method main.
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
final int listWidth = 50;
final int listHeight = 50;
final int selCellIndex = 0;
JList<String> list = new JList<String>();
list.setSize(listWidth, listHeight);
DefaultListModel<String> listModel = new DefaultListModel<String>();
listModel.add(selCellIndex, "E");
list.setModel(listModel);
list.setSelectedIndex(selCellIndex);
BufferedImage img = new BufferedImage(listWidth, listHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
list.paint(g);
g.dispose();
Rectangle cellRect = list.getCellBounds(selCellIndex, selCellIndex);
HashSet<Color> cellColors = new HashSet<Color>();
int uniqueColorIndex = 0;
for (int x = cellRect.x; x < (cellRect.x + cellRect.width); x++) {
for (int y = cellRect.y; y < (cellRect.y + cellRect.height); y++) {
Color cellColor = new Color(img.getRGB(x, y), true);
if (cellColors.add(cellColor)) {
System.err.println(String.format("Cell color #%d: %s", uniqueColorIndex++, cellColor));
}
}
}
Color selForegroundColor = list.getSelectionForeground();
Color selBackgroundColor = list.getSelectionBackground();
if (!cellColors.contains(new Color(selForegroundColor.getRGB(), true))) {
throw new RuntimeException(String.format("Selected cell is drawn without selection foreground color '%s'.", selForegroundColor));
}
if (!cellColors.contains(new Color(selBackgroundColor.getRGB(), true))) {
throw new RuntimeException(String.format("Selected cell is drawn without selection background color '%s'.", selBackgroundColor));
}
}
});
} catch (UnsupportedLookAndFeelException | InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
use of java.awt.Graphics in project jdk8u_jdk by JetBrains.
the class bug7181438 method createBufferedImage.
private static BufferedImage createBufferedImage() {
final BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
final Graphics bg = bi.getGraphics();
//Black color and alpha = 0
bg.setColor(new Color(0, 0, 0, 0));
bg.fillRect(0, 0, SIZE, SIZE);
bg.dispose();
return bi;
}
use of java.awt.Graphics in project jdk8u_jdk by JetBrains.
the class HiDPIRobotScreenCaptureTest method main.
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
return;
}
Frame frame = new Frame();
frame.setBounds(40, 30, 400, 300);
frame.setUndecorated(true);
Panel panel = new Panel(new BorderLayout());
Canvas canvas = new Canvas() {
@Override
public void paint(Graphics g) {
super.paint(g);
int w = getWidth();
int h = getHeight();
g.setColor(COLORS[0]);
g.fillRect(0, 0, w / 2, h / 2);
g.setColor(COLORS[1]);
g.fillRect(w / 2, 0, w / 2, h / 2);
g.setColor(COLORS[2]);
g.fillRect(0, h / 2, w / 2, h / 2);
g.setColor(COLORS[3]);
g.fillRect(w / 2, h / 2, w / 2, h / 2);
}
};
panel.add(canvas);
frame.add(panel);
frame.setVisible(true);
Robot robot = new Robot();
robot.waitForIdle();
Thread.sleep(200);
Rectangle rect = canvas.getBounds();
rect.setLocation(canvas.getLocationOnScreen());
BufferedImage image = robot.createScreenCapture(rect);
frame.dispose();
int w = image.getWidth();
int h = image.getHeight();
if (w != frame.getWidth() || h != frame.getHeight()) {
throw new RuntimeException("Wrong image size!");
}
if (image.getRGB(w / 4, h / 4) != COLORS[0].getRGB()) {
throw new RuntimeException("Wrong image color!");
}
if (image.getRGB(3 * w / 4, h / 4) != COLORS[1].getRGB()) {
throw new RuntimeException("Wrong image color!");
}
if (image.getRGB(w / 4, 3 * h / 4) != COLORS[2].getRGB()) {
throw new RuntimeException("Wrong image color!");
}
if (image.getRGB(3 * w / 4, 3 * h / 4) != COLORS[3].getRGB()) {
throw new RuntimeException("Wrong image color!");
}
}
use of java.awt.Graphics in project jdk8u_jdk by JetBrains.
the class MultiResolutionSplashTest method getScaleFactor.
static float getScaleFactor() {
final Dialog dialog = new Dialog((Window) null);
dialog.setSize(100, 100);
dialog.setModal(true);
final float[] scaleFactors = new float[1];
Panel panel = new Panel() {
@Override
public void paint(Graphics g) {
float scaleFactor = 1;
if (g instanceof SunGraphics2D) {
scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
}
scaleFactors[0] = scaleFactor;
dialog.setVisible(false);
}
};
dialog.add(panel);
dialog.setVisible(true);
dialog.dispose();
return scaleFactors[0];
}
Aggregations