Home > Blockchain >  How Can Turn Output To Red and Green
How Can Turn Output To Red and Green

Time:11-06

Can You Guys Help me

i need the Change Output Checkbox to Green And Red color image

like input 1 and 2 checked color green if one input checked turn red

i dont know how to do this is for my college but they dont teach me how to do that i cant find a good docs for that

Output


class AndGate extends JFrame {

    JFrame frame = new JFrame("And Gate");
    JCheckBox input1 = new JCheckBox("Input 1");
    JCheckBox input2 = new JCheckBox("Input 2");
    JCheckBox output = new JCheckBox("Output");
    JPanel outputPanel = new JPanel();

    public AndGate() {

        input1.addActionListener(actionEvent -> {
            updateOutputState();
        });


        input2.addActionListener(actionEvent -> {
            updateOutputState();
        });

        createFrame();
    }

    private void createFrame() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.add(input1);
        inputPanel.add(input2);
        frame.add(inputPanel);


        outputPanel.add(output);
        frame.add(outputPanel);


        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private void updateOutputState() {
        if(input1.isSelected() && input2.isSelected()) {
            output.setSelected(true);
        } else {
            output.setSelected(false);
        }
        outputPanel.repaint();
    }
}

class RunAndGate {
    public static void main(String[] args) {
        new AndGate();
    }
}

CodePudding user response:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class AndGate extends JPanel {

    JFrame frame = new JFrame("And Gate");
    JCheckBox input1 = new JCheckBox("Input 1");
    JCheckBox input2 = new JCheckBox("Input 2");
    JPanel outputPanel = new Box(Color.PINK);

    public AndGate() {

        input1.addActionListener(actionEvent -> {
            updateOutputState();
        });


        input2.addActionListener(actionEvent -> {
            updateOutputState();
        });

        createFrame();
    }

    private void createFrame() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.add(input1);
        inputPanel.add(input2);
        frame.add(inputPanel);
        frame.add(outputPanel);


        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private void updateOutputState() {
        if(input1.isSelected() && input2.isSelected()) {
            frame.remove(outputPanel);
            this.outputPanel = new Box(Color.GREEN);
            frame.add(outputPanel);
        } else {
            frame.remove(outputPanel);
            this.outputPanel = new Box(Color.RED);
            frame.add(outputPanel);
        }
        frame.revalidate();
        frame.repaint();
    }
}

class Box extends JPanel {

    Color color;
    public Box(Color color) {
        this.color = color;
    }

    public Dimension getPreferredSize() {
        return new Dimension(200,200);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        g2.setPaint(color);
        Rectangle2D rect=new Rectangle2D.Double(20,20,200,200);
        g2.draw(rect);
        g2.fill(rect);
    }
}

class RunAndGate {
    public static void main(String[] args) {
        new AndGate();
    }
}

CodePudding user response:

Don't extend JFrame but extend JPanel. That can serve as your output panel. Then just add the CheckBoxes and small panel to that main panel.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class AndGate extends JPanel {

    JFrame frame = new JFrame("And Gate");
    JCheckBox input1 = new JCheckBox("Input 1");
    JCheckBox input2 = new JCheckBox("Input 2");
    JPanel output = new JPanel();
    
    public AndGate() {

        input1.addActionListener(actionEvent -> {
            updateOutputState();
        });

        input2.addActionListener(actionEvent -> {
            updateOutputState();
        });

        createFrame();
    }

    private void createFrame() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        output.setPreferredSize(new Dimension(200,200));
        output.setVisible(true);
        output.setBackground(Color.red);
        add(input1);
        add(input2);
        add(output);
        
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null); // center on screen
        frame.setVisible(true);
        repaint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);

    }

    private void updateOutputState() {
       
        if(input1.isSelected() && input2.isSelected()) {
           output.setBackground(Color.green); 
        } else {
            output.setBackground(Color.red);
        }
        
    }
}

public class RunAndGate {
    public static void main(String[] args) {
        new AndGate();
    }
}
  • Related