Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    vi prego aiutatemi

    Ciao a tutti,non sono molto pratico di java , volevo sapere se era possibile sostituire i frame bianchi della pioggia che cade con delle imagini di piccola dimensione.

    questo è il codice :

    package rain;

    import java.awt.Color;
    import java.awt.Graphics;

    import javax.swing.JFrame;
    import javax.swing.Timer;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;/*

    */
    public class SRainFrame extends JFrame implements ActionListener{

    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;


    private SRainPanel panel = new SRainPanel();
    private Timer timer = new Timer(100, null);

    public SRainFrame(){
    super("Soothing Rain");

    createFrame();

    timer.addActionListener(this);
    }

    private void createFrame(){

    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    this.setBackground(Color.BLACK);

    this.setContentPane(panel);

    this.setResizable(false);

    this.setVisible(true);
    }

    public void start(){

    timer.start();
    }

    public void paint(Graphics g){

    g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);

    g.setColor(Color.white);

    panel.paint(g);
    }

    public void actionPerformed(ActionEvent e){

    repaint();
    }

    public static void main(String[] args){

    SRainFrame frf = new SRainFrame();

    frf.start();
    }
    }

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: vi prego aiutatemi

    Originariamente inviato da esploratore8
    Ciao a tutti,non sono molto pratico di java , volevo sapere se era possibile sostituire i frame bianchi della pioggia che cade con delle imagini di piccola dimensione.
    Bisognerebbe vedere cosa fa quel SRainPanel ma in ogni caso ti dico subito che quel panel.paint(g); non è affatto una bella cosa!!! Stai chiedendo ad un altro componente di "disegnarsi" su un altro Graphics non suo.

    P.S. posta il codice tra i tag CODE.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Posto Tutte le classi:

    SRainDrop.java:

    package rain;


    import java.awt.Graphics;

    /*
    *
    *
    *
    */

    /**
    *
    *
    *
    *
    */
    public class SRainDrop {
    int x, y, yStop;
    int width = 1;
    int height = 1;
    int velocity;

    private static int DELTA = 5;
    boolean done = false;
    public SRainDrop(int x, int y, int velocity, int yStop){

    this.x = x;
    this.y = y;
    this.velocity = velocity;
    this.yStop = yStop;
    }

    public void paint(Graphics g){
    if(y < yStop){

    g.drawLine(x, y, x, y+DELTA);

    y = y+DELTA+velocity;
    }
    else{

    g.drawOval(x, y, width, height);

    width = width+1;

    if(width%5 == 0){
    height = height+1;
    }

    done = width>20? true:false;
    }
    }
    }




    SRainPanel.java:

    package rain;

    import java.awt.Color;
    import java.awt.Graphics;

    import javax.swing.JPanel;
    /*
    */

    /**
    *
    *
    *
    *
    */
    public class SRainPanel extends JPanel{

    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;
    private static int NUM_DROPS = 150;

    private SRainDrop[] rainDrops = new SRainDrop[NUM_DROPS];

    public SRainPanel(){
    createRainDrops();
    createPanel();
    }

    private void createRainDrops(){
    for(int i = 0; i < rainDrops.length; ++i){

    createRainDrop(i);
    }
    }

    private void createRainDrop(int index){
    int randomX = (int) (Math.random()*500);
    int randomY = (int) (Math.random()*50);
    int randomV = (int) (Math.random()*10);
    int stop = (int) (Math.random()*20) + 465;

    rainDrops[index] = new SRainDrop(randomX, randomY, randomV, stop);
    }

    private void createPanel(){
    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    this.setBackground(Color.BLACK);
    }
    public void paint(Graphics g){

    for(int i = 0; i < rainDrops.length; ++i){
    if(rainDrops[i].done){
    createRainDrop(i);
    }
    rainDrops[i].paint(g);
    }
    }
    }




    SRainFrame.java:

    package rain;

    import java.awt.Color;
    import java.awt.Graphics;

    import javax.swing.JFrame;
    import javax.swing.Timer;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;/*

    */
    public class SRainFrame extends JFrame implements ActionListener{

    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;


    private SRainPanel panel = new SRainPanel();
    private Timer timer = new Timer(100, null);

    public SRainFrame(){
    super("Soothing Rain");

    createFrame();

    timer.addActionListener(this);
    }

    private void createFrame(){

    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    this.setBackground(Color.BLACK);

    this.setContentPane(panel);

    this.setResizable(false);

    this.setVisible(true);
    }

    public void start(){

    timer.start();
    }

    public void paint(Graphics g){

    g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);

    g.setColor(Color.white);

    panel.paint(g);
    }

    public void actionPerformed(ActionEvent e){

    repaint();
    }

    public static void main(String[] args){

    SRainFrame frf = new SRainFrame();

    frf.start();
    }
    }


    Scusate se ho aperto un altra discussione ho sbagliato.

  4. #4
    Utente di HTML.it L'avatar di @DI3GO@
    Registrato dal
    Nov 2008
    Messaggi
    537
    I TAG CODE!!!!!
    Leggi il regolamento....altrimenti prima di scrivere codice indentato metti all'inizio [code] ed alla fine [ /code] togliendo lo spazio
    Nipote: persona incompetente, con le soli doti di "copia/incolla" e la creazione automatica di siti internet ed interfaccie grafiche.Compie lavori apparentemente qualificati e richiesta una modifica sparisce in quatemala con i pochi soldi ottenuti.[...] Fonte la Diegonzelli

  5. #5
    SRainDrop.java:

    codice:
    package rain;
    import java.awt.Graphics;
    /*
    public class SRainDrop {	
    int x, y, yStop;	
    int width = 1;	
    int height = 1;	
    int velocity;
    private static int DELTA = 5;	
    boolean done = false;	
    public SRainDrop(int x, int y, int velocity, int yStop){
    this.x = x;
    this.y = y;
    this.velocity = velocity;
    this.yStop = yStop;	
    }
    public void paint(Graphics g){
    if(y < yStop){
    g.drawLine(x, y, x, y+DELTA);
    y = y+DELTA+velocity;
    }
    else{
    g.drawOval(x, y, width, height);
    width = width+1;	
    if(width%5 == 0){
    height = height+1;
    }
    done = width>20? true:false;
    }
    }
    }
    SRainPanel.java:

    codice:
    package rain;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    */
    public class SRainPanel extends JPanel{
    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;
    private static int NUM_DROPS = 150;
    private SRainDrop[] rainDrops = new SRainDrop[NUM_DROPS];
    public SRainPanel(){
    createRainDrops();	
    createPanel();
    }
    private void createRainDrops(){
    for(int i = 0; i < rainDrops.length; ++i){
    createRainDrop(i);
    }
    }
    private void createRainDrop(int index){	
    int randomX = (int) (Math.random()*500);	
    int randomY = (int) (Math.random()*50);
    int randomV = (int) (Math.random()*10);	
    int stop = (int) (Math.random()*20) + 465;
    rainDrops[index] = new SRainDrop(randomX, randomY, randomV, stop);
    }
    private void createPanel(){	
    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);	
    this.setBackground(Color.BLACK);
    }	
    public void paint(Graphics g){
    for(int i = 0; i < rainDrops.length; ++i){
    if(rainDrops[i].done){
    createRainDrop(i);
    }
    rainDrops[i].paint(g);
    }
    }
    }
    SRainFrame.java

    codice:
    package rain;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;/*
    */
    public class SRainFrame extends JFrame implements ActionListener{
    private static int DEFAULT_WIDTH = 500;
    private static int DEFAULT_HEIGHT = 500;
    private SRainPanel panel = new SRainPanel();
    private Timer timer = new Timer(100, null);
    public SRainFrame(){
    super("Soothing Rain");
    createFrame();
    timer.addActionListener(this);
    }
    private void createFrame(){
    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBackground(Color.BLACK);
    this.setContentPane(panel);
    this.setResizable(false);
    this.setVisible(true);
    }
    public void start(){
    timer.start();
    }
    public void paint(Graphics g){
    g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    g.setColor(Color.white);
    panel.paint(g);
    }
    public void actionPerformed(ActionEvent e){
    repaint();
    }
    public static void main(String[] args){
    SRainFrame frf = new SRainFrame();
    frf.start();
    }
    }
    scusa ma sono nuovo non sapevo come fare...

  6. #6
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,480

    Moderazione

    Originariamente inviato da esploratore8
    vi prego aiutatemi
    Usa titoli più significativi per le discussioni, come da Regolamento.

    Questo l'ho corretto io.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

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 © 2025 vBulletin Solutions, Inc. All rights reserved.