Ich habe folgendes Problem: ich möchte auf ein geladenes Bild einen Punkt zeichnen, weiss aber nicht, wie ich in der Methode "mouseClicked" die Methode "draw" aufrufen soll d.h. was genau ich in die mouseClicked-Methode reinschreiben muss, damit draw aufgerufen wird. Wär nett, wenn sich Jemand meinen Code mal ansieht.Bin dankbar für jede Hilfe.
Code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class HandManager extends JFrame implements ActionListener {
private static HandManager hand;
private JFileChooser fc;
private PictureCanvas2 ph;
private JButton b;
public HandManager() {
super ("Fenster");
fc = new JFileChooser();
ph = new PictureCanvas2();
//ph.setPreferedSize(300,300);
Container c = getContentPane();
c.setLayout (new BorderLayout(3,3));
b = new JButton ( "Button");
b.addActionListener( this);
c.add(b,BorderLayout.NORTH);
//Component ph = new PictureHandler();
getContentPane().add(ph,BorderLayout.CENTER);
pack();
setSize(300,300);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main (String [] args) {
hand = new HandManager();
}
public void actionPerformed(ActionEvent event) {
if (event.getSource()==b) {
int returnVal = fc.showOpenDialog(HandManager.this);
ph.showImage(fc.getSelectedFile());
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
class PictureCanvas2 extends JPanel implements MouseListener {
class PaintGeometricStuff {
Graphics myGraphic;
public PaintGeometricStuff(Graphics g) {
myGraphic = g;
}
public void draw(Graphics myGraphics) {
myGraphics.setColor(Color.black);
myGraphics.fillOval( 4,5,5,4 );
}
}
BufferedImage bi;
public void paintComponent(Graphics g) {
if (bi != null) {
g.drawImage(bi,0,0,bi.getWidth(),bi.getHeight ( ),Color.white,null);
}
}
public void showImage(File f) {
try {
bi = ImageIO.read(f);
}
catch (Exception e) {
System.out.println("Fehler beim Bildladen");
}
repaint();
}
public void mouseClicked(MouseEvent arg0) {
PaintGeometricStuff.draw() ;
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}