Как я могу показать сообщение () на дисплее ()? - PullRequest
0 голосов
/ 24 февраля 2020
package messagedisplayterminal2;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 *
 * @author ADMIN
 */
public class Display extends JFrame {

    /**
     * Creates new form Display
     */

    private boolean isOn;
    private int messageCount;
    private int totalMessagePeriod;
    private int Ns = 0;

    private DrawingPanel panel = new DrawingPanel(1000, 1000);
    Graphics dp = panel.getGraphics();
    ImageIcon icon = new ImageIcon("/messagedisplayterminal2/myComputerImage.PNG\"");
    Image image = icon.getImage();

    public Display() {
        dp.drawImage(image,200,200,null);
        dp.setColor(Color.DARK_GRAY);
        dp.fillRect(212,214,575,336);

        sleep(1000);
        turnOn();
        sleep(1000);
        turnOff();
        sleep(1000);
        clearDisplay();
        sleep(1000);


        setVisible(true);

Как я могу showMessage() здесь на Display()

        initComponents();
    }



    public void turnOn() {
        isOn = true;
        clearDisplay();
    }

    public void turnOff() {
        isOn = false;
        clearDisplay();
    }

    public void clearDisplay() {
        if (isOn) {
            dp.setColor(Color.WHITE);
        }
        else {
            dp.setColor(Color.DARK_GRAY);
        }
        dp.fillRect(213,215,675,436);
    }

    public void showMessage (Message message) {


        Font myFont = new Font("Arial", Font.BOLD, message.getFontSize());
        FontMetrics fm= dp.getFontMetrics();
        dp.setFont(myFont);
        dp.setColor(Color.RED);
        dp.drawString("Hello World", 321, 231 + Ns);
        Ns += message.getFontSize();

        if (Ns == 300) {
            dp.setColor(Color.WHITE);
            dp.fillRect(213,215,675,436);
            Ns = message.getFontSize();
        }

        if (message.getPriority() == Message.MessagePriority.HIGH) {
            sleep(1000);
        }
        else if (message.getPriority() == Message.MessagePriority.MEDIUM ) {
            sleep(500);
        }
        else if (message.getPriority() == Message.MessagePriority.LOW) {
            sleep(250);
        }

    }

     public int getMessageCount() {
        return messageCount;
    }

    public int getTotalMessageLength() {
        return totalMessagePeriod;
    }
    public boolean getIsOn() {
        return isOn;
    }



    private void sleep(int sleepMs) {
        try {
            Thread.sleep(sleepMs);
        } catch (InterruptedException e) {

        }
    }



    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/messagedisplayterminal2/myComputerImage.PNG"))); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(41, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>                        

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
    }                                    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Display.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Display().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}
...