0% found this document useful (0 votes)
14 views

Code Java

Code java

Uploaded by

tenkampythagore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Code Java

Code java

Uploaded by

tenkampythagore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class CircuitBuilderApp extends JFrame {

private JPanel drawingPanel;


private ArrayList<Component> components;
private Component selectedComponent;
private JTextField componentNameInput;

public CircuitBuilderApp() {
setTitle("Circuit Builder");
setSize(1000, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

components = new ArrayList<>();

// Panel for drawing circuit


drawingPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawGrid(g);
for (Component component : components) {
component.draw(g);
}
}
};
drawingPanel.setBackground(Color.WHITE);
drawingPanel.setLayout(null); // Use null layout for absolute positioning
add(drawingPanel, BorderLayout.CENTER);

// Input field to select components by name


JPanel controlPanel = new JPanel();
JLabel selectLabel = new JLabel("Select Component by Name:");
componentNameInput = new JTextField(10);
JButton selectButton = new JButton("Select");
selectButton.addActionListener(e ->
selectComponentByName(componentNameInput.getText()));
controlPanel.add(selectLabel);
controlPanel.add(componentNameInput);
controlPanel.add(selectButton);
add(controlPanel, BorderLayout.NORTH);

// Mouse listener to handle component selection and editing


drawingPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
selectComponentAt(e.getX(), e.getY());
}
});

// Key listener to handle rotation of selected component


drawingPanel.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (selectedComponent != null && e.getKeyCode() == KeyEvent.VK_R
&& e.isControlDown()) {
rotateComponent(selectedComponent);
drawingPanel.repaint();
}
}
});

// Enable panel to receive focus for key events


drawingPanel.setFocusable(true);
drawingPanel.requestFocus();

setVisible(true);
}

// Method to draw grid on the drawing panel


private void drawGrid(Graphics g) {
int width = drawingPanel.getWidth();
int height = drawingPanel.getHeight();
g.setColor(Color.LIGHT_GRAY);
int gridSize = 20;
for (int x = 0; x <= width; x += gridSize) {
g.drawLine(x, 0, x, height);
}
for (int y = 0; y <= height; y += gridSize) {
g.drawLine(0, y, width, y);
}
}

// Method to select a component at given (x, y) coordinates


private void selectComponentAt(int x, int y) {
for (Component component : components) {
if (component.contains(x, y)) {
selectedComponent = component;
openAttributeEditor(selectedComponent);
return;
}
}
selectedComponent = null;
}

// Method to rotate a component by 90 degrees


private void rotateComponent(Component component) {
// Rotate the component
component.rotate();
}

// Method to select a component by its name


private void selectComponentByName(String componentName) {
for (Component component : components) {
if (component.getName().equals(componentName)) {
selectedComponent = component;
openAttributeEditor(selectedComponent);
return;
}
}
selectedComponent = null;
JOptionPane.showMessageDialog(this, "Component not found: " +
componentName, "Error", JOptionPane.ERROR_MESSAGE);
}

// Method to open attribute editor dialog for selected component


private void openAttributeEditor(Component component) {
AttributeEditorDialog dialog = new AttributeEditorDialog(this, component);
dialog.setVisible(true);
if (dialog.isConfirmed()) {
drawingPanel.repaint();
}
}

// Method to add a component to the drawing panel


public void addComponent(Component component) {
components.add(component);
drawingPanel.repaint();
}

public static void main(String[] args) {


SwingUtilities.invokeLater(CircuitBuilderApp::new);
}
}

import java.awt.*;
abstract class Component {
protected String name;
protected int x, y; // Position of the component

public Component(String name, int x, int y) {


this.name = name;
this.x = x;
this.y = y;
}

public abstract void draw(Graphics g);

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getX() {


return x;
}

public void setX(int x) {


this.x = x;
}

public int getY() {


return y;
}

public void setY(int y) {


this.y = y;
}

public boolean contains(int x, int y) {


// Implement logic to check if (x, y) is within component bounds
return false;
}

public void rotate() {


// Implement rotation logic (if needed)
}
}

class Resistor extends Component {


private double value; // Resistance value in ohms

public Resistor(String name, int x, int y, double value) {


super(name, x, y);
this.value = value;
}

@Override
public void draw(Graphics g) {
// Implement resistor drawing logic
}
}

class Inductor extends Component {


private double value; // Inductance value in henrys

public Inductor(String name, int x, int y, double value) {


super(name, x, y);
this.value = value;
}

@Override
public void draw(Graphics g) {
// Implement inductor drawing logic
}
}

class Capacitor extends Component {


private double value; // Capacitance value in farads

public Capacitor(String name, int x, int y, double value) {


super(name, x, y);
this.value = value;
}

@Override
public void draw(Graphics g) {
// Implement capacitor drawing logic
}
}

class ACGenerator extends Component {


private double amplitude; // Amplitude of AC signal
private double frequency; // Frequency of AC signal

public ACGenerator(String name, int x, int y, double amplitude, double frequency)


{
super(name, x, y);
this.amplitude = amplitude;
this.frequency = frequency;
}

@Override
public void draw(Graphics g) {
// Implement AC generator drawing logic
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AttributeEditorDialog extends JDialog {

private JTextField nameField;


private JButton okButton, cancelButton;
private boolean confirmed = false;

public AttributeEditorDialog(JFrame parent, Component component) {


super(parent, "Edit Component Attributes", true);
setSize(300, 200);
setLocationRelativeTo(parent);
setLayout(new BorderLayout());

JPanel inputPanel = new JPanel(new GridLayout(2, 2));


inputPanel.add(new JLabel("Name:"));
nameField = new JTextField(component.getName());
inputPanel.add(nameField);

// Add more input fields for other attributes if needed

add(inputPanel, BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();


okButton = new JButton("OK");
okButton.addActionListener(e -> {
confirmed = true;
component.setName(nameField.getText());
dispose();
});
buttonPanel.add(okButton);

cancelButton = new JButton("Cancel");


cancelButton.addActionListener(e -> {
confirmed = false;
dispose();
});
buttonPanel.add(cancelButton);

add(buttonPanel, BorderLayout.SOUTH);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

public boolean isConfirmed() {


return confirmed;
}
}

You might also like