I can’t get this code to work, after compilation the window just doesn’t appear, although the window is active in the task manager. Perhaps someone will find my code strange, I just want to draw relative to the adjacency matrix.
`#include <JuceHeader.h>
#include
#include
#include
#include
#include <juce_gui_basics/juce_gui_basics.h>
using namespace juce;
class WeightedGraph
{
std::vector<std::vector> adjacencyMatrix;
public:
WeightedGraph() { adjacencyMatrix.resize(0, std::vector<int>(0, 0)); }
void addVertex()
{
int numVertices = adjacencyMatrix.size();
for (int i = 0; i < numVertices; i++) adjacencyMatrix[i].push_back(0);
adjacencyMatrix.push_back(std::vector<int>(numVertices + 1, 0));
}
void removeVertex(int vertex)
{
int numVertices = adjacencyMatrix.size();
if (vertex >= numVertices) { std::cout << "Invalid vertex!" << std::endl; return; }
adjacencyMatrix.erase(adjacencyMatrix.begin() + vertex);
for (int i = 0; i < numVertices - 1; i++) adjacencyMatrix[i].erase(adjacencyMatrix[i].begin() + vertex);
}
void addEdge(int source, int destination, int weight)
{
int numVertices = adjacencyMatrix.size();
if (source >= numVertices || destination >= numVertices) { std::cout << "Invalid vertices!" << std::endl; return; }
adjacencyMatrix[source][destination] = weight;
}
void removeEdge(int source, int destination)
{
int numVertices = adjacencyMatrix.size();
if (source >= numVertices || destination >= numVertices) { std::cout << "Invalid vertices!" << std::endl; return; }
adjacencyMatrix[source][destination] = 0;
}
void printAdjacencyMatrix()
{
int numVertices = adjacencyMatrix.size();
for (int i = 0; i < numVertices; i++)
{
for (int j = 0; j < numVertices; j++) std::cout << adjacencyMatrix[i][j] << " ";
std::cout << std::endl;
}
}
auto getAdjacencyMatrix() { return adjacencyMatrix; }
void DFS(int startVertex)
{
int numVertices = adjacencyMatrix.size();
std::vector<bool> visited(numVertices, false);
std::stack<int> stack;
visited[startVertex] = true;
stack.push(startVertex);
while (!stack.empty())
{
int currentVertex = stack.top();
stack.pop();
std::cout << currentVertex << " ";
for (int i = 0; i < numVertices; i++)
if (adjacencyMatrix[currentVertex][i] != 0 && !visited[i])
{
visited[i] = true;
stack.push(i);
}
}
}
void BFS(int startVertex)
{
int numVertices = adjacencyMatrix.size();
std::vector<bool> visited(numVertices, false);
std::queue<int> queue;
visited[startVertex] = true;
queue.push(startVertex);
while (!queue.empty())
{
int currentVertex = queue.front();
queue.pop();
std::cout << currentVertex << " ";
for (int i = 0; i < numVertices; i++)
if (adjacencyMatrix[currentVertex][i] != 0 && !visited[i])
{
visited[i] = true;
queue.push(i);
}
}
}
std::vector<int> Dijkstra(int startVertex)
{
int numVertices = adjacencyMatrix.size();
std::vector<int> distance(numVertices, std::numeric_limits<int>::max());
std::vector<bool> visited(numVertices, false);
distance[startVertex] = 0;
for (int count = 0; count < numVertices - 1; count++) {
int minDistance = std::numeric_limits<int>::max();
int minVertex = -1;
for (int v = 0; v < numVertices; v++)
if (!visited[v] && distance[v] <= minDistance) { minDistance = distance[v]; minVertex = v; }
if (minVertex == -1) break;
visited[minVertex] = true;
for (int v = 0; v < numVertices; v++)
if (!visited[v] && adjacencyMatrix[minVertex][v] != 0 &&
distance[minVertex] != std::numeric_limits<int>::max() &&
distance[minVertex] + adjacencyMatrix[minVertex][v] < distance[v]) {
distance[v] = distance[minVertex] + adjacencyMatrix[minVertex][v];
}
} return distance;
}
};
class Creative_1Application : public JUCEApplication
{
static WeightedGraph graph;
public:
Creative_1Application() {}
const String getApplicationName() override { return ProjectInfo::projectName; }
const String getApplicationVersion() override { return ProjectInfo::versionString; }
bool moreThanOneInstanceAllowed() override { return true; }
void initialise(const String& commandLine) override
{
mainWindow.reset(new MainWindow(getApplicationName()));
mainWindow->setVisible(true);
}
void shutdown() override { mainWindow = nullptr; }
void systemRequestedQuit() override { quit(); }
void anotherInstanceStarted(const String& commandLine) override {}
class MainWindow : public juce::Component, private juce::Button::Listener
{
TextButton addVertexButton;
TextButton removeVertexButton;
TextButton addEdgeButton;
TextButton removeEdgeButton;
TextEditor vertexInput;
TextEditor edgeSourceInput;
TextEditor edgeDestinationInput;
TextEditor edgeWeightInput;
public:
void resized() override
{
// Здесь определите размеры и позиции всех компонентов вашего окна
}
void paint(Graphics& g) override
{
// Здесь определите, как компоненты должны быть отрисованы на экране
}
MainWindow(String name)
{
centreWithSize(600, 600);
setVisible(true);
addAndMakeVisible(addVertexButton);
addVertexButton.setButtonText("Добавить вершину");
addVertexButton.setBounds(10, 10, 150, 30);
addVertexButton.addListener(this);
addAndMakeVisible(removeVertexButton);
removeVertexButton.setButtonText("Удалить вершину");
removeVertexButton.setBounds(10, 50, 150, 30);
removeVertexButton.addListener(this);
addAndMakeVisible(addEdgeButton);
addEdgeButton.setButtonText("Добавить ребро");
addEdgeButton.setBounds(10, 90, 150, 30);
addEdgeButton.addListener(this);
addAndMakeVisible(removeEdgeButton);
removeEdgeButton.setButtonText("Удалить ребро");
removeEdgeButton.setBounds(10, 130, 150, 30);
removeEdgeButton.addListener(this);
addAndMakeVisible(vertexInput);
vertexInput.setBounds(200, 50, 50, 30);
addAndMakeVisible(edgeSourceInput);
edgeSourceInput.setBounds(200, 90, 50, 30);
addAndMakeVisible(edgeDestinationInput);
edgeDestinationInput.setBounds(260, 90, 50, 30);
addAndMakeVisible(edgeWeightInput);
edgeWeightInput.setBounds(320, 90, 50, 30);
}
void closeButtonPressed() { JUCEApplication::getInstance()->systemRequestedQuit(); }
void buttonClicked(Button* button) override
{
if (button == &addVertexButton)
{
graph.addVertex();
}
else if (button == &removeVertexButton)
{
int vertex = vertexInput.getText().getIntValue();
graph.removeVertex(vertex);
}
else if (button == &addEdgeButton)
{
int source = edgeSourceInput.getText().getIntValue();
int destination = edgeDestinationInput.getText().getIntValue();
int weight = edgeWeightInput.getText().getIntValue();
graph.addEdge(source, destination, weight);
}
else if (button == &removeEdgeButton)
{
int source = edgeSourceInput.getText().getIntValue();
int destination = edgeDestinationInput.getText().getIntValue();
graph.removeEdge(source, destination);
}
}
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainWindow)
// Вспомогательные функции для получения данных от пользователя
int getVertexToRemoveFromUser()
{
// Здесь можно реализовать ввод номера вершины через диалоговое окно или другим удобным способом
// Возвращаем номер вершины, введенный пользователем
}
int getSourceFromUser()
{
// Здесь можно реализовать ввод номера исходной вершины через диалоговое окно или другим удобным способом
// Возвращаем номер исходной вершины, введенный пользователем
}
int getDestinationFromUser()
{
// Здесь можно реализовать ввод номера конечной вершины через диалоговое окно или другим удобным способом
// Возвращаем номер конечной вершины, введенный пользователем
}
int getWeightFromUser()
{
// Здесь можно реализовать ввод веса ребра через диалоговое окно или другим удобным способом
// Возвращаем вес ребра, введенный пользователем
}
};
private:
std::unique_ptr mainWindow;
void addVertex()
{
graph.addVertex();
}
void removeVertex(int vertex)
{
graph.removeVertex(vertex);
}
void addEdge(int source, int destination, int weight)
{
graph.addEdge(source, destination, weight);
}
void removeEdge(int source, int destination)
{
graph.removeEdge(source, destination);
}
};
WeightedGraph Creative_1Application::graph;
START_JUCE_APPLICATION(Creative_1Application)`