0% found this document useful (0 votes)
34 views3 pages

Code Source

This document contains code for a Java application that allows a user to visualize a hash table. It includes: 1) A HashtableDrawComponent class that draws the hash table visualization based on a size and name provided. 2) A Presentation class with a console method that builds a GUI with menus to get user input for the table size and a name to add. 3) The GUI calls the HashtableDrawComponent to display the visualization based on the user input.

Uploaded by

Tâsnim LÂ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Code Source

This document contains code for a Java application that allows a user to visualize a hash table. It includes: 1) A HashtableDrawComponent class that draws the hash table visualization based on a size and name provided. 2) A Presentation class with a console method that builds a GUI with menus to get user input for the table size and a name to add. 3) The GUI calls the HashtableDrawComponent to display the visualization based on the user input.

Uploaded by

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

package tn.usousse.eniso.ia1.stage.example.presentation.

view;

import tn.usousse.eniso.ia1.stage.example.presentation.model.Table;
import tn.usousse.eniso.ia1.stage.example.service.Service;

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

public class HashtableDrawComponent extends JComponent {


private int tableSize;
private String nameValue ;
private boolean added;
public HashtableDrawComponent(){
tableSize = 0;
}

public boolean isAdded() {


return added;
}

public void setSize(int size) {


tableSize = size;
}
public void setName(String name){
this.nameValue = name;
}

private void drawNode(Graphics2D g2d,int caseSize, int startX, int startY){


if(nameValue !=null){
Table table = new Table(tableSize);
Service service = new Service(table);
int i = service.hash(nameValue);
int lineY = startY +caseSize* i-100;
g2d.setStroke(new BasicStroke(1));
g2d.drawLine(startX+caseSize, lineY, startX + caseSize+50, lineY);
int rectY = lineY + caseSize;
g2d.drawRect(startX+caseSize+50, rectY-caseSize-30, caseSize -20,
caseSize -40);
g2d.drawString(nameValue, startX + caseSize + 75, lineY);
g2d.drawLine(startX + caseSize*2+30, lineY, startX + caseSize*2+100,
lineY);
int lineX = startX + caseSize*2+100;
g2d.drawLine(lineX, lineY+20, lineX, lineY-20);
g2d.drawLine(lineX, lineY+10, lineX +10, lineY+20 );
g2d.drawLine(lineX, lineY-10, lineX +10, lineY );
added = service.add(nameValue);

}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int caseSize = 100;
int tableHeight = caseSize*tableSize;
int startX = (getWidth() - caseSize) / 2;
int startY = (getHeight() - tableHeight) / 2;

for (int i = 0; i < tableSize; i++) {


g2d.setStroke(new BasicStroke(3 ));
int y = startY + i * caseSize;
g2d.drawRect(startX, y, caseSize, caseSize);

}
System.out.println(added);
drawNode(g2d,caseSize,startX,startY);
}
}

package tn.usousse.eniso.ia1.stage.example.presentation.controller;

import tn.usousse.eniso.ia1.stage.example.presentation.model.Node;
import tn.usousse.eniso.ia1.stage.example.presentation.model.Table;
import tn.usousse.eniso.ia1.stage.example.presentation.view.HashtableDrawComponent;
import tn.usousse.eniso.ia1.stage.example.service.Service;

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.util.Scanner;

public class Presentation {


int size;

public Presentation() {
}

public void console(){


JFrame f = new JFrame();
f.setTitle("SWING");

// creating the header


JMenuBar mb=new JMenuBar();
JMenu menu =new JMenu("file");
JMenu help = new JMenu("help");
JMenuItem about = new JMenuItem("about");
JMenuItem n=new JMenuItem("size ");
JMenuItem addName=new JMenuItem("add");
menu.add(n);
menu.add(addName);
help.add(about);
mb.add(menu);
mb.add(help);
f.setJMenuBar(mb);

// creating the dialogs


n.addActionListener(e -> {
String sizeText = JOptionPane.showInputDialog(f, "Size: ");
if (sizeText != null) {
this.size = Integer.parseInt(sizeText);
HashtableDrawComponent drawComponent = new
HashtableDrawComponent();
drawComponent.setSize(size);
f.add(drawComponent);
f.setVisible(true);

}
});
about.addActionListener(e -> {
JOptionPane.showMessageDialog(f, "Tayssir");

});
addName.addActionListener(e ->{
String name = JOptionPane.showInputDialog(f, "Name");
HashtableDrawComponent drawComponent = new HashtableDrawComponent();
boolean test = drawComponent.isAdded();
if(test){
drawComponent.setName(name);
f.add(drawComponent);
f.setVisible(true);
}

});

f.setSize(700,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

}}

You might also like