Obrazki w JavaApplication

0

Wszędzie te aplety i aplety :/ . Chcę dodać obrazek w zwykłym JavaApplication. Jak to zrobić?

0
add(new JLabel(new ImageIcon(nazwaPlikuZObrazkiem)));
0

Jakoś to niespecjalnie działa.

W apletach używałem

 public void paint(Graphics aplecik) 
   {
    aplecik.drawImage(img, 0, 0, this);
   }

I wszystko fajnie działało. A tutaj?

0

Tak samo jak w aplecie. Sprecyzuj może gdzie chcesz ten obrazek pokazać.
Działa zarówno to

new JLabel(new ImageIcon("Ayers.jpg"))

jak i to

public void paint(Graphics g)
   {
    g.drawImage(img, 0, 0, this);
   }
0

Trochę rozbuduje odpowiedź. Tworzysz główne okno (obiekt klasy JFrame) i:
1 sposób umieszczasz na nim obiekty klasy JLabel z obrazkami
2 sposób umieszczasz na nim panel (tzn, twoją własną klasę, która dziedziczy po JPanel), w tej klasie nadpisujesz metodę paint(...) lub paintComponent(...).

0

A mogę to zrobić na zwykłym Frame?

Frame f = new Frame()
Label a nie JLabel itp.

Bo jesli chodzi o

public void paint(Graphics g)
   {
    g.drawImage(img, 0, 0, this);
   } 

To jest błąd w this

0

Sorry

public void  paint(Graphics  g)
   {
    g.drawImage(img, 0, 0, null);
   }

Na Label nie możesz, bo Label nie może zawierać obrazka.

0
public class komponenty implements ActionListener{

          Image img;

      
komponenty(){

img = getImage(getCodeBase(),"o1.jpg"); 
..........
}

 

przy getImage(getCodeBase() wywala błąd.

Więc niby wszystko jest na dobrej drodze w wyświetleniu tylko do zmiennej img nie moge przypisać obrazka.

Pozdrawiam.

0

getCodeBase() to metoda apletów

BufferedImage img=ImageIO.read(f);

f jest typu File.

0

Hmm... Czy mógłbyś użyć tego kodu na przykladzie? Bo z tego co widzę musiałbym pododawać z kilka bibliotek itp.

0

Odświeżam.

0
package net.forum4programmers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame implements Runnable {
    private static final long serialVersionUID = 8721839888660003459L;
    
    private ImageViewer viewer;

    protected void initDefaults() {
        this.setTitle("ImageViewer");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 500);
    }
    
    private void loadImage() {
        try {
            this.viewer.setImage(new ImageIcon(new URL(
                    "http://www.bwa.olsztyn.pl/a05/3/gfx/vdt_beksinski.jpg")));
        } catch (final MalformedURLException e) {
            e.printStackTrace();
        }
        
    }
    
    protected void initComponents() {
        this.viewer = new ImageViewer();
    }
    
    protected void createLayout() {
        final Container cp = this.getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(this.viewer, BorderLayout.CENTER);
    }

    @Override
    public void run() {
        this.initComponents();
        this.createLayout();
        this.initDefaults();
        this.loadImage();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /**
     * Main.
     */
    public static void main(final String[] args) {
       SwingUtilities.invokeLater(new Main()); 
    }
}

class ImageViewer extends JPanel {
    private static final long serialVersionUID = 5756432148139301451L;
    
    private ImageIcon image;
    
    ImageViewer() {
        this.initDefault();
    }
    
    protected void initDefault() {
        this.setBackground(Color.BLACK);
    }
    
    @Override
    protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        this.drawImage(g);
    }
    
    protected void drawImage(final Graphics g) {
        if (this.hasImage()) {
            final Rectangle bounds = this.getImageBounds();
            g.drawImage(this.image.getImage(), 
                    bounds.x, bounds.y, bounds.width, bounds.height, this);
        }
    }
    
    private Rectangle getImageBounds() {
        final int x = (this.getSize().width / 2) - (this.image.getIconWidth() / 2);
        final int y = (this.getSize().height / 2) - (this.image.getIconHeight() / 2);
        return new Rectangle(x, y, this.image.getIconWidth(), this.image.getIconHeight());
    }
    
    private boolean hasImage() {
        return this.image != null && this.image.getImage() != null;
    }

    public ImageIcon getImage() {
        return image;
    }

    public void setImage(final ImageIcon img) {
        this.image = img;
    }
}

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