Interface Buttonevent...

PissPain

Aktives Mitglied
Hallo,
ich mache grade ein paar Tests und wollte 2D-Programmierung lernen, doch bin ich dabei auf ein Problem gestoßen ;)

Ich arbeite da grade an einem eigenem Button, da JButton und der awt Button komische grafik-bugs haben. Es funktioniert eigentlich alles perfekt, doch bei einer Methode weiß ich nicht weiter...

Ich zeig euch erstmal meine Klassen wo ich den Button zeichne, überprüfe usw.
Wundert euch nicht wenn dort komischer Code steht, da ich eh nur Tests mache und gucke was ich schon schaffen kann, also wäre es nett, wenn ihr mir auch dazu was sagen könntet, da ich nicht glaube, dass das ein effizienter Weg ist.

Menü-Klasse:
Java:
package org.source.display;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import org.source.display.button.ActionAdaptar;
import org.source.display.button.ActionEvent;
import org.source.display.button.ActionListener;
import org.source.display.button.Button;
import org.source.display.graphics.Graphics2D;
import org.source.engine.Source;

public class Menu implements ActionListener {
	private BufferedImage BACKGROUND;
	private List<Button> BUTTONS;
	
	public Menu(JFrame FRAME, int WIDTH, int HEIGHT) throws IOException {
		BUTTONS = new LinkedList<Button>();
		
		BACKGROUND = Graphics2D.getImage(new BufferedImage(1600, 900, BufferedImage.TYPE_INT_RGB));
		
		Color COLOR = null;
		float rgb = 0f, steps = 0f, colorSize = 255/4f;
		boolean middle = false;
		
		for(int y = 0; y < BACKGROUND.getHeight(); y++) {
			if(y > steps) {
				steps+=BACKGROUND.getHeight()/colorSize;
				if(rgb >= colorSize/2f)if(!middle)middle = true;
				if(!middle)rgb++;
				else rgb--;
			}
			
			COLOR = new Color((int)rgb, (int)rgb, (int)rgb);
			
			for(int x = 0; x < BACKGROUND.getWidth(); x++) {
				BACKGROUND.setRGB(x, y, COLOR.getRGB());
			}
		}
		
		BufferedImage EXIT_BUTTON_IMAGE_1 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/EXIT_BUTTON_1.png"));
		BufferedImage EXIT_BUTTON_IMAGE_2 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/EXIT_BUTTON_2.png"));
		BufferedImage EXIT_BUTTON_IMAGE_3 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/EXIT_BUTTON_3.png"));
		
		Button EXIT_BUTTON = new Button("EXIT");
		EXIT_BUTTON.setSize(300, 45);
		EXIT_BUTTON.setLocation(WIDTH/2-EXIT_BUTTON.getWidth()/2, HEIGHT/2-EXIT_BUTTON.getHeight()/2+80);
		EXIT_BUTTON.setImage(EXIT_BUTTON_IMAGE_1);
		EXIT_BUTTON.setRolloverImage(EXIT_BUTTON_IMAGE_2);
		EXIT_BUTTON.setPressedImage(EXIT_BUTTON_IMAGE_3);
		BUTTONS.add(EXIT_BUTTON);
		
		BufferedImage PLAY_BUTTON_IMAGE_1 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/PLAY_BUTTON_1.png"));
		BufferedImage PLAY_BUTTON_IMAGE_2 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/PLAY_BUTTON_2.png"));
		BufferedImage PLAY_BUTTON_IMAGE_3 = Graphics2D.getImage(Source.getBufferedImage("org/source/textures/PLAY_BUTTON_3.png"));
		
		Button PLAY_BUTTON = new Button("PLAY");
		PLAY_BUTTON.setSize(300, 45);
		PLAY_BUTTON.setLocation(WIDTH/2-PLAY_BUTTON.getWidth()/2, HEIGHT/2-PLAY_BUTTON.getHeight()/2+20);
		PLAY_BUTTON.setImage(PLAY_BUTTON_IMAGE_1);
		PLAY_BUTTON.setRolloverImage(PLAY_BUTTON_IMAGE_2);
		PLAY_BUTTON.setPressedImage(PLAY_BUTTON_IMAGE_3);
		BUTTONS.add(PLAY_BUTTON);
		
		new ActionAdaptar(FRAME, this, BUTTONS);
	}
	
