K
k-s
Gast
Code:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class LineRectOval
{
public static void main(String args[])
{new MyFrame("LineRectOval");}
}
class MyFrame extends JFrame implements ActionListener
{
private Point a = null;
private Point b = null;
JButton bLine = new JButton("Line");
JButton bRectangle = new JButton("Rectangle");
JButton bOval = new JButton("Oval");
JPanel zFlaeche = new JPanel(new GridLayout(1,1));
JPanel panel = new JPanel();
MyFrame(String s)
{
super(s);
getContentPane().setLayout(new BorderLayout());
panel.add(bLine); panel.add(bRectangle); panel.add(bOval);
getContentPane().add(panel, BorderLayout.SOUTH);
getContentPane().add(zFlaeche, BorderLayout.CENTER);
zFlaeche.setBackground(Color.WHITE);
zFlaeche.setBorder(BorderFactory.createLineBorder(Color.BLACK));
bLine.addActionListener(this);bLine.setActionCommand("LINE");
bRectangle.addActionListener(this);bRectangle.setActionCommand("RECTANGLE");
bOval.addActionListener(this);bOval.setActionCommand("OVAL");
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{if(a==null){a = e.getPoint();} else{b = e.getPoint();}};
});
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation( (d.width-900)/2 , (d.height-900)/2 );
setSize(900, 900);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if((a!=null)&(b!=null))
{
int obenlinks_x,obenlinks_y,breite,hoehe;
obenlinks_x = (int)Math.min(a.getX(), b.getX());
obenlinks_y = (int)Math.min(a.getY(), b.getY());
breite = (int)Math.abs(a.getX()-b.getX());
hoehe = (int)Math.abs(a.getY()-b.getY());
if (e.getActionCommand().equals("LINE"))
{zFlaeche.getGraphics().drawLine((int)a.getX(),(int)a.getY(),(int)b.getX(),(int)b.getY());}
else
if (e.getActionCommand().equals("RECTANGLE"))
{zFlaeche.getGraphics().drawRect(obenlinks_x,obenlinks_y,breite,hoehe);}
else
if (e.getActionCommand().equals("OVAL"))
{ int radius = (int)Math.sqrt((breite*breite)+(hoehe*hoehe));
zFlaeche.getGraphics().drawOval
((int)a.getX()-radius,(int)a.getY()-radius,2*radius,2*radius);}
}
a = null; b = null;
}
}
Bei mir verschieben sich alle Figuren auf der Zeichenfläche um paar Pixeln nach unten-rechts, habe keine Ahnung warum Ö_ö