Hallo, ich habe eine JFrame und in dem befindet sich ein JPanel. In das JPanel habe ich ein Image hinzugefügt.
Ich möchte jetzt auf einem Teilbereich des Images einmal das Image rotor1 und einmal rotor2 darstellen.
Kann mir da jemand helfen. Ich komme nicht mehr weiter. Unten meine drei Klassen.
lg und vielen Dank
Stefan
Ich möchte jetzt auf einem Teilbereich des Images einmal das Image rotor1 und einmal rotor2 darstellen.
Kann mir da jemand helfen. Ich komme nicht mehr weiter. Unten meine drei Klassen.
lg und vielen Dank
Stefan
Code:
// diese Klasse ist meine Hauptklasse, in dieser wird das hintergrundBild auf dem JPanel dargestellt
public class MainFrame extends JFrame {
private Image hintergrundBild;
private Image rotor1;
private Image rotor2;
private JankSchalter schalter;
private JankSchalter taster;
private JButton button1;
private ImageIcon buttonAus;
private ImageIcon buttonEin;
private ImageIcon tasterAus;
private ImageIcon tasterEin;
private JLabel label;
private JPanel p;
private JPanel panelHMI;
private RotorDrehung rotor;
public MainFrame()
{
super("MainFrame");
hintergrundBild = getToolkit().getImage(System.getProperty("user.dir") + "\\pics\\hintergrundBild.png");
rotor1 = getToolkit().getImage(System.getProperty("user.dir") + "\\pics\\rotor1.png");
rotor2 = getToolkit().getImage(System.getProperty("user.dir") + "\\pics\\rotor2.png");
buttonAus = new ImageIcon(System.getProperty("user.dir") + "\\pics\\aus.gif");
buttonEin = new ImageIcon(System.getProperty("user.dir") + "\\pics\\ein.gif");
tasterAus = new ImageIcon(System.getProperty("user.dir") + "\\pics\\ausbutton.gif");
tasterEin = new ImageIcon(System.getProperty("user.dir") + "\\pics\\einbutton.gif");
MediaTracker mt = new MediaTracker(this);
mt.addImage(hintergrundBild, 0);
mt.addImage(rotor1, 0);
mt.addImage(rotor2, 0);
try { mt.waitForAll();}
catch (InterruptedException e) {}
panelHMI = new JPanel();
panelHMI.setLayout(new BorderLayout());
panelHMI.setBackground(Color.GRAY);
panelHMI.setBounds(10, 10, hintergrundBild.getWidth(this), hintergrundBild.getHeight(this));
panelHMI.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
p = new BildPanel(hintergrundBild, rotor2); // hier wird das hintergrundbild dargestellt
p.setBounds(10,10,200,200);
schalter = new JankSchalter("EinAusSchalter", "Style1", buttonAus, 5, "Times New Roman", 12);
schalter.setLocation(hintergrundBild.getWidth(this)-200, hintergrundBild.getHeight(this)-180);
schalter.addActionListener(new GUIControl(schalter, buttonAus, buttonEin));
taster = new JankSchalter("Taster", "Style1", tasterAus, 5, "Times New Roman", 12);
taster.setLocation(hintergrundBild.getWidth(this)-300, hintergrundBild.getHeight(this)-180);
taster.addMouseListener(new GUIControl(taster, tasterAus, tasterEin));
button1 = new JButton("Test");
button1.setLocation(10,10);
button1.setSize(80, 20);
button1.addActionListener(new GUIControl(schalter, buttonAus, buttonEin));
Thread th = new Thread();
th.start();
add(panelHMI);
panelHMI.add(schalter);
panelHMI.add(taster);
panelHMI.add(button1);
panelHMI.add(p); // hier wird das hintergrundBild zu panelHMI hinzugefügt
rotor = new RotorDrehung(p, rotor1, rotor2, panelHMI); // hier möchte ich eben die Klasse
//RotorDrehung aufrufen, um den Thread für die
//abwechselnde Darstellung von rotor1 und rotor2 zu
//erreichen
rotor.start();
}
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame();
mainFrame.setLocation(120, 20);
mainFrame.setSize(800, 700);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(null);
mainFrame.setAlwaysOnTop(true);
mainFrame.setVisible(true);
}
}
//Klasse zum Anzeigen der Images
class BildPanel extends JPanel {
private Image hintergrundBild;
private Image rotor;
public BildPanel(Image hintergrundBild, Image rotor)
{
this.hintergrundBild = hintergrundBild;
this.rotor = rotor;
}
public BildPanel(Image rotor)
{
this.rotor = rotor;
}
public void setRotor(Image rotor)
{
System.out.println("in setrotor");
this.rotor = rotor;
repaint();
}
public void paintComponent(Graphics g)
{
System.out.println("in paint");
super.paintComponent(g);
g.drawImage(hintergrundBild,0,0,getWidth(),getHeight(), this);
g.drawImage(rotor,288,388,this);
}
}
//Klasse zur abwechselden Darstellung zweier Images auf dem Panel panelHMI
class RotorDrehung extends Thread {
private JPanel p;
private JPanel panelHMI;
private Image rotor1;
private Image rotor2;
private BildPanel bildPanel = new BildPanel(rotor2);
public RotorDrehung(JPanel p, Image rotor1, Image rotor2, JPanel panelHMI)
{
this.p = p;
this.panelHMI = panelHMI;
this.rotor1 = rotor1;
this.rotor2 = rotor2;
}
public void run()
{
try {
while (true)
{
Thread.sleep(3000);
bildPanel.setRotor(rotor1); // hier soll eben irgendwie das Image rotor1 an die Klasse BildPanel gegeben werden und auf meinem panelHMI dargestellt werden -> das funktioniert aber nicht, vielleicht hat da jemand eine Idee,
bildPanel.repaint();
Thread.sleep(3000);
bildPanel.setRotor(rotor2); // hier dann rotor2
bildPanel.repaint();
}
}
catch (Exception th)
{
System.out.println("Fehler: " + th);
}
}
}