Abwächselnd ein Punkt und ein x anzeigen

Hallo Leute,

es geht darum, dass ich ein Programm schreibe, mit dem abwchselnd ein Punkt (2 Pixel gross) und ein x (der sich in einem JLabel befindet) mittig auf einem Frame anzeigen werden soll. Beim Laden des Programms ist zuerst das x sichtbar. Nach drücken eines Start-Buttons erscheint dann der Punkt, verschwindet aber nach 2 sekunden und der x erscheint wieder. Für weitere Durchäufe verschwindet der Start-Buttons. Stattdessen hat man dann zwei Radiobuttons. Ich habs zwar soweit, dass das x mittig angezeigt wird, wenn ich jedoch den Punkt auf den JPanel adde verschwindet auch der x. Das Punkt ist dann auch nicht sichtbar.

Der Code denn ich geschrieben habe ist folgenderm:
Java:
package Aufgaben;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JLabel;

public class Aufgabe1 extends JFrame implements ActionListener {

    private JPanel contentPane;
    private JPanel checkPanel;
    private JPanel pointPanel;
   
    private JRadioButton rdbtnJa;
    private JRadioButton rdbtnNein;
    private ButtonGroup btnGroup;
   
    private JButton btnStart;
    private JLabel pointstr;
    private drawPoint point;
   
    private String status;
    private String answer;
    private int diff;
    private int sec;
    private int count;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Aufgabe1 frame = new Aufgabe1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Aufgabe1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 700, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        btnStart = new JButton("Start");
        btnStart.setBounds(0, 0, 684, 23);
        contentPane.add(btnStart);
       
            checkPanel = new JPanel();
            checkPanel.setBounds(0, 229, 684, 33);
            checkPanel.setVisible(false);;
            contentPane.add(checkPanel);

            btnGroup = new ButtonGroup();
           
            rdbtnJa = new JRadioButton("Ja");
            rdbtnJa.setActionCommand("RadioButton_Ja");
            rdbtnJa.addActionListener(this);
            checkPanel.add(rdbtnJa);
            btnGroup.add(rdbtnJa);
           
            rdbtnNein = new JRadioButton("Nein");
            rdbtnNein.setActionCommand("RadioButton_Nein");
            rdbtnNein.addActionListener(this);
            checkPanel.add(rdbtnNein);
            btnGroup.add(rdbtnNein);
           
        pointPanel = new JPanel(new BorderLayout());
        pointPanel.setBackground(Color.BLACK);
        pointPanel.setBounds(0, 23, 684, 209);
        contentPane.add(pointPanel);
       
        pointstr = new JLabel("X");
        pointstr.setForeground(Color.WHITE);
        pointstr.setVerticalAlignment(JLabel.CENTER);
        pointstr.setHorizontalAlignment(JLabel.CENTER);
        pointPanel.add(pointstr, BorderLayout.CENTER);
       
        point = new drawPoint();
        point.setBackground(new Color(sec,sec,sec));
        point.setPreferredSize(new Dimension(700, 300));
        pointPanel.add(point, BorderLayout.CENTER);
        point.setVisible(false);
       
        answer = new String();
        status = new String();
        diff = 0;
        sec = 0;
        count = 0;
        run();
    }
   
    public void run() {
       
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createLightPoint();
               
                btnStart.setEnabled(false);
                btnStart.setVisible(false);
               
                pointPanel.setBounds(0, 0, 684, 242);
               
            }
        });
    }
   
    private void createLightPoint() {
        btnGroup.clearSelection();
       
        point.setForeground(new Color(sec,sec,sec));
        setVisibility(true);

       
        Thread th = new Thread(new Runnable() {
            public void run() {
                try { Thread.sleep(2000); } catch (InterruptedException ex) {}
                setVisibility(false);
            }
        });
        th.start();
    }
    private void setVisibility(final boolean value) {
        if (SwingUtilities.isEventDispatchThread()) {
            pointstr.setVisible(!value);
            checkPanel.setVisible(!value);
//            point.setVisible(value);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() { setVisibility(value); }
            });
        }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if((ae.getActionCommand().equals("RadioButton_Ja") || ae.getActionCommand().equals("RadioButton_Nein")) && count <= 6) {
            if(ae.getActionCommand().equals("RadioButton_Ja")) {
                if(status.equals("ueberschwellig")) {
                    diff = -20;
                } else {
                    sec = 255;
                    diff = 0;
                    status = "ueberschwellig";
                }
                answer = "JA";
               
            } else if(ae.getActionCommand().equals("RadioButton_Nein")) {
                if(status.equals("ueberschwellig")) {
                    sec = 0;
                    diff = 0;
                    status = "unterschwellig";
                } else {
                    diff = 20;
                }
                answer = "NEIN";
            }
        }
       
        System.out.println("Light Point. status: " + status + " sec: " + sec + " answer: " + answer + " diff: " + diff);
       
        createLightPoint();
    }
}
class drawPoint extends JLabel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D pointer = (Graphics2D) g;

        pointer.setColor(Color.WHITE);
        pointer.drawOval(0, 0, 1, 1);
//        setSize(1, 1);
      }
}
Ich hoffe ihr könnt mir helfen.

Lg Padawan
 
Ich hoffe ihr könnt mir helfen.
Java:
import javax.swing.JFrame;

public class start {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("draw point at center");
        frame.add(new PointFlash(400, 400));
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class PointFlash extends JPanel implements ActionListener {
    private Timer timer = null;
    private JButton start = new JButton("start");
    private JLabel labelX = new JLabel("X");
    private Point point = new Point();
    private int pointSize = 4;
    private Color color = Color.YELLOW;

    public PointFlash(int width, int height) {
        setLayout(null);
        setBackground(Color.DARK_GRAY);
        start.setPreferredSize(new Dimension(width / 4, start.getFont().getSize() * 2));
        labelX.setBorder(BorderFactory.createLineBorder(color));
        labelX.setHorizontalAlignment(JLabel.CENTER);
        labelX.setForeground(color);
        add(labelX);
        add(start);
        start.addActionListener(this);
        updateSize(width, height);
    }

    /**
     * Receives messages from button start and point's timer.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if (src == start)
            showPoint(2000);
        if (src == timer)
            showX();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // draw point only if labelX is not visible
        if (!labelX.isVisible()) {
            g.setColor(color);
            g.fillOval(point.x, point.y, pointSize, pointSize);
        }
    }

    /**
     * Sets lableX invisible and starts a timer which calls
     * {@link #actionPerformed(ActionEvent)} after duration.
     *
     * @param duration
     */
    private void showPoint(int duration) {
        labelX.setVisible(false);
        timer = new Timer(duration, this);
        timer.start();
    }

    /**
     * Stops timer activated by {@link #showPoint(int)} and sets labelX visible.
     */
    private void showX() {
        if (timer != null)
            timer.stop();
        timer = null;
        labelX.setVisible(true);
    }

    private void updateSize(int width, int height) {
        setPreferredSize(new Dimension(width, height));
        int dimX = (int) (start.getFont().getSize() * 2);
        labelX.setBounds((width - dimX) / 2, (height - dimX) / 2, dimX, dimX);
        int startWidth = width / 5;
        start.setBounds((width - startWidth) / 2, (height - dimX), startWidth, dimX);
        point.x = width / 2;
        point.y = height / 2;
    }
}
 
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben