Java [NetBeans] Vogliamo realizzare un'applet che ci consenta ...

thrillukrainianΛογισμικό & κατασκευή λογ/κού

7 Ιουν 2012 (πριν από 5 χρόνια και 2 μήνες)

574 εμφανίσεις

Laboratorio di sistemi
Modello RGB JApplet
Java [NetBeans]
Vogliamo realizzare un'applet che ci consenta di provare il modello di colore RGB (Red, Green,

Blue). Nella figura precedente, è visualizzata la pagina web contenente la applet. In NetBeans,

scegliere New > JApplet Form.
color_japplet.pdf
Pag.
1
/
5
Cozzetto ©
Laboratorio di sistemi
Modello RGB JApplet
Java [NetBeans]
All'interno della nostra area di lavoro, disponiamo i seguenti componenti:
Proprietà notevoli degli JSlider
minimum=0
maximun=255
value=0
orientation=HORIZONTAL
NetBeans genera una pagina web di prova nella cartella build del progetto, utilizzando la classe

generata nella cartella classes, ma la grafica come si può notare, è scadente. Conviene usare

Dreamweaver 8, un famoso e potente editor di pagine web, prodotto da un'azienda di nome

Macromedia, ora assorbita da Adobe Inc. Conviene tuttavia copiare il codice della applet

direttamente dalla pagina web generata da NetBeans (da <applet> a </applet>) in quanto

inserendo la applet da Dreamweaver ci possono essere dei problemi.
Ecco il codice HTML della pagina web. La pagina è compatibile con XHTML 1.0.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test ColorJApplet</title>
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style></head>
color_japplet.pdf
Pag.
2
/
5
Cozzetto ©
redJSlider
greenJSlider
blueJSlider
colorJPanel
Laboratorio di sistemi
Modello RGB JApplet
Java [NetBeans]
<body>
<h3>Pagina preparata con Dreamweaver 8</h3>
<hr />
<p>
<APPLET codebase="classes" code="colorjavaapp/ColorJApplet.class" width=300

height=300>
</APPLET>
</p>
<hr />
<p>MaurizioCozzetto.it &copy;</p>
</body>
</html>
Segue codice Java della Japplet.
/*
* ColorJApplet.java
*
* Created on 18 febbraio 2007, 22.19
*/
package colorjavaapp;
import java.awt.Color;
/**
*
* @author Maurizio
*/
public class ColorJApplet extends javax.swing.JApplet {
int red, green, blue;

/** Initializes the applet ColorJApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
setSize(300, 300);
red=0;
green=0;
blue=0;
impostaColorePannello(red, green, blue);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
color_japplet.pdf
Pag.
3
/
5
Cozzetto ©
Laboratorio di sistemi
Modello RGB JApplet
Java [NetBeans]
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
colorJPanel = new javax.swing.JPanel();
redJSlider = new javax.swing.JSlider();
greenJSlider = new javax.swing.JSlider();
blueJSlider = new javax.swing.JSlider();
javax.swing.GroupLayout colorJPanelLayout = new

javax.swing.GroupLayout(colorJPanel);
colorJPanel.setLayout(colorJPanelLayout);
colorJPanelLayout.setHorizontalGroup(
colorJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 368, Short.MAX_VALUE)
);
colorJPanelLayout.setVerticalGroup(
colorJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 251, Short.MAX_VALUE)
);
redJSlider.setMaximum(255);
redJSlider.setValue(0);
redJSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
redJSliderStateChanged(evt);
}
});
greenJSlider.setMaximum(255);
greenJSlider.setValue(0);
greenJSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
greenJSliderStateChanged(evt);
}
});
blueJSlider.setMaximum(255);
blueJSlider.setValue(0);
blueJSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
blueJSliderStateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(colorJPanel, javax.swing.GroupLayout.Alignment.TRAILING,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

Short.MAX_VALUE)
.addComponent(redJSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 368,

color_japplet.pdf
Pag.
4
/
5
Cozzetto ©
Laboratorio di sistemi
Modello RGB JApplet
Java [NetBeans]
Short.MAX_VALUE)
.addComponent(greenJSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 368,

Short.MAX_VALUE)
.addComponent(blueJSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 368,

Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(redJSlider, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(greenJSlider, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(blueJSlider, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(colorJPanel, javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
private void blueJSliderStateChanged(javax.swing.event.ChangeEvent evt) {

blue=blueJSlider.getValue();
impostaColorePannello(red, green, blue);
}
private void greenJSliderStateChanged(javax.swing.event.ChangeEvent evt) {

green=greenJSlider.getValue();
impostaColorePannello(red, green, blue);
}
private void redJSliderStateChanged(javax.swing.event.ChangeEvent evt) {

red=redJSlider.getValue();
impostaColorePannello(red, green, blue);
}

private void impostaColorePannello(int red, int green, int blue) {
Color bg=new Color(red,green,blue);
colorJPanel.setBackground(bg);
}


// Variables declaration - do not modify
private javax.swing.JSlider blueJSlider;
private javax.swing.JPanel colorJPanel;
private javax.swing.JSlider greenJSlider;
private javax.swing.JSlider redJSlider;
// End of variables declaration

}
color_japplet.pdf
Pag.
5
/
5
Cozzetto ©