📄 portviewerpanel.java
字号:
//MAP PANEL
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import parport.ParallelPort;
public class PortViewerPanel extends JPanel{
/*-----------Settings-----------*/
public static final int LPT1=1;
public static final int LPT2=2;
public static final boolean OUT=false;
public static final boolean IN=true;
public static int currentLpt=LPT1;
public static boolean currentInOut=OUT;
private String lptNumber[]={"LPT1 378h","LPT2 278h"};
private String lptState[]={"Data Out","Data In"};
private JComboBox lptNumberChoose=new JComboBox(lptNumber);
private JComboBox lptStateChoose=new JComboBox(lptState);
private JLabel lptNumberChooseLabel=new JLabel("Choose LPT Number");
private JLabel lptStateChooseLabel=new JLabel("Choose Data Port In/Out mode");
/*-------------Leds-------------*/
private Image mapPicture = new ImageIcon(getClass().getResource("/Parallel/ParallelPort.gif")).getImage();
private static int howManyLedsAre=18;
private Led ledsOnPortViewerArray[]=new Led[howManyLedsAre];
private BitField bitFieldsOnPortViewerArray[]=new BitField[howManyLedsAre];
private static boolean bitState[]=new boolean[howManyLedsAre];
private static final int fieldCoordinateX[]={393,552,520,488,458,426,394,361,330,297, 73, 40,104,137,520,169,490,458};
private static final int fieldCoordinateY[]={323,323, 75, 75, 75, 75, 75, 75, 75, 75,323,323,323,323,323,323,323,323};
private static final int ledCoordinateX[]={394,549,516,484,453,421,389,357,325,293,261,229,196,164,531,499,468,436};
private static final int ledCoordinateY[]={311,146,146,146,146,146,146,146,146,146,146,146,146,146,197,197,197,197};
private static final String fieldToolTips[]=
{"Bi-directional Enable","Bit 0 of Controll Port","Bit 0 of Data Port","Bit 1 of Data Port",
"Bit 2 of Data Port","Bit 3 of Data Port","Bit 4 of Data Port",
"Bit 5 of Data Port","Bit 6 of Data Port","Bit 7 of Data Port",
"Bit 6 of Status Port","Bit 7 of Status Port","Bit 5 of Status Port",
"Bit 4 of Status Port","Bit 1 of Controll Port","Bit 3 of Status Port",
"Bit 2 of Controll Port","Bit 3 of Controll Port"};
private static final String ledToolTips[]=fieldToolTips;
/*-----------Parallel-----------*/
private static boolean lptPort[] = new boolean[18];
UpdateParallel updateParallel = new UpdateParallel();
/*------------------------------*/
SyncronizationOfAllElements syncronizationOfAllElements = new SyncronizationOfAllElements();
SyncronizationOfInOut syncronizationOfInOut = new SyncronizationOfInOut();
private static final int updateDelay = 100;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(mapPicture,0,0,this);
}
public PortViewerPanel(){
setLayout(null);
setPreferredSize(new Dimension(mapPicture.getWidth(this),mapPicture.getHeight(this)));
for (int i=0;i<howManyLedsAre;i++){
bitFieldsOnPortViewerArray[i]=new BitField(fieldCoordinateX[i],fieldCoordinateY[i],i,fieldToolTips[i]);
ledsOnPortViewerArray[i] = new Led(ledCoordinateX[i],ledCoordinateY[i],i,ledToolTips[i]);
add(bitFieldsOnPortViewerArray[i]);
add(ledsOnPortViewerArray[i]);
}
lptNumberChooseLabel.setFont(new Font("Arial",Font.BOLD,12));
lptNumberChoose.setFont(new Font("Arial",Font.BOLD,12));
lptStateChooseLabel.setFont(new Font("Arial",Font.BOLD,12));
lptStateChoose.setFont(new Font("Arial",Font.BOLD,12));
int lptSettingCoordinateX=20;
lptNumberChooseLabel.setBounds(lptSettingCoordinateX,
15,
lptNumberChooseLabel.getPreferredSize().width,
lptNumberChooseLabel.getPreferredSize().height);
lptSettingCoordinateX += lptNumberChooseLabel.getPreferredSize().width;
lptSettingCoordinateX += 10;
add(lptNumberChooseLabel);
lptNumberChoose.setBounds(lptSettingCoordinateX,
10,
lptNumberChoose.getPreferredSize().width,
lptNumberChoose.getPreferredSize().height);
lptSettingCoordinateX += lptNumberChoose.getPreferredSize().width;
lptSettingCoordinateX += 30;
add(lptNumberChoose);
lptStateChooseLabel.setBounds(lptSettingCoordinateX,
15,
lptStateChooseLabel.getPreferredSize().width,
lptStateChooseLabel.getPreferredSize().height);
lptSettingCoordinateX += lptStateChooseLabel.getPreferredSize().width;
lptSettingCoordinateX += 10;
add(lptStateChooseLabel);
lptStateChoose.setBounds(lptSettingCoordinateX,
10,
lptStateChoose.getPreferredSize().width,
lptStateChoose.getPreferredSize().height);
add(lptStateChoose);
lptNumberChoose.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
currentLpt=lptNumberChoose.getSelectedIndex()+1;
}
}
);
lptStateChoose.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
currentInOut=(lptStateChoose.getSelectedIndex()==0)?OUT:IN;
}
}
);
updateParallel.start();
syncronizationOfAllElements.start();
syncronizationOfInOut.start();
}
private class UpdateParallel extends Thread{
boolean lptPortTemp[] = new boolean[18];
private boolean lptPortTempIsNotEqualToOutPins(){
boolean Temp[]=null;
if (currentLpt==LPT1) Temp = ParallelPort.getLPT1PortWithNoInverted();
if (currentLpt==LPT2) Temp = ParallelPort.getLPT2PortWithNoInverted();
for (int i=0;i<18;i++){
if(lptPortTemp[i] != Temp[i]) return(true);
}
return(false);
}
private boolean lptPortTempIsNotEqualToLptPort(){
for (int i=0;i<18;i++){
if(lptPortTemp[i] != lptPort[i]) return(true);
}
return(false);
}
private void outPinsHasBeenChanged(){
boolean Temp[]=null;
if (currentLpt==LPT1) Temp = ParallelPort.getLPT1PortWithNoInverted();
if (currentLpt==LPT2) Temp = ParallelPort.getLPT2PortWithNoInverted();
for (int i=0;i<18;i++){
lptPortTemp[i] = lptPort[i] = Temp[i];
}
}
private void lptPortHasBeenChanged(){
boolean Temp[] = new boolean[18];
for (int i=0;i<18;i++){
lptPortTemp[i] = Temp[i] = lptPort[i];
}
if (currentLpt==LPT1) ParallelPort.setLPT1PortWithNoInverted(Temp);
if (currentLpt==LPT2) ParallelPort.setLPT2PortWithNoInverted(Temp);
}
public void run(){
while(true){
if (lptPortTempIsNotEqualToOutPins()) outPinsHasBeenChanged();
if (lptPortTempIsNotEqualToLptPort()) lptPortHasBeenChanged();
try{Thread.sleep(updateDelay);}catch(InterruptedException e){}
}
}
}
private class SyncronizationOfAllElements extends Thread{
boolean allElementsTemp[] = new boolean[18];
private boolean allElementsTempIsNotEqualToLeds(){
for (int i=0;i<18;i++){
if(allElementsTemp[i] != Led.ledsOnPortViewerArrayState[i]) return(true);
}
return(false);
}
private boolean allElementsTempIsNotEqualToFields(){
for (int i=0;i<18;i++){
if(allElementsTemp[i] != BitField.fieldsOnPortViewerArrayValue[i]) return(true);
}
return(false);
}
private boolean allElementsTempIsNotEqualToLptPort(){
for (int i=0;i<18;i++){
if(allElementsTemp[i] != lptPort[i]) return(true);
}
return(false);
}
private void ledsHasBeenChanged(){
for (int i=0;i<18;i++){
allElementsTemp[i] = lptPort[i] = BitField.fieldsOnPortViewerArrayValue[i] = Led.ledsOnPortViewerArrayState[i];
}
}
private void fieldsHasBeenChanged(){
for (int i=0;i<18;i++){
allElementsTemp[i] = lptPort[i] = Led.ledsOnPortViewerArrayState[i] = BitField.fieldsOnPortViewerArrayValue[i];
}
}
private void lptPortHasBeenChanged(){
for (int i=0;i<18;i++){
allElementsTemp[i] = Led.ledsOnPortViewerArrayState[i] = BitField.fieldsOnPortViewerArrayValue[i] = lptPort[i];
}
}
public void run(){
while(true){
if (allElementsTempIsNotEqualToLptPort()) lptPortHasBeenChanged();
if (allElementsTempIsNotEqualToLeds()) ledsHasBeenChanged();
if (allElementsTempIsNotEqualToFields()) fieldsHasBeenChanged();
try{Thread.sleep(updateDelay);}catch(InterruptedException e){}
}
}
}
private class SyncronizationOfInOut extends Thread{
boolean inOutTemp;
private void comboInOutHasBeenChanged(){
inOutTemp = BitField.fieldsOnPortViewerArrayValue[0] = Led.ledsOnPortViewerArrayState[0] = currentInOut;
}
private void ledZeroHasBeenChanged(){
inOutTemp = BitField.fieldsOnPortViewerArrayValue[0] = currentInOut = Led.ledsOnPortViewerArrayState[0];
lptStateChoose.setSelectedIndex((Led.ledsOnPortViewerArrayState[0]==false)?0:1);
}
private void fieldZeroHasBeenChanged(){
inOutTemp = currentInOut = Led.ledsOnPortViewerArrayState[0] = BitField.fieldsOnPortViewerArrayValue[0];
lptStateChoose.setSelectedIndex((BitField.fieldsOnPortViewerArrayValue[0]==false)?0:1);
}
public void run(){
while(true){
if (inOutTemp != currentInOut) comboInOutHasBeenChanged();
if (inOutTemp != Led.ledsOnPortViewerArrayState[0]) ledZeroHasBeenChanged();
if (inOutTemp != BitField.fieldsOnPortViewerArrayValue[0]) fieldZeroHasBeenChanged();
try{Thread.sleep(updateDelay);}catch(InterruptedException e){}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -