Problem z metodą paintComponent.

0

Mam taki oto kod programu:

package gify;

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

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

class PanelObrazu extends JPanel {
        public PanelObrazu(){
                obraz=new ImageIcon(this.getClass().getResource("plyta.gif")).getImage();
                nieruchoma=new ImageIcon(this.getClass().getResource("nieruchoma.gif")).getImage();    
        }
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                setBackground(Color.DARK_GRAY);  
                    
                g2.drawImage(obraz, 100, 100, 200, 200, Color.DARK_GRAY, this);
                //g2.drawImage(nieruchoma, 100, 100, 200, 200, Color.DARK_GRAY, this);
        }
       private Image obraz;
       private Image nieruchoma;
public void start(){obraz=nieruchoma;}
}

class Okno extends JFrame {
    int i;
        public Okno() {
     ActionListener akcja = new ActionListener()
     {
         public void actionPerformed(ActionEvent e)
         {
             if(e.getActionCommand().equals("PLAY"))
                 i=1;                 
         }
     };    
                Button play=new Button("PLAY");
                play.setBounds(450, 350, 70, 30);
                play.addActionListener(akcja);
                play.setActionCommand("PLAY");
                add(play);
                
                setSize(900, 550);
                setBackground(Color.DARK_GRAY);    
                PanelObrazu panel = new PanelObrazu();
                add(panel);
        }
}

public class Obraz {
        public static void main(String[] args) {
                JFrame okno = new Okno();
                okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                okno.setVisible(true);
        }
}

Problem w tym że nie wiem jak stworzyć jakąś metodę albo i bez nowej metody aby po kliknięciu na przycisk "play" wyrysowałby mi się nowy gif -> g2.drawImage(nieruchoma, 100, 100, 200, 200, Color.DARK_GRAY, this);

;-)

0
package gify;

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

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

class PanelObrazu extends JPanel {
	boolean bDrawImage = false;
        public PanelObrazu(){
                obraz=new ImageIcon(this.getClass().getResource("plyta.gif")).getImage();
                nieruchoma=new ImageIcon(this.getClass().getResource("nieruchoma.gif")).getImage();    
        }
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                setBackground(Color.DARK_GRAY);  
                    
                g2.drawImage(obraz, 100, 100, 200, 200, Color.DARK_GRAY, this);
               	if(bDrawImage==true){
			g2.drawImage(nieruchoma, 100, 100, 200, 200, Color.DARK_GRAY, this);
		}
        }
       private Image obraz;
       private Image nieruchoma;
public void start(){obraz=nieruchoma;}
}

class Okno extends JFrame {
    int i;
        public Okno() {
     ActionListener akcja = new ActionListener()
     {
         public void actionPerformed(ActionEvent e)
         {
             if(e.getActionCommand().equals("PLAY")){
                 i=1;             
	     bDrawImage = true;
	     repaint();    
              }
         }
     };    
                Button play=new Button("PLAY");
                play.setBounds(450, 350, 70, 30);
                play.addActionListener(akcja);
                play.setActionCommand("PLAY");
                add(play);
                
                setSize(900, 550);
                setBackground(Color.DARK_GRAY);    
                PanelObrazu panel = new PanelObrazu();
                add(panel);
        }
}

public class Obraz {
        public static void main(String[] args) {
                JFrame okno = new Okno();
                okno.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                okno.setVisible(true);
        }
}

Nie kompilowałem.
Pozdrawiam

0

