Witam
czy istnieje możliwość ustawiania tła komponentu JPanel (javax.swing) na podstawie jakiegos obrazu (x.jpg np.)?? (zdaje się że można ustawiać kolor tła...)
z góry dzięki
Witam
czy istnieje możliwość ustawiania tła komponentu JPanel (javax.swing) na podstawie jakiegos obrazu (x.jpg np.)?? (zdaje się że można ustawiać kolor tła...)
z góry dzięki
np:
public class Obrazek extends JPanel {
private ImageIcon obrazek;
private Color bgcolor;
public Obrazek(String o,Color bgcolor) {
super();
this.obrazek = new ImageIcon(o);
this.bgcolor = bgcolor;
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
g.setColor(bgcolor);
g.fillRect(0,0, this.getSize().width, this.getSize().height);
g.drawImage(obrazek.getImage(), 0, 00, obrazek.getIconWidth(), obrazek.getIconHeight(), null);
super.paintComponent(g);
}
public Dimension getPreferredSize() {
return new Dimension(10+obrazek.getIconWidth(), obrazek.getIconHeight()+10);
}
}
Dokładnie chodzi mi o to, by w jednym JPanelu wyświetlać tło ramki, a w drugim wyświetlać wczytywane obrazy w taki sposób, aby tło nie znikało (oczywiście JPanel przeznaczony do wyświetlania obrazów musi być odpowiednio mniejszy, aby w ogóle było widać to tło).
Nie wiem niestety jak to zrobić, myślałem o tym, aby niejako zagnieździć te komponenty jeden wewnątrz drugiego, korzystając nieco z powyższej podpowiedzi, ale to nie działa. [???]
Bardzo proszę o pomoc i z góry dziękuje...
Pozdrawiam
oto moja twórczość :-/
class Tlo extends JPanel
{
private ImageIcon obrazek;
public Tlo(String o)
{
super();
this.obrazek = new ImageIcon(o);
this.setOpaque(false);
Obrazek panelObrazka = new Obrazek("pe_blue.jpg");
this.add(panelObrazka);
}
public void paintComponent(Graphics g)
{
g.drawImage(obrazek.getImage(), 0, 0, this.getSize().width, this.getSize().height, null);
super.paintComponent(g);
}
}
class Obrazek extends JPanel
{
private ImageIcon obrazek;
public Obrazek(String o)
{
super();
this.setSize(100,100);
this.setLocation(100,100);
this.obrazek = new ImageIcon(o);
this.setOpaque(false);
}
public void paintComponent(Graphics g)
{
g.drawImage(obrazek.getImage(), 100, 100, obrazek.getIconWidth()/2, obrazek.getIconHeight()/2, null);
super.paintComponent(g);
}
}
package gui;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
public class Background extends JPanel {
private ImageIcon image;
public Background(ImageIcon image,Color col) {
super();
this.image = image;
this.setBackground(col);
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
drawImage(g);
super.paintComponent(g);
}
private void drawImage(Graphics g) {
if(image == null) return;
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage(image.getImage(), x, y, null, null );
}
}
package gui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.GridLayout;
public class Gallery extends JPanel {
public Gallery(int rows, int columns) {
super();
this.setOpaque(false);
this.setLayout(new GridLayout(rows,columns));
}
public void addImg(ImageIcon img) {
JLabel lab = new JLabel(img);
this.add(lab);
}
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Dimension;
import gui.Background;
import gui.Gallery;
public class Main extends JFrame {
private Background bg;
private Gallery gal;
private JScrollPane sp;
private JLayeredPane layer;
public Main() {
super("Layer Demo");
this.setSize(600,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
initComponents();
this.getContentPane().add(BorderLayout.CENTER,sp);
this.show();
}
private void initComponents() {
bg = new Background(new ImageIcon("tlo.jpg"),Color.BLACK);
gal = new Gallery(2,2);
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
layer = new JLayeredPane();
layer.add(bg, JLayeredPane.DEFAULT_LAYER);
bg.setBounds(0,0,600,800);
layer.add(gal, JLayeredPane.PALETTE_LAYER);
gal.setBounds(0,0,600,400);
layer.setPreferredSize(new Dimension(600,400));
sp = new JScrollPane(layer);
}
public static void main(String[] args) {
new Main();
}
}
W zasadzie jeśli wystarczą Ci dwie warstwy, możesz zrobić tak, że tło będzie statyczne a będziesz mógł przesówać na nim drugi panel np: z innymi obrazkami:
package gui;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
public class Background extends JScrollPane {
private ImageIcon image;
public Background(ImageIcon image,Color col) {
super();
this.image = image;
this.setBackground(col);
this.setOpaque(false);
this.getViewport().setOpaque(false);
}
public void paintComponent(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
drawImage(g);
super.paintComponent(g);
}
private void drawImage(Graphics g) {
if(image == null) return;
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage(image.getImage(), x, y, null, null );
}
}
package gui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.GridLayout;
public class Gallery extends JPanel {
public Gallery(int rows, int columns) {
super();
this.setOpaque(false);
//this.setBackground(new Color(255,255,255,255));
this.setLayout(new GridLayout(rows,columns));
}
public void addImg(ImageIcon img) {
JLabel lab = new JLabel(img);
this.add(lab);
}
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JLayeredPane;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Dimension;
import gui.Background;
import gui.Gallery;
public class Main extends JFrame {
private Background bg;
private Gallery gal;
public Main() {
super("Tla");
this.setSize(600,400);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
initComponents();
this.getContentPane().add(BorderLayout.CENTER,bg);
this.show();
}
private void initComponents() {
bg = new Background(new ImageIcon("tlo.jpg"),Color.BLACK);
gal = new Gallery(2,2);
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
gal.addImg(new ImageIcon("obrazek.jpg"));
bg.getViewport().setView(gal);
}
public static void main(String[] args) {
new Main();
}
}
Wielkie dzięki
mam tylko taki szczegół, mianowicie chyba zamiast:
this.show();
powinno być:
this.setVisable(true);
w każdym razie mi kompilator nie chciał tego odpuścić, bo to zdaje się jest metoda przestarzała
ale jak wspomniałem to jest szczegół
jeszcze raz wielkie dzięki Jacobus2k, bardzo mi pomogłeś
pozdrawiam