Ich möchte itm4 anklicken und dafür sorgen dass sich das Hintergrund panelbk-Objekt der Klasse PWorld ändert. Das Foto würde apsnight.png heißen. Wie könnte ich das Hintergrund nach der PGUI-Initialisierung trotzdem ändern, wenn itm4 angeklickt wird?
Java:
import java.awt.BorderLayout;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PGUI extends JFrame
{
private static final long serialVersionUID = 1L;
PWorld panelbk= new PWorld();;
JPanel itembar=new JPanel();
JPanel unsichtbar=new JPanel();
JLabel lbl1=new JLabel();
JLabel lbl2=new JLabel();
JLabel lbl3=new JLabel();
JLabel lbl4=new JLabel();
double hunger=50;
double gsnd=50;
double sbr=50;
double enr=50;
int xxx;
boolean activebg=true;
Timer myTimer=new Timer();
TimerTask task=new TimerTask(){
public void run() {
if(sbr>0){
lbl1.setText("Sauberheit: "+new DecimalFormat("0.00").format(sbr+=-0.001));}
if(hunger>0){
lbl2.setText("Hunger: "+new DecimalFormat("0.00").format(hunger+=-0.001));}
if(gsnd>0){
lbl3.setText("Gesundheit: "+new DecimalFormat("0.00").format(gsnd+=-0.001));}
if(enr>0){
lbl4.setText("Energie: "+new DecimalFormat("0.00").format(enr+=-0.001));}
if(activebg==false){
lbl4.setText("Energie: "+new DecimalFormat("0.00").format(enr+=0.2));
}
}
};
public PGUI(){
//frame#1
setTitle("Pou-Windows-10-Edition-Free");
setSize(1280,720);
setResizable(false);
//labels
lbl1.setSize(150, 14);
lbl1.setLocation(20, 48);
lbl1.setText("Sauberheit: "+sbr);
lbl2.setSize(150, 14);
lbl2.setLocation(180, 48);
lbl2.setText("Hunger: "+hunger);
lbl3.setSize(150, 14);
lbl3.setLocation(350, 48);
lbl3.setText("Gesundheit: "+gsnd);
lbl4.setSize(150, 14);
lbl4.setLocation(510, 48);
lbl4.setText("Energie: "+enr);
//items
Items itm1=new Items(50,490,200,200,"brld/bs.png",this,1);
Items itm2=new Items(350,490,200,200,"brld/cs.png",this,2);
Items itm3=new Items(750,490,200,200,"brld/fs.png",this,3);
Items itm4=new Items(1050,490,200,200,"brld/ds.png",-1,this,4);
Items pou=new Items(500,100,300,300,"brld/es.png",-1,this,-1);
//panelbk
panelbk.setLayout(null);
panelbk.add(itm1);
panelbk.add(itm2);
panelbk.add(itm3);
panelbk.add(itm4);
panelbk.add(lbl1);
panelbk.add(lbl2);
panelbk.add(lbl3);
panelbk.add(lbl4);
panelbk.add(pou);
getContentPane().add(panelbk,BorderLayout.CENTER);
setVisible(true);}
public static void main(String[] args) {
PGUI pg=new PGUI();
pg.starti();
}
public void setlbls(double adds,double addh,double addg,double adde){
if(sbr<100.0){
lbl1.setText("Sauberheit: "+new DecimalFormat("0.00").format(sbr+=adds));}
if(hunger<100.0){
lbl2.setText("Hunger: "+new DecimalFormat("0.00").format(hunger+=addh));}
if(gsnd<100.0){
lbl3.setText("Gesundheit: "+new DecimalFormat("0.00").format(gsnd+=addg));}
if(enr<100.0){
lbl4.setText("Energie: "+new DecimalFormat("0.00").format(enr+=adde));}
}
public void starti(){
myTimer.scheduleAtFixedRate(task,0,1000);
}
public int getx(){
int xxx=getLocation().x;
return xxx;
}
public int gety(){
int yyy=getLocation().y;
return yyy;
}
public void setactive(boolean active){
activebg=active;
}
}
Java:
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Items extends JButton {
private static final long serialVersionUID = 1L;
private int xPos=-1;
private int yPos=-1;
boolean active=true;
public Items(int xx,int yy,int w,int h, String url,PGUI pg, int ac){
try{
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(url).getFile());
BufferedImage buttonIcon = ImageIO.read(file);
setIcon(new ImageIcon(buttonIcon));
}
catch(IOException e){System.out.println(e.getMessage());}
setBounds(xx,yy,w,h);
setBorder(BorderFactory.createEmptyBorder());
setContentAreaFilled(false);
addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e){
if(xPos==-1 || yPos==-1){
xPos = e.getX();
yPos = e.getY();
}
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point point = pointerInfo.getLocation();
int x = (int)point.getX();
int y = (int)point.getY();
int xxx=pg.getx();
int yyy=pg.gety();
setLocation(x-xPos-(xxx)-3, y-yPos-30-(yyy)+4);//!!hier
if(ac==1){
if(getLocation().x<=800 && getLocation().x>=500 && getLocation().y>=100 && getLocation().y<=400){
pg.setlbls(0.01,0,0,0);
}}
if(ac==2){
if(getLocation().x<=800 && getLocation().x>=500 && getLocation().y>=100 && getLocation().y<=400){
pg.setlbls(0,0.01,0,0);
}}
if(ac==3){
if(getLocation().x<=800 && getLocation().x>=500 && getLocation().y>=100 && getLocation().y<=400){
pg.setlbls(0,0,0.01,0);
}}
}
public void mouseMoved(MouseEvent e){
xPos = -1;
yPos = -1;
setBounds(xx,yy,w,h);
}
});
}
public Items(int xx,int yy,int w,int h, String url, int i,PGUI pg,int ac){
if(i==-1){
try{
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(url).getFile());
BufferedImage buttonIcon = ImageIO.read(file);
setIcon(new ImageIcon(buttonIcon));
}
catch(IOException e){System.out.println(e.getMessage());}
setBounds(xx,yy,w,h);
setBorder(BorderFactory.createEmptyBorder());
setContentAreaFilled(false);
}
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ac==4){
pg.setactive(active=!active);
}
}
});
}
}
Java:
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class PWorld extends JPanel {
private static final long serialVersionUID = 1L;
Image img;
public PWorld(){
try{ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("brld/aps.png").getFile());
img = ImageIO.read(file);}
catch(IOException e){System.out.println(e.getMessage());}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0,0,null);
}
}