Pagina 3 di 3 primaprima 1 2 3
Visualizzazione dei risultati da 21 a 24 su 24
  1. #21
    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.

  2. #22
    E' normale che con questo codice:

    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);
    	}
    }
    Venga fuori questo:



    ?

  3. #23
    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.

  4. #24
    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.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.