Red de conocimiento informático - Problemas con los teléfonos móviles - Programación GUI de clase Swing para programar semáforos

Programación GUI de clase Swing para programar semáforos

¿clase?pública?Prueba?{

estática?JTextField?redT,?amarilloT,?greenT;

estática?JButton?startB,?stopB;

static?LightPanel?lp;

public?static?void?main(String[]?args)?{

show();

handleAction();

}

privado?static?void?handleAction()?{

startB.addActionListener(new?ActionListener()? {

@Override

public?void?actionPerformed(ActionEvent?e)?{

lp.setTimer(Integer.parseInt(redT.getText()) ,

Integer.parseInt(greenT.getText()),

Integer.parseInt(greenT.getText()));

startB.setEnabled(false );

stopB.setEnabled(true);

}

});

stopB.addActionListener(new?ActionListener() ?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

lp.stopLight();

startB.setEnabled(true);

stopB.setEnabled(false);

}

});

}

static?void?show()?{

JFrame?f?=?new?JFrame();

lp?=?new?LightPanel();

f.add(lp);

JPanel?con?=?new?JPanel();

f.add(con,?BorderLayout.EAST) ;

con.setLayout(new?GridLayout(0,?2));

redT?=?new?JTextField("3");

yellowT?=?new?JTextField("1");

greenT?=?new?JTextField("5");

JLabel?l?=?new?JLabel( "¿rojo?:?");

l.setHorizontalAlignment(JLabel.TRAILING);

con.add(l);

con.add(redT );

l?=?new?JLabel("amarillo?:?");

l.setHorizontalAlignment(JLabel.TRAIL

ING);

con.add(l);

con.add(amarilloT);

l?=?new?JLabel("verde?: ?");

l.setHorizontalAlignment(JLabel.TRAILING);

con.add(l);

con.add(greenT); p>

startB?=?new?JButton("Inicio");

stopB?=?new?JButton("Detener");

stopB.setEnabled (falso );

con.add(startB);

con.add(stopB);

f.pack();

f.setDefaultCloseOperation(3);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

clase?LightPanel?extends?JPanel?{

privado?int?size?=?40;

privado?int?w,? h;

privado?Temporizador?t;

privado?int?r,?y,?g;

Color?cr,?cy,?cg ;

int?i?=?0;

public?LightPanel()?{

this.setPreferredSize(new?Dimension(200,?100)

this.setBorder(BorderFactory.createLineBorder(Color.GRAY));

timerHandle();

}

público ?void ?stopLight()?{

i?=?0;

t.stop();

t.setInitialDelay(0);

cr?=?cy?=?cg?=?Color.gray;

repintar();

}

public?void ?setTimer (int?r,?int?y,?int?g)?{

this.r?=?r?*?1000;

this.y?= ?y ?*?1000;

this.g?=?g?*?1000;

cr?=?cy?=?cg?=?Color.gray;

repintar();

t.restart();

}

privado?void?timerHandle()?{

t?=?new?Timer(0,?new?ActionListener()?{

boolean?flag?=?true;

@Override

public?void?actionPerformed(ActionEvent?e)?{

¿cambio?(i?%?3)?{

caso?0:

cr ?=?Color.RED;

cy

?=?cg?=?Color.gray;

repintar();

t.setInitialDelay(r);

t.restart();

ruptura;

caso?1:

cy?=?Color.amarillo;

cr?=?cg?=?Color .gray;

repintar();

t.setInitialDelay(y);

t.restart();

romper;

caso?2:

cg?=?Color.verde;

cy?=?cr?=?Color.gray;

repintar();

t.setInitialDelay(g);

t.restart();

romper;

}

si?(i?==?0)

bandera?=?true;

si?(i?==?2)

bandera?=?false;

System.out.println(i);

si?(bandera)?{

i++;

}?else

i--;

}

});

t.isRepeats() ;

}

@Override

public?void?paintComponent(Graphics?g)?{

w?=?this. getWidth();

h?=?this.getHeight();

super.paintComponent(g);

drawRedLight(g,?cr);

drawYellowLight(g,?cy);

drawGreenLight(g,?cg);

}

privado?void?drawGreenLight (¿Gráficos?g,?Color?c)?{

if?(c?!=?null)

g.setColor(c);

g.fillOval(w?/?4?-?size?/?2,?h?/?2?-?size?/?2,?size,?size);

}

private?void?drawYellowLight(Gráficos?g,?Color?c)?{

if?(c?!=?null)

g.setColor( c);

g.fillOval(w?/?4?*?2?-?tamaño?/?2,?h?/?2?-?tamaño?/?2,?tamaño, ?tamaño);

}

privado?void?drawRedLight(Gráficos?g,?Color?c)?{

si?(c?!= ?null)

g.setColor(c);

g.fillOval(w?/?4?*?3?-?siz

e?/?2,?h?/?2?-?tamaño?/?2,?tamaño,?tamaño);

}

}