So hi, wie könnte ich in den folgenden Code eine methode einbauen, damit "Graphics g" wenn ich auf das menu item "circle1" einen Kreis zeichnet, ??? ???:L
Kann mir einer helfen?
Kann mir einer helfen?
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.*;
/* -------------------------------
Main Frame in program PaintShop
-------------------------------*/
public class PaintShop extends JFrame
implements MouseListener, MouseMotionListener {
static int outFileSeq = 0;
static String outFileBase = "ScreenShot.Race_Alpträume_";
static NumberFormat nf = NumberFormat.getInstance();
static int circleradius = 0;
private Color color = Color.blue;
private DrawingPanel drawingPanel;
public PaintShop()
{
super( "PaintShop first version" );
Container c = getContentPane( );
c.setLayout (null);
//////////////////////////////////////////////
JMenuBar a = new JMenuBar();
a.setBounds(0,0,750,20);
add(a);
JMenu start = new JMenu("Start");
a.add(start);
JMenuItem save = new JMenuItem("Unter ''C:/Math/Data'' Speichern");
start.add(save);
save.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{ BufferedImage bi = null;
try {
bi = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
} catch (HeadlessException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (AWTException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
nf.setMaximumFractionDigits(10);
try {
ImageIO.write( bi, "jpg", new File("F:/"+ outFileBase + nf.format(outFileSeq++)+".arschloch"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}});
JMenuItem exit = new JMenuItem("Beenden");
start.add(exit);
exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e)
{setVisible(false);}});
//
JMenuBar Circle = new JMenuBar();
Circle.setBounds(0,20,750,20);
add(Circle);
JMenu Circle1 = new JMenu("Kreis zeichen");
Circle.add(Circle1);
JMenu CircleSize = new JMenu("Kreis Radius");
Circle.add(CircleSize);
JMenuItem a1 = new JMenuItem("Kreis Zeichnung bestätigen");
Circle1.add(a1);
a1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e)
{circleradius=1;}});
//////////////////////////////////////////////
drawingPanel = new DrawingPanel(0,0);
drawingPanel.setBounds(0,0,750,500);
addMouseListener(this);
addMouseMotionListener(this);
c.add(drawingPanel);
setSize( 750, 500 );
setVisible(true);
}
/* ------------------------------
Listening for mouse activities
------------------------------*/
public void mouseClicked ( MouseEvent e )
{
}
public void mouseEntered ( MouseEvent e )
{
}
public void mouseExited ( MouseEvent e )
{
}
public void mousePressed ( MouseEvent e )
{
drawingPanel.setPaint();
}
public void mouseReleased ( MouseEvent e )
{
drawingPanel.setNotPaint();
}
/* -----------------------------
Listening for mouse movements
-----------------------------*/
public void mouseMoved ( MouseEvent e )
{
}
public void mouseDragged ( MouseEvent e )
{
drawingPanel.setX( e.getX() );
drawingPanel.setY( e.getY() );
drawingPanel.paintComponent(getGraphics());
}
/* ---------------------------------------
Class DrawingPanel
This Panel is used to draw a picture on
---------------------------------------*/
private class DrawingPanel extends JPanel {
private int xValue;
private int yValue;
private boolean paint;
public DrawingPanel(int x, int y) {
xValue = x;
yValue = y;
paint = false;
}
public void setX(int x) {
xValue = x;
}
public void setY(int y) {
yValue = y;
}
public void setPaint() {
paint = true;
}
public void setNotPaint() {
paint = false;
}
public void paintComponent(final Graphics g)
{
g.setColor(color);
g.drawLine(xValue, yValue, 4, 4);
}
}
/* -----------
MainProgram
-----------*/
public static void main( String args[] )
{
PaintShop app = new PaintShop();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
}