Dzięki ale kurcze nie działa:( W klasie Okno nie widzi już boolean bDrawImage :(

0

To go przenieś tak, żeby widział, albo oznacz jako public i odpowiednio się odwołaj.
Pozdrawiam,
Afish

0

No tylko nie za bardzo mi to na razie wychodzi :-(

0

Nie wiem czy dobrze zrozumiałem.

package gify;

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

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

class PanelObrazu extends JPanel {
	private ImageIcon img = null;
	public PanelObrazu(String image_path) {
		super();
		this.setBackground(Color.DARK_GRAY);
		this.setOpaque(false);
		this.img = new ImageIcon(image_path);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img.getImage(),0, 0, getBackground(), null);
	}

	public void ustawObraz(String image_path) {
		this.img = new ImageIcon(image_path);
		repaint();
	}
}


class Okno extends JFrame {
	public final static String OBRAZ1_PATH = "plyta.gif";
	public final static String OBRAZ2_PATH = "nieruchoma.gif";
	private boolean state = false;

	private PanelObrazu panel = null;
	public Okno() {
		super("Okno");
		this.setSize(900, 550);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setBackground(Color.DARK_GRAY);

		panel = new PanelObrazu("bus.gif");
		JButton play=new JButton("PLAY");
		play.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				panel.ustawObraz( (state) ? OBRAZ1_PATH : OBRAZ2_PATH );
				state = !state;
			}
		});

		JPanel menuBar = new JPanel();
		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT));
		menuBar.add(play);

		this.getContentPane().add(BorderLayout.CENTER, panel);
		this.getContentPane().add(BorderLayout.SOUTH, menuBar);
	}
}



public class Obraz {
	public static void main(String[] args) {
		Okno okno = new Okno();
		okno.setVisible(true);
	}
}
0

yyyy... dzieki ból_głowy za pomoc ale nic a nic mi się nie wyświetla po kliknięciu na button :-(

0

Zmień :

public PanelObrazu(String image_path) 
public void ustawObraz(String image_path)

na:

public PanelObrazu(URL image_path) 
public void ustawObraz(URL image_path)

i zmień zmienne OBRAZ1_PATH i OBRAZ2_PATH na

URL obraz1_path;
URL obraz2_path;

i zainicjuj je, tak jak miałeś wcześniej, tj:

obraz1_path = this.getClass().getResource("nieruchoma.gif");
obraz1_path = this.getClass().getResource("plyta.gif");
0

Zresztą, jeżeli chcesz by gif wyświetlał animacje, to zrób to za pomocą JLabel'a:

package gify;

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

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

import java.net.URL;
import java.net.MalformedURLException;

class PanelObrazu extends JPanel {
	private JLabel img = null;
	public PanelObrazu(URL image_path) {
		super();
		this.setBackground(Color.DARK_GRAY);
		this.setLayout(new BorderLayout());

		img = new JLabel(new ImageIcon(image_path));
		this.add(BorderLayout.CENTER, img);
	}


	public void ustawObraz(URL image_path) {
		this.img.setIcon(new ImageIcon(image_path));
	}
}


class Okno extends JFrame {
	private URL obraz1_path;
	private URL obraz2_path ;
	private boolean state = false;

	private PanelObrazu panel = null;
	private JButton play = null;

	public Okno() {
		super("Okno");
		this.setSize(900, 550);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setBackground(Color.DARK_GRAY);


		obraz1_path = this.getClass().getResource("nieruchoma.gif");
		obraz2_path = this.getClass().getResource("plyta.gif");


		panel = new PanelObrazu(obraz1_path);
		play=new JButton("PLAY");
		play.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				panel.ustawObraz( (state) ? obraz1_path : obraz2_path );
				play.setText( (state) ? "Play" : "Stop" );
				state = !state;

			}
		});

		JPanel menuBar = new JPanel();
		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT));
		menuBar.add(play);

		this.getContentPane().add(BorderLayout.CENTER, panel);
		this.getContentPane().add(BorderLayout.SOUTH, menuBar);
	}
}



public class Obraz {
	public static void main(String[] args) {
		Okno okno = new Okno();
		okno.setVisible(true);
	}
}
0

Suuupppeeerrr :-) Naprawdę nie wiem jak Ci się odwdzięczyć ból_głowy ;-)
Wielkie dzięki sam bym tego niw zrobił ;-)
Pozdrawiam :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1