Home > Software engineering >  XNOR Gate With Java
XNOR Gate With Java

Time:11-06

Can You Guys Help me

like input 1 or 2 checked color RED if both checked input checked turn green

its for XNOR gate logic gates in java

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

i tried so much things but when i try its keep give red when i check both

class XNOR extends JPanel {

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

    public XNOR() {

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

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

        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();
    }

    private void updateOutputState1() {
        if(input1.isSelected() || input2.isSelected()){
        frame.remove(outputPanel);
        this.outputPanel = new Box(Color.RED);
        frame.add(outputPanel);
    }
    else {
        frame.remove(outputPanel);
        this.outputPanel = new Box(Color.GREEN);
        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 RunXNOR {
    public static void main(String[] args) {
        new XNOR();
    }
}


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");
    Box outputPanel = new Box();

    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.repaint();
        frame.add(outputPanel);


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

    private void updateOutputState() {
        if(input1.isSelected() && input2.isSelected()) {
            this.outputPanel.changeColor(Color.GREEN);
        } else {
            this.outputPanel.changeColor(Color.RED);
        }
    }
}

class Box extends JPanel {

    Graphics2D g2;
    Color color = Color.RED;
    Rectangle2D rect=new Rectangle2D.Double(20,20,200,200);

    public Box() {
    }

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

    public void changeColor(Color color) {
        this.color = color;
        g2.setPaint(color);
        g2.fill(rect);
        this.repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g2 = (Graphics2D) g;
        g2.setPaint(color);
        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