How to convert GUI component's graphics to int[]or byte[]?
- From: Aki Laukkanen <aki.laukkanen@xxxxxxxxxxx>
- Date: Fri, 30 Sep 2005 15:29:01 +0300
Has anyone ran into the problem of trying to get a GUI component's graphics converted into an array of ints (or, even better, bytes)?
I've tried doing this by having the component paint() its graphics on a java.awt.image.BufferedImage and then using a java.awt.image.PixelGrabber' s getPixels() to extract the array.
For some reason, no matter what the component GUI looks like, I get an array full of -16777216's.
Here's an SSCCE demonstrating what I'm trying to do:
<SSCCE>
import java.awt.image.PixelGrabber; import java.awt.Frame; import java.awt.Button; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.BorderLayout; import java.awt.image.BufferedImage; import java.awt.Graphics; import java.awt.Color; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
/** * <p>Title: PixelGrabbing Demo</p> * <p>Description: Simple test construct to attempt grabbing graphics * pixels from a GUI component</p> * @author Aki Laukkanen * @version 0.0 */
public class PixelGrabbingDemo {
/**
* Initial width of application GUI
* */
private static final int INIT_WIDTH = 90;/** * Initial height of application GUI * */ private static final int INIT_HEIGHT = 120;
private Frame appFrame; private TestGraphicPanel testPanel; private Button testButton; private PixelGrabber testGrabber; private int[] grabbedPixels;
public PixelGrabbingDemo() {}/** * Test harness * @param args not used * */
public static void main(String[] args) {
PixelGrabbingDemo theDemo = new PixelGrabbingDemo();
theDemo.initGUI();
}/** * Populate appFrame and set it visible. * */
private void initGUI() {
appFrame = new Frame("Pixel Grabbing Test");
testButton = new Button("Press to test");
testButton.addActionListener(new TestButtonListener());
testPanel = new TestGraphicPanel();
appFrame.setLayout(new BorderLayout());
appFrame.addWindowListener(new TestFrameListener());
appFrame.add(testPanel, BorderLayout.CENTER);
appFrame.add(testButton, BorderLayout.SOUTH);
appFrame.pack();
appFrame.setSize(INIT_WIDTH, INIT_HEIGHT);
testPanel.setVisible(true);
testPanel.setIgnoreRepaint(false);
testPanel.repaint();
appFrame.validate();
appFrame.setVisible(true);
} private class TestButtonListener implements ActionListener {
/**
* Simplified response to button click, validity checks etc.
* omitted.
*
* @param e ActionEvent detected
*/
public void actionPerformed(ActionEvent e) {try {
boolean grabbingSucceeded;
BufferedImage imageOfPanel = new BufferedImage(testPanel.getWidth(),testPanel.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics imageGraphics = imageOfPanel.getGraphics();
testPanel.paint(imageGraphics);
testGrabber = new PixelGrabber(imageOfPanel,0,0,testPanel.getWidth(),testPanel.getHeight(),true);
try {
grabbingSucceeded = testGrabber.grabPixels();
}
catch (InterruptedException ex) {
ex.printStackTrace();
grabbingSucceeded = false;
} if (grabbingSucceeded) {
System.out.println("Pixels grabbed successfully.");
grabbedPixels = (int[]) testGrabber.getPixels(); //Print out int value of each pixel on each row.
int pixel = 0;
for (int i = 0; i < testGrabber.getHeight(); i++) {
for (int j = 0; j < testGrabber.getWidth(); j++) {
System.out.print(Integer.toString(grabbedPixels[pixel])+',');
}
System.out.print('\n');
}
}
else {
System.out.println("Grabbing failed.");
System.exit(1);
}
}
catch (Exception exc) {
exc.printStackTrace();
}
}
} class TestFrameListener implements WindowListener {
/**
* Used to detect when user closes the test window.
* */
//Obligatory WindowListener boilerplate
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
//React to window closing
public void windowClosing(WindowEvent e) {
System.exit( -1);
}
} class TestGraphicPanel extends Panel { /**
* Paints alternating black and white quadrants
* @param g Graphics configuration to paint on
* */ public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.black);
g.fillRect(0,0,(getWidth()/2),(getHeight()/2));
g.fillRect((getWidth()/2),(getHeight()/2),getWidth(),getHeight());
}
}}
</SSCCE>
-- -Aki "Sus" Laukkanen .
- Follow-Ups:
- Re: How to convert GUI component's graphics to int[]or byte[]?
- From: Andrew Thompson
- Re: How to convert GUI component's graphics to int[]or byte[]?
- Prev by Date: Re: Mouse Cursor with hourglass and pointer
- Next by Date: Re: Coding Challenge
- Previous by thread: How to reorder JTable
- Next by thread: Re: How to convert GUI component's graphics to int[]or byte[]?
- Index(es):