Hallo,
ich würde gern auf ein Panel ein Dreieck zeichnen. Das geht aber nicht so wie es soll. Da ich neben dem Dreieck auch noch andere Formen (Kreise, Rechtecke) darstellen kann weiß ich nicht warum es beim Dreieck nicht geht. Der Code ist an sich fast der Gleiche. Zwischen Haupt- und Unterklassen, bed'findet sich noch eine abstrakte Klasse.
Erst nachdem ich das Panel einmal repaintet habe wird das Dreieck gezeichnet. Aber warum? Und warum kann ich das Dreieck nicht bwegen?
Ich hab mal all das rausgelöscht, das ich für unnötig hielt. Ist leider immer noch ziemlich viel:
ich würde gern auf ein Panel ein Dreieck zeichnen. Das geht aber nicht so wie es soll. Da ich neben dem Dreieck auch noch andere Formen (Kreise, Rechtecke) darstellen kann weiß ich nicht warum es beim Dreieck nicht geht. Der Code ist an sich fast der Gleiche. Zwischen Haupt- und Unterklassen, bed'findet sich noch eine abstrakte Klasse.
Erst nachdem ich das Panel einmal repaintet habe wird das Dreieck gezeichnet. Aber warum? Und warum kann ich das Dreieck nicht bwegen?
Ich hab mal all das rausgelöscht, das ich für unnötig hielt. Ist leider immer noch ziemlich viel:
Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* This is the main class
* @author antoras
*
*/
public class myGF extends JFrame {
private JPanel panel;
private final JButton button_triangle = new JButton("Dreieck");
private final JButton button_display = new JButton("anzeigen");
private final JButton button_hide = new JButton("ausblenden");
/*
* 0 = circle
* 1 = rectangle
* 2 = triangle
*/
private final GeometryForm[] form = new GeometryForm[3];
/*
* 0 = nothing
* 1 = circle
* 2 = rectangle
* 3 = triangle
*/
private int object = 0;
/**
* Launch the application
* @param args
*/
public static void main(final String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
final myGF frame = new myGF();
frame.setVisible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public myGF() {
super("Geometry Forms");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
initGUI();
} catch (final Throwable e) {
e.printStackTrace();
}
}
private void initGUI() throws Exception {
/**
* paint on a panel
*/
panel = new JPanel(new FlowLayout()){
public void paintComponent(final Graphics g) {
super.paintComponent(g);
/*
* Create the form if it is not created yet
* AND
* Create if --object-- agree with the form
*/
if (object == 3) {
if (form[2] == null) {
form[2] = new Triangle(100, 100, 140, 150, 60, 150);
}
form[2].paint(g);
}
}
};
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
move(e);
}
});
getContentPane().add(panel, BorderLayout.CENTER);
button_triangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
object = 3;
panel.repaint();
}
});
panel.add(button_triangle);
button_display.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
form[2].display();
panel.repaint();
}
});
panel.add(button_display);
button_hide.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
form[2].hide();
panel.repaint();
}
});
panel.add(button_hide);
}
/**
* Move the geometry form which is indicated
* @param e
*/
protected void move(final MouseEvent e) {
final int x = e.getX();
final int y = e.getY();
if (object == 3) {
form[2].go(x, y);
}
panel.repaint();
}
}
Code:
import java.awt.Graphics;
/**
* Class to create a triangle
* @author antoras
*
*/
public class Triangle extends GeometryForm {
private int xPos2;
private int xPos3;
private int yPos2;
private int yPos3;
private int[] xPositions = {xPos, xPos2, xPos3};
private int[] yPositions = {yPos, yPos2, yPos3};
public Triangle(final int x1, final int y1, final int x2, final int y2, final int x3, final int y3) {
xPositions[0] = x1;
yPositions[0] = y1;
xPositions[1] = x2;
yPositions[1] = y2;
xPositions[2] = x3;
yPositions[2] = y3;
}
@Override
protected void paint(Graphics g) {
if (show) {
g.drawPolygon(xPositions, yPositions, xPositions.length);
}
}
@Override
protected void go(final int x, final int y) {
if (xPositions[0] < x) {
xPos = xPos + stepsize;
xPos2 = xPos2 + stepsize;
xPos3 = xPos3 + stepsize;
}
if (xPositions[0] > x) {
xPos = xPos - stepsize;
xPos2 = xPos2 - stepsize;
xPos3 = xPos3 - stepsize;
}
if (yPositions[0] < y) {
yPos = yPos + stepsize;
yPos2 = yPos2 + stepsize;
yPos3 = yPos3 + stepsize;
}
if (yPositions[0] > y) {
yPos = yPos - stepsize;
yPos2 = yPos2 - stepsize;
yPos3 = yPos3 - stepsize;
}
}
}