	public void draw(Graphics2D g) {
		g.drawImage(BACKGROUND, 0, 0);
		
		for(int i = 0; i < BUTTONS.size(); i++) {
			BUTTONS.get(i).draw(g);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().getName().equals("EXIT"))System.exit(0);
		if(e.getSource().getName().equals("PLAY"));
	}
}

Meine Buttonklassen (ActionListener usw.) habe ich erstmal so genannt, weil ich das von den normalen Listener kenne und keine Ahnung habe wie ich sie sonst nennen sollte, wobei ich auch keine Ahnung habe wie die funktionieren.

Button-Klasse:
Java:
package org.source.display.button;

import java.awt.image.BufferedImage;

import org.source.display.graphics.Graphics2D;
import org.source.display.graphics.Size;

public class Button {
	public int X, Y, WIDTH, HEIGHT, K_Y;
	public BufferedImage[] IMAGES;
	public boolean isPressed = false, isClicked = false, isEntered = false, isDisabled = false;
	private String NAME = "";
	
	public Button(String NAME) { 
		IMAGES = new BufferedImage[3];
		this.NAME = NAME;
	}
	
	public Button(String NAME, int X, int Y, int WIDTH, int HEIGHT) { 
		IMAGES = new BufferedImage[3];
		this.NAME = NAME;
		this.X = X; this.Y = Y; this.WIDTH = WIDTH; this.HEIGHT = HEIGHT;
		this.K_Y = Graphics2D.getK_Y()+Y;
	}
	
	public void setBounds(int X, int Y, int WIDTH, int HEIGHT) {
		this.X = X; this.Y = Y; 
		this.WIDTH = (int)Size.getResizedWidth(WIDTH);
		this.HEIGHT = (int)Size.getResizedHeight(HEIGHT);
	}
	
	public void setSize(int WIDTH, int HEIGHT) {
		this.WIDTH = (int)Size.getResizedWidth(WIDTH);
		this.HEIGHT = (int)Size.getResizedHeight(HEIGHT);
	}
	
	public void setLocation(int X, int Y) {
		this.X = X;
		this.Y = Y;
		this.K_Y = Graphics2D.getK_Y()+Y;
	}
	
	public int getWidth() {
		return WIDTH;
	}
	
	public int getHeight() {
		return HEIGHT;
	}
	
	public int getX() {
		return X;
	}
	
	public int getY() {
		return Y;
	}
	
	public int getK_Y() {
		return K_Y;
	}
	
	public void setImage(BufferedImage IMAGE) {
		IMAGES[0] = IMAGE;
	}
	
	public void setRolloverImage(BufferedImage IMAGE) {
		IMAGES[1] = IMAGE;
	}
	
	public void setPressedImage(BufferedImage IMAGE) {
		IMAGES[2] = IMAGE;
	}
	
	public BufferedImage getImage() {
		return IMAGES[0];
	}
	
	public BufferedImage getRolloverImage() {
		return IMAGES[1];
	}
	
	public BufferedImage getPressedImage() {
		return IMAGES[2];
	}
	
	public void setName(String NAME) {
		this.NAME = NAME;
	}
	
	public String getName() {
		return NAME;
	}
	
	public void draw(Graphics2D g) {
		if(!isDisabled) {
			if(isPressed)g.drawImage(IMAGES[2], X, Y);
			else if(isEntered)g.drawImage(IMAGES[1], X, Y);
			else g.drawImage(IMAGES[0], X, Y);
		} else {
			g.drawImage(IMAGES[0], X, Y);
		}		
	}
	
	public void setPressed(boolean isPressed) {
		this.isPressed = isPressed;
	}
	
	public void setClicked(boolean isClicked) {
		this.isClicked = isClicked;
	}
	
	public void setEntered(boolean isEntered) {
		this.isEntered = isEntered;
	}
	
	public void setDisabled(boolean isDisabled) {
		this.isDisabled = isDisabled;
	}
}

ActionAdaptar:
Java:
package org.source.display.button;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

import javax.swing.JFrame;

public class ActionAdaptar {
	private JFrame FRAME;
	private ActionListener LISTENER;
	private ActionEvent EVENT;
	private List<Button> BUTTONS;
	
	public ActionAdaptar(JFrame FRAME, ActionListener LISTENER, List<Button> BUTTONS) {
		this.LISTENER = LISTENER;
		this.FRAME = FRAME;
		this.BUTTONS = BUTTONS;
		
		EVENT = new ActionEvent(BUTTONS);
		
		eventThread();
	}
	
