a questo punto prova con una classe di test..
memorizza i colori dei pixel in una matrice e poi disegnali..
così vediamo cosa va a "pescare"
a questo punto prova con una classe di test..
memorizza i colori dei pixel in una matrice e poi disegnali..
così vediamo cosa va a "pescare"
Write Once, Run Everywhere.
Write Less, Do More.
E' normale che con questo codice:
Venga fuori questo:codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestColor extends JPanel { private Robot robot; private Color[][] pixel = new Color[100][100]; private static JButton button; public TestColor() { try { robot = new Robot(); } catch (Exception e) { System.exit (1); } button = new JButton ("Clicca!"); button.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) { pixel[i][j] = robot.getPixelColor (i, j); } repaint(); } } ); } public void paintComponent (Graphics g) { super.paintComponent (g); Point point = getLocationOnScreen(); for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) { g.setColor (pixel[i][j]); g.drawRect (i + point.x, j + point.y, i + 1 + point.x, j + 1 + point.y); } } public static void main (String[] args) { TestColor panel = new TestColor(); JFrame frame = new JFrame ("Test Color"); frame.setSize (640, 480); frame.setLocationRelativeTo (null); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.add (panel); frame.add (button, BorderLayout.NORTH); frame.setVisible (true); } }
?![]()
sì, sei andato a memorizzare nella matrice i punti che vanno da 0 a 100 in x e y (i for nel costruttore)
e poi li hai disegnati nella finestra con un offset...
prima ti serve la posizione degli oggetti nella tua finestra,
da qui l'offset: Posizione Finestra + Posizione Oggetto
e poi metodo del Robot..
Write Once, Run Everywhere.
Write Less, Do More.
Ecco, con questo codice funziona.
Il metodo della classe Robot va ad associare i colori dei Pixel a partire dalla locazione del Jpanel SUL Monitor
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestColor extends JPanel implements ActionListener { private Robot robot; private Color[][] pixel = new Color[300][300]; private JButton button; private int xDelPanel =0; private int yDelPanel =0; JFrame frame; public TestColor() { /*for (int i = 0; i < 300; i++) for (int j = 0; j < 300; j++) { pixel[i][j] = Color.white; }*/ frame = new JFrame ("Test Color"); frame.setSize (640, 480); frame.setLocation(50, 100); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); try { robot = new Robot(); } catch (Exception e) { System.exit (1); } button = new JButton ("Clicca!"); button.addActionListener(this); frame.add (this); frame.add (button, BorderLayout.NORTH); frame.setVisible (true); } public void paintComponent (Graphics g) { super.paintComponent (g); for (int i = 0; i < 300; i++) for (int j = 0; j < 300; j++) { g.setColor (pixel[i][j]); g.fillRect(i, j , 1, 1); } } public static void main (String[] args) { new TestColor(); } public void creaJFrameTEST(){ JFrame jf ; jf = new JFrame("Ciaoooo"); jf.setBounds(this.xDelPanel, this.yDelPanel, 50, 50); jf.setVisible(true); } public void actionPerformed(ActionEvent arg0) { this.xDelPanel= (int)this.getLocationOnScreen().getX(); this.yDelPanel= (int)this.getLocationOnScreen().getY(); //creo una finestra per testare il getPixelColor this.creaJFrameTEST(); for (int i = 0; i < 300; i++) for (int j = 0; j < 300; j++){ pixel[i][j] = robot.getPixelColor (i+this.xDelPanel, j+this.yDelPanel); } repaint(); } }
Write Once, Run Everywhere.
Write Less, Do More.