Jo Leute.. ich ahbe probiert einen eigenen Button zu schreiben...
leider funktioniert das actionPerformed nicht..
Main-Klasse:
Playstate:
Button:
Dann habe ich noch ein actionlistener Interface und ein actionevent mit einem getter für source..
Kann mir jmd sagen warum das nicht klappt?
LG
leider funktioniert das actionPerformed nicht..
Main-Klasse:
Java:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JLabel{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(1280, 720);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
Draw d = new Draw();
d.setSize(1280,720);
d.setVisible(true);
new Playstate();
jf.add(d);
jf.setVisible(true);
}
}
Playstate:
Java:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.RepaintManager;
public class Playstate implements ActionListener{
private static Button Start;
public Playstate() {
Start = new Button(this,"Start Game", 200, 200, 100, 30);
}
public void update() {
}
public static void render(Graphics g) {
Start.RenderButton(g);
}
public void mousePressed(MouseEvent e) {
Start.mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
Start.mouseReleased(e);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Start) {
System.out.println("Start");
}
}
}
Button:
Java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
public class Button {
private int x,y,width,height, mouseX, mouseY;
private boolean enabled,pressed;
private String text;
private final Font font = new Font("Verdana", Font.PLAIN, 14);
private ActionListener listener;
public Button(ActionListener listener, String text, int x, int y, int width, int height) {
this.listener = listener;
this.text = text;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
enabled = true;
}
public void RenderButton(Graphics g) {
mouseX = Draw.mouseX;
mouseY = Draw.mouseY;
if(isPressed(mouseX, mouseY)) {
g.setColor(Color.red);
}else {
g.setColor(Color.red.darker());
}
g.fillRoundRect(x, y, width, height, 15, 15);
g.setColor(Color.BLACK);
g.drawRoundRect(x, y, width, height, 15, 15);
int stringWidth = g.getFontMetrics().stringWidth(text);
g.drawString(text, x + width/2-stringWidth/2, y+height/2);
}
private boolean isPressed(int x, int y) {
return x>= this.x && x <= this.x+width && y >= this.y && y <= this.y+height;
}
public void mousePressed(MouseEvent e) {
if(isPressed(e.getX(), e.getY())) {
pressed = true;
}
}
public void mouseReleased(MouseEvent e) {
if(pressed && enabled) {
listener.actionPerformed(new ActionEvent(this));
pressed = false;
}
}
public void mouseClick(MouseEvent e) {
if(pressed && enabled) {
listener.actionPerformed(new ActionEvent(this));
pressed = false;
}
}
}
Kann mir jmd sagen warum das nicht klappt?
LG