	private void eventThread() {
		FRAME.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				int MouseX = e.getX();
				int MouseY = e.getY();
				
				for(int i = 0; i < BUTTONS.size(); i++) {
					Button BUTTON = BUTTONS.get(i);
					if(!BUTTON.isDisabled) {
						if(MouseX > BUTTON.X && BUTTON.X+BUTTON.WIDTH > MouseX && MouseY > BUTTON.K_Y && BUTTON.K_Y+BUTTON.HEIGHT > MouseY) {
							if(BUTTON.isPressed) {
								BUTTONS.get(i).setClicked(true);
								BUTTONS.get(i).setPressed(false);
								LISTENER.actionPerformed(EVENT);
							}
						} else if(BUTTON.isPressed) {
							BUTTONS.get(i).setPressed(false);
							BUTTONS.get(i).setEntered(false);
						}
					}
				}
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				int MouseX = e.getX();
				int MouseY = e.getY();
				
				for(int i = 0; i < BUTTONS.size(); i++) {
					Button BUTTON = BUTTONS.get(i);
					
					if(!BUTTON.isDisabled) {
						if(MouseX > BUTTON.X && BUTTON.X+BUTTON.WIDTH > MouseX && MouseY > BUTTON.K_Y && BUTTON.K_Y+BUTTON.HEIGHT > MouseY)BUTTONS.get(i).setPressed(true);
						else BUTTONS.get(i).setPressed(false);
					}
				}
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
			}
		});
		
		this.FRAME.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
			}
			@Override
			public void mouseMoved(MouseEvent e) {
				int MouseX = e.getX();
				int MouseY = e.getY();
				
				for(int i = 0; i < BUTTONS.size(); i++) {
					Button BUTTON = BUTTONS.get(i);
					
					if(!BUTTON.isDisabled) {
						if(MouseX > BUTTON.X && BUTTON.X+BUTTON.WIDTH > MouseX && MouseY > BUTTON.K_Y && BUTTON.K_Y+BUTTON.HEIGHT > MouseY)BUTTONS.get(i).setEntered(true);
						else BUTTONS.get(i).setEntered(false);
					}
				}
			}
		});
	}
}

ActionListener:
Java:
package org.source.display.button;

public interface ActionListener {
	public void actionPerformed(ActionEvent e);
}

ActionEvent:
Java:
package org.source.display.button;

import java.util.List;

public class ActionEvent {
	private List<Button> BUTTONS;
	
	public ActionEvent(List<Button> BUTTONS) {
		this.BUTTONS = BUTTONS;
	}
	
	public Button getSource() {
		for(int i = 0; i < BUTTONS.size(); i++) {
			if(BUTTONS.get(i).isClicked) {
				BUTTONS.get(i).setClicked(false);
				return BUTTONS.get(i);
			}
		}
		return null;
	}
}

Ist erstmal bischen Code, aber ihr müsst euch nicht alles angucken, da ich ja nur ein Problem habe...
Z.b. die Graphics2D habe ich selber geschrieben, da ich im Vollbild bin und diese Klasse mir alles vereinfacht, beim Bilderverkleinern, Positionenanpassung usw.
(Ich hoffe das ist erstmal alles verständlich)

So, jetzt zu dem Problem, ich habe keine Ahnung warum ich in meiner actionPerformed(ActionEvent e)
Methode nur einen Button abfragen kann, also bei dem zweiten gibt es eine Exception!?

Hier zur Meldung:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.source.display.Menu.actionPerformed(Menu.java:84)
	at org.source.display.button.ActionAdaptar$1.mouseReleased(ActionAdaptar.java:40)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Window.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Ich hoffe, dass das erstmal reicht und ihr mal kurz drunter gucken könnt, um mir zu sagen, ob ich was vergessen habe habe oder anderes.

Vielen Dank :)
 

PissPain

Aktives Mitglied
Ok, bin nochmal drüber gegangen und habe bemerkt, dass ich in meiner Methode "actionPerformed(ActionEvent e)" nur ein mal den Button getten kann... Also habe ich es so gemacht:

Java:
@Override
	public void actionPerformed(ActionEvent e) {
		String NAME = e.getSource().getName();
		
		if(NAME.equals("EXIT"))System.exit(0);
		if(NAME.equals("PLAY"));
	}
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
P SWT Buttonevent Java Basics - Anfänger-Themen 2

Ähnliche Java Themen


Oben