Oppure

Loading
28/01/08 8:36
netarrow
ho provato ma non va con WindowListener, bisogna usare WindowFocusListener:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShowColors extends JFrame implements WindowFocusListener
{
    public ShowColors()
    {
        setTitle("Using colors");
        setSize(500,150);
        setResizable(false);
        addWindowFocusListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e) {
	repaint();
    }

    
    public void windowLostFocus(WindowEvent e) {
	/* quanto perde il focus non fare nulla */
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(new Color(255,0,0));
        g.fillRect(25,35,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,50);
        
        g.setColor(new Color(0.0f,1.0f,0.0f));
        g.fillRect(25,60,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,75);
        
        g.setColor(Color.BLUE);
        g.fillRect(25,85,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,100);
        
        Color color = Color.MAGENTA;
        g.setColor(color);
        g.fillRect(25,110,100,25);
        g.drawString("RGB values: "+color.getRed()+", "+color.getGreen()+", "+color.getBlue(),130,125);
    }
    
    public static void main(String[] args)
    {
       ShowColors finestra = new ShowColors();
    }
}
Ultima modifica effettuata da netarrow 28/01/08 8:36
aaa
28/01/08 14:13
The Lizard King
Postato originariamente da netarrow:

ho provato ma non va con WindowListener, bisogna usare WindowFocusListener:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShowColors extends JFrame implements WindowFocusListener
{
    public ShowColors()
    {
        setTitle("Using colors");
        setSize(500,150);
        setResizable(false);
        addWindowFocusListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e) {
	repaint();
    }

    
    public void windowLostFocus(WindowEvent e) {
	/* quanto perde il focus non fare nulla */
    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(new Color(255,0,0));
        g.fillRect(25,35,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,50);
        
        g.setColor(new Color(0.0f,1.0f,0.0f));
        g.fillRect(25,60,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,75);
        
        g.setColor(Color.BLUE);
        g.fillRect(25,85,100,30);
        g.drawString("Current RGB: "+g.getColor(),130,100);
        
        Color color = Color.MAGENTA;
        g.setColor(color);
        g.fillRect(25,110,100,25);
        g.drawString("RGB values: "+color.getRed()+", "+color.getGreen()+", "+color.getBlue(),130,125);
    }
    
    public static void main(String[] args)
    {
       ShowColors finestra = new ShowColors();
    }
}


Ho provato ma cambia poco... a volte appare e a volte scompare :d
aaa
29/01/08 15:01
netarrow
allora non saprei, se non si può riprodurre l'errore non so come sistemartelo.

Un problema simile l'ho avuto su linux perchè per errore usaro la versione tarocca del jre del gnu, appena ho messo l'oroginale della sun tutto ha ripreso a funziare; se anche tu stai su sistemi simili assicurati di non star usando versioni sbagliate dell'interprete.
Il comando da dare è:

update-alternatives - config java

e

update-alternatives - config javac

se invece sei su windows non saprei che altro potrebbe essere, se è come dici la scheda video il problema interessante sarebbe far provare lo stesso programma ad un altro che ha sta scheda ma ti ripeto mi fa molto strano che dia questi problemi.
Ultima modifica effettuata da netarrow 29/01/08 15:02
aaa
31/01/08 10:47
w3b
public void paint(Graphics g) 
    { 
        super.paint(g); 
        g.setColor(new Color(255,0,0)); 
        g.fillRect(25,35,100,30); 
        g.drawString("Current RGB: "+g.getColor(),130,50); 
         
        g.setColor(new Color(0.0f,1.0f,0.0f)); 
        g.fillRect(25,60,100,30); 
        g.drawString("Current RGB: "+g.getColor(),130,75); 
         
        g.setColor(Color.BLUE); 
        g.fillRect(25,85,100,30); 
        g.drawString("Current RGB: "+g.getColor(),130,100); 
         
        Color color = Color.MAGENTA; 
        g.setColor(color); 
        g.fillRect(25,110,100,25); 
        g.drawString("RGB values: "+color.getRed()+", "+color.getGreen()+", "+color.getBlue(),130,125); 
    }


ora io non vorrei fare il pignolo.. ma quì c'è un erroruccio gente..

super.paintComponent(g);

per disegnare il componente di default, POI il codice per i cosi colorati.. e solo successivamente un paint del pannello per il refresh delle info.. con un repaint();

in questo caso il codice resiste a tutto.. resize, finestre sopra altre finestre, e quello che vi pare..

:k:

(P.S.
E togliete quell'ascoltatore dalla finestra che è inutile..)
Ultima modifica effettuata da w3b 31/01/08 10:48
aaa
31/01/08 17:35
netarrow
di usare paintComponent gli abbiamo già detto, quello che ci è sfuggito è l'errore che c'è qui:

Postato originariamente da The Lizard King:

Postato originariamente da netarrow:

import java.awt.*;
import javax.swing.*;

class Display extends JPanel {
 public void paintComponent(Graphics g){
   super.paintComponent(g);
   g.drawString("CiaoATutti",5,25);
  }
 }
 }


e poi inserisci il pannello nella finestra.


Ho provato così ma mi da errore:

import java.awt.*;
import javax.swing.*;

class Display extends JPanel
{
	public Display()
	{
		paintComponent();
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawString("CiaoATutti",5,25);
	}
	public static void main(String[] args)
	{
		JFrame finestra = new JFrame("Prova");
		finestra.setSize(200,180);
		finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		finestra.add(Display);
		finestra.setVisible(true);
	}
}
:d


dove

finestra.add(Display);

deve essere ovviamente

finestra.add(new Display());

se poi il pannello lo fa che disegna quello desiderato e lo mette dentro alla finestra dovrebbe andare.
Ultima modifica effettuata da netarrow 31/01/08 17:38
aaa
06/02/08 21:28
The Lizard King
Raga ho risolto usando un JPanel e un paintComponent()... tutto merito al libro "Java: tecniche avanzate di programmazione" terza edizione... grazie potete chiudere ;D
aaa
07/02/08 15:52
netarrow
:-|
aaa