📄 paint.java
字号:
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Paint" width=1000 height=500>
</applet>
*/
public class Paint extends javax.swing.JApplet
{
Object myColorString;
int lineThickness = 1;
int prevX = 0;
int prevY = 0;
/** Initializes the applet Paint */
public void init()
{
try {
java.awt.EventQueue.invokeAndWait(new Runnable()
{
public void run()
{
initComponents();
colorComboBox.addItem("Black");
colorComboBox.addItem("Red");
colorComboBox.addItem("Orange");
colorComboBox.addItem("Yellow");
colorComboBox.addItem("Green");
colorComboBox.addItem("Blue");
}
});
} catch (Exception ex)
{
ex.printStackTrace();
}
}
private void initComponents()
{
java.awt.GridBagConstraints gridBagConstraints;
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
colorComboBox = new javax.swing.JComboBox();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
drawPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
getContentPane().setLayout(new java.awt.GridBagLayout());
jPanel1.setLayout(null);
jPanel1.setMinimumSize(new java.awt.Dimension(500, 330));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.ipadx = -1;
gridBagConstraints.ipady = -1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jPanel1, gridBagConstraints);
colorComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
colorComboBoxHandler(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = 4;
gridBagConstraints.ipady = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(6, 40, 0, 0);
getContentPane().add(colorComboBox, gridBagConstraints);
buttonGroup1.add(jRadioButton1);
jRadioButton1.setSelected(true);
jRadioButton1.setText("Thin");
jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
thinLineHandler(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 4;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 40, 0, 0);
getContentPane().add(jRadioButton1, gridBagConstraints);
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Medium");
jRadioButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mediumLineHandler(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 5;
gridBagConstraints.gridwidth = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(7, 40, 0, 0);
getContentPane().add(jRadioButton2, gridBagConstraints);
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("Thick");
jRadioButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
thickLineHandler(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 6;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(7, 40, 0, 0);
getContentPane().add(jRadioButton3, gridBagConstraints);
jButton1.setText("Clear");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearHandler(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 7;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(37, 40, 0, 0);
getContentPane().add(jButton1, gridBagConstraints);
jPanel3.setLayout(null);
jPanel3.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 0, 255), 2, true), "Draw Below!", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 0, 11)));
drawPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
myMouseDragPaint(evt);
}
});
drawPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
myMouseReleasedHandler(evt);
}
});
jPanel3.add(drawPanel);
drawPanel.setBounds(10, 18, 230, 270);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 9;
gridBagConstraints.ipadx = 249;
gridBagConstraints.ipady = 299;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jPanel3, gridBagConstraints);
jLabel1.setText("Choose Line Thickness:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = 7;
gridBagConstraints.gridheight = 2;
gridBagConstraints.ipady = 6;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(18, 40, 0, 56);
getContentPane().add(jLabel1, gridBagConstraints);
jLabel2.setText("Choose Line Color:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 6;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(30, 40, 0, 0);
getContentPane().add(jLabel2, gridBagConstraints);
}
// </editor-fold>//GEN-END:initComponents
private void myMouseDragPaint(java.awt.event.MouseEvent evt)
{//GEN-FIRST:event_myMouseDragPaint
Graphics G = drawPanel.getGraphics();
int currX = evt.getX();
int currY = evt.getY();
//Previous coordinates will only be 0,0 if this is a new line.
//If this is the case, set previous coords equal to new coords
//which will begin a new line.
if (prevX == 0) { prevX = currX; }
if (prevY == 0) { prevY = currY; }
//If-else group to set the line color based on the global color value.
if (myColorString == "Red")
{
G.setColor(Color.red);
}
else if (myColorString == "Orange")
{
G.setColor(Color.orange);
}
else if (myColorString == "Yellow")
{
G.setColor(Color.yellow);
}
else if (myColorString == "Green")
{
G.setColor(Color.green);
}
else if (myColorString == "Blue")
{
G.setColor(Color.blue);
}
else
{
G.setColor(Color.black);
}
//Switch statement to set the line thickness based on the global
//line thickness value.
switch (lineThickness)
{
case 1:
G.drawLine(prevX, prevY, currX, currY);
break;
case 2:
G.drawRect(currX, currY, 1, 1);
break;
case 3:
G.drawRect(currX, currY, 2, 2);
break;
default:
G.drawLine(prevX, prevY, currX, currY);
break;
}
//Set previous coords equal to current coords to create a smooth line.
prevX = currX;
prevY = currY;
}//GEN-LAST:event_myMouseDragPaint
private void thickLineHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_thickLineHandler
//Set global line thickness value to 3, meaning a thick line.
lineThickness = 3;
}//GEN-LAST:event_thickLineHandler
private void mediumLineHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mediumLineHandler
//Set global line thickness value to 2, meaning a medium line.
lineThickness = 2;
}//GEN-LAST:event_mediumLineHandler
private void thinLineHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_thinLineHandler
//Set global line thickness value to 1, meaning a thin line.
lineThickness = 1;
}//GEN-LAST:event_thinLineHandler
private void clearHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearHandler
//Clear the drawing panel when the Clear button is pushed.
Graphics G = drawPanel.getGraphics();
G.setColor(drawPanel.getBackground());
G.fillRect(0,0,drawPanel.getBounds().width,drawPanel.getBounds().height);
}//GEN-LAST:event_clearHandler
private void myMouseReleasedHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myMouseReleasedHandler
//Reset coordinates to 0,0 indicating that the line has been broken
prevX = 0;
prevY = 0;
}//GEN-LAST:event_myMouseReleasedHandler
private void colorComboBoxHandler(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_colorComboBoxHandler
//Set new color value globally when color value is changed in
//the combo box.
myColorString = evt.getItem();
}//GEN-LAST:event_colorComboBoxHandler
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JComboBox colorComboBox;
private javax.swing.JPanel drawPanel;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -