Java Buttons mehreckig und Druckfunktionen?

Status
Nicht offen für weitere Antworten.

electronic_green

Neues Mitglied
Hallo,
ich habe im Moment einige Problem:
In Java möchten wir ein Spiel programmieren, wir brauchen aber eine Möglichkeit ein 6eckiges Feld abzufragen, gibts eine elegante Methode da mehrere Buttons drauf zu legen? Brauchen einmal einen Zugriff auf das Feld selbst plus die Eckpunkte davon (ja ich gebs zu es soll ein Nachbau von "Siedler von Catan" werden 🙂 )
Außerdem soll für den Sieger am Ende eine Urkunde ausgedruckt werden (wie das mit einer normalen Grafik geht wissen wir) aber in dieser Urkunde sollen Name und Punktestand enthalten sein, gibts eine Möglichkeit ein Image zu erstellen mit einer Grafik im Hintergrund und dann da einen Text drauf zu legen?
Hab leider bis jetzt noch nichts finden können.

Gruß

Niels
 
Zu 1.) Dazu kannst Du Dir einen eigenen Button schreiben

Zu 2.) Wie liegt dein Grafik für die Urkunde denn vor?? Hier mal ein Bsp. für BufferedImage:
Code:
    BufferedImage bi = new BufferedImage(150,50, BufferedImage.TYPE_INT_RGB); // Wird neu erstellt, kann natürlich auch vorher geladen werden mittels "ImageIO#read"
    Graphics g = bi.getGraphics();
    g.setColor(Color.WHITE); // Farbe setzen
    g.fillRect(0,0,150,50);  // Hintergrund auf WHITE setzen
    g.setColor(Color.RED);   // Farbe setzen
    g.drawString("Thomas Mustermann", 0, 10); // Name schreiben
    ImageIO.write(bi, "png", new File("c:\\test.png")); // Bild speichern (kann natürlich auch gedruckt werden...)
 
Das geht ganz gut in Java. In meinem Beispiel kann der Button noch nichts aber immerhin.

Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;

import javax.swing.JButton;
import javax.swing.border.Border;

public class HexButton extends JButton {

	// das Polygon beschreibt die Form
	private Polygon shape;

	private boolean showBorder;

	private Color borderColor, foregroundColor, buttonPressedColor;

	public HexButton(int sideLength) {
		this.shape = new Polygon();
		// initialisiere Form
		this.initialize(sideLength);
		this.setContentAreaFilled(false);
		this.showBorder = true;
		this.setOpaque(false);
		this.foregroundColor = Color.LIGHT_GRAY;
		this.borderColor = Color.BLACK;
		this.buttonPressedColor = Color.GRAY;
		this.setLayout(null);
	}

	public void showBorder(boolean b) {
		this.showBorder = b;
		this.repaint();
	}

	protected void initialize(int sideLength) {
		Point p1, p2, p3, p4, p5, p6;
		this.setSize(2 * sideLength + 1, (int) (Math.round(Math.sin(Math.PI / 3)
				* sideLength) * 2));

		// Berechnung der 6 Ecken des Hexagons mit Hilfe der Seitenlänge
		p1 = new Point(Math.round(sideLength / 2), 0);
		p2 = new Point(Math.round(sideLength / 2) + sideLength, 0);
		// bei einer ungeraden Seitenlänge wird Punkt3 um einen Pixel nach links
		// versetzt
		if (sideLength % 2 != 0) {
			p3 = new Point((2 * sideLength - 1), this.getHeight() / 2);
		} else {
			p3 = new Point(2 * sideLength, this.getHeight() / 2);
		}
		p4 = new Point(Math.round(sideLength / 2) + sideLength, (2 * this
				.getHeight() / 2) - 1);
		p5 = new Point(Math.round(sideLength / 2), (2 * this.getHeight() / 2) - 1);
		p6 = new Point(0, this.getHeight() / 2);

		this.shape.addPoint((int) Math.round(p1.getX()), (int) Math.round(p1
				.getY()));
		this.shape.addPoint((int) Math.round(p2.getX()), (int) Math.round(p2
				.getY()));
		this.shape.addPoint((int) Math.round(p3.getX()), (int) Math.round(p3
				.getY()));
		this.shape.addPoint((int) Math.round(p4.getX()), (int) Math.round(p4
				.getY()));
		this.shape.addPoint((int) Math.round(p5.getX()), (int) Math.round(p5
				.getY()));
		this.shape.addPoint((int) Math.round(p6.getX()), (int) Math.round(p6
				.getY()));
		this.setMinimumSize(this.getSize());
		this.setMaximumSize(this.getSize());
		this.setPreferredSize(this.getSize());
	}

	// Hit detection
	public boolean contains(int x, int y) {
		return this.shape.contains(x, y);
	}

	// Zeichne den Button
	protected void paintComponent(Graphics g) {
		Graphics2D gCopy = (Graphics2D) g.create();
		if (getModel().isArmed()) {
			gCopy.setColor(this.buttonPressedColor);
		} else {
			gCopy.setColor(this.foregroundColor);
		}
		gCopy.fillPolygon(this.shape);
	}

	// zeichne die Border
	protected void paintBorder(Graphics g) {
		if (this.showBorder) {
			Graphics2D gCopy = (Graphics2D) g.create();
			gCopy.setColor(this.borderColor);
			gCopy.drawPolygon(this.shape);
		}
	}

	public Color getBorderColor() {
		return borderColor;
	}

	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
	}

	public Color getButtonPressedColor() {
		return buttonPressedColor;
	}

	public void setButtonPressedColor(Color buttonPressedColor) {
		this.buttonPressedColor = buttonPressedColor;
	}

	public Color getForegroundColor() {
		return foregroundColor;
	}

	public void setForegroundColor(Color foregroundColor) {
		this.foregroundColor = foregroundColor;
	}

	public Polygon getShape() {
		return shape;
	}

	public void setShape(Polygon shape) {
		this.shape = shape;
	}

}

und Dies zum testen

Code:
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextHex extends JFrame {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		HexButton b1, b2, b3;
		b1 = new HexButton(280);
		b2 = new HexButton(28);
		b3 = new HexButton(47);
		b1.setButtonPressedColor(Color.GREEN);
		b2.showBorder(false);
		b3.setBorderColor(Color.CYAN);
		panel.add(b1);
		panel.add(b2);
		panel.add(b3);
		frame.add(panel);
		frame.setBounds(10, 10, 800, 700);
		frame.setVisible(true);
	}
}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben