- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个用 Java 编写的绘图应用程序,遇到了一些(更多)问题。我有一个图形用户界面和一个工作程序(有点),我唯一的问题是用户绘制的线条和图形没有保存(绘制下一个后消失)。
从过去的一个问题中,我了解到我需要使用 BufferedImage 来存储绘图,然后在我的绘画类中绘制它。
我的问题是,任何人都可以提供有关如何使用缓冲图像存储和绘制绘图的基本解释/示例吗?如果需要,我将如何将绘制线条的颜色和粗细传递到存储的数组中。
那是我的整个类(class)。
package javapaint;
import java.awt.*;
import javax.swing.*;
public class JavaPaintUI extends javax.swing.JFrame {
/** Creates new form JavaPaintUI */
public JavaPaintUI() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jPanel4 = new javax.swing.JPanel();
jRadioButton9 = new javax.swing.JRadioButton();
jRadioButton10 = new javax.swing.JRadioButton();
jSlider2 = new javax.swing.JSlider();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new Panel2();
jPanel3 = new javax.swing.JPanel();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jRadioButton6 = new javax.swing.JRadioButton();
jRadioButton7 = new javax.swing.JRadioButton();
jRadioButton8 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("JavaPaint ~ Nick R");
jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Tool"));
buttonGroup1.add(jRadioButton9);
jRadioButton9.setText("Pen");
jRadioButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton9ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton10);
jRadioButton10.setText("Line");
jRadioButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton10ActionPerformed(evt);
}
});
jSlider2.setMajorTickSpacing(10);
jSlider2.setMaximum(51);
jSlider2.setMinimum(1);
jSlider2.setMinorTickSpacing(5);
jSlider2.setPaintTicks(true);
jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider2StateChanged(evt);
}
});
jLabel1.setText("Stroke Size (Radius)");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton9)
.addComponent(jRadioButton10))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton9)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton10)))
);
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jPanel2MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jPanel2MouseReleased(evt);
}
});
jPanel2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jPanel2MouseDragged(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 596, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 357, Short.MAX_VALUE)
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Color"));
buttonGroup2.add(jRadioButton3);
jRadioButton3.setText("Red");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton4);
jRadioButton4.setText("Black");
jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton5);
jRadioButton5.setText("Gray");
jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton5ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton6);
jRadioButton6.setText("Green");
jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton6ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton7);
jRadioButton7.setText("White");
jRadioButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton7ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton8);
jRadioButton8.setText("Blue");
jRadioButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton8ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jRadioButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
.addComponent(jRadioButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jRadioButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5)
.addComponent(jRadioButton7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton3)
.addComponent(jRadioButton6)
.addComponent(jRadioButton8)))
);
jButton1.setText("Clear");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("About");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(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.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("You cleared the canvas.");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "JavaPaint is a simple java based painting application."
+ " Nick R 5/22/2011", "About", JOptionPane.INFORMATION_MESSAGE);
}
int currentX, currentY, oldX, oldY;
private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
repaint();
if (tool == 1) {
oldX = currentX;
oldY = currentY;
System.out.println("Using pen at " + currentX + ", " + currentY);
}
}
private void jPanel2MousePressed(java.awt.event.MouseEvent evt) {
oldX = evt.getX();
oldY = evt.getY();
if (tool == 2) {
currentX = oldX;
currentY = oldY;
}
}
//Tool Selection//
int tool = 0;
private void jRadioButton9ActionPerformed(java.awt.event.ActionEvent evt) {
tool = 1;
System.out.println("Using the pen tool.");
}
private void jRadioButton10ActionPerformed(java.awt.event.ActionEvent evt) {
tool = 2;
System.out.println("Using the line tool.");
}
//Slider Properties//
double value = 5;
private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {
value = jSlider2.getValue();
System.out.println(value);
}
//COLOR CODE//
int color = 1;
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {
color = 1; // Black
}
private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {
color = 2; // Grey
}
private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {
color = 3; // White
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
color = 4; // Red
}
private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {
color = 5; // Green
}
private void jRadioButton8ActionPerformed(java.awt.event.ActionEvent evt) {
color = 6; // Blue
}
//mouse released//
private void jPanel2MouseReleased(java.awt.event.MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
if (tool == 2) {
System.out.println("line from " + oldX +", "+ oldY + " to " + currentX + ", " + currentY);
}
}
//set ui visible//
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaPaintUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
public javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JRadioButton jRadioButton10;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
private javax.swing.JRadioButton jRadioButton6;
private javax.swing.JRadioButton jRadioButton7;
private javax.swing.JRadioButton jRadioButton8;
private javax.swing.JRadioButton jRadioButton9;
public javax.swing.JSlider jSlider2;
// End of variables declaration
class Panel2 extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
if(color==1)
g2.setPaint( Color.black );
else if(color==2)
g2.setPaint( Color.gray);
else if(color==3)
g2.setPaint( Color.white );
else if(color==4)
g2.setPaint( Color.red );
else if(color==5)
g2.setPaint( Color.green );
else if(color==6)
g2.setPaint( Color.blue );
if (tool == 1) {
g2.fillOval(currentX - ((int) value / 2), currentY - ((int) value / 2), (int) value, (int) value);
} else if (tool == 2) {
g2.setStroke(new BasicStroke((float) value, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g2.drawLine(oldX, oldY, currentX, currentY);
g2.setStroke(new BasicStroke(1.0f));
}
}
}
}
提前致谢!
最佳答案
好的 - 这里有一些..一些变化。
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class JavaPaintUI extends JFrame {
private BufferedImage canvas;
/** Creates new form JavaPaintUI */
public JavaPaintUI() {
initComponents();
}
//this was moved from the overriden paintComponent()
// instead it update the canvas BufferedImage and calls repaint()
public void updateCanvas() {
Graphics2D g2d = canvas.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint( getColor() );
if (tool == 1) {
g2d.fillOval(currentX - ((int) value / 2), currentY - ((int) value / 2), (int) value, (int) value);
} else if (tool == 2) {
g2d.setStroke(new BasicStroke((float) value, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g2d.drawLine(oldX, oldY, currentX, currentY);
g2d.setStroke(new BasicStroke(1.0f));
}
repaint();
}
// used in both the updateCanvas and 'clear' method.
private Color getColor() {
Color c = null;
if(color==1)
c = Color.black;
else if(color==2)
c = Color.gray;
else if(color==3)
c = Color.white;
else if(color==4)
c = Color.red;
else if(color==5)
c = Color.green;
else if(color==6)
c = Color.blue;
return c;
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
canvas = new BufferedImage(600,400,BufferedImage.TYPE_INT_RGB);
buttonGroup1 = new ButtonGroup();
buttonGroup2 = new ButtonGroup();
jPanel4 = new JPanel();
jRadioButton9 = new JRadioButton();
jRadioButton10 = new JRadioButton();
jSlider2 = new JSlider();
jLabel1 = new JLabel();
jPanel2 = new JPanel(new GridBagLayout());
JLabel canvasLabel = new JLabel(new ImageIcon(canvas));
jPanel2.add(canvasLabel, null);
jPanel3 = new JPanel();
jRadioButton3 = new JRadioButton();
jRadioButton4 = new JRadioButton();
jRadioButton5 = new JRadioButton();
jRadioButton6 = new JRadioButton();
jRadioButton7 = new JRadioButton();
jRadioButton8 = new JRadioButton();
jButton1 = new JButton();
jButton2 = new JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("JavaPaint ~ Nick R");
jPanel4.setBorder(BorderFactory.createTitledBorder("Tool"));
buttonGroup1.add(jRadioButton9);
jRadioButton9.setText("Pen");
jRadioButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton9ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton10);
jRadioButton10.setText("Line");
jRadioButton10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton10ActionPerformed(evt);
}
});
jSlider2.setMajorTickSpacing(10);
jSlider2.setMaximum(51);
jSlider2.setMinimum(1);
jSlider2.setMinorTickSpacing(5);
jSlider2.setPaintTicks(true);
jSlider2.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
jSlider2StateChanged(evt);
}
});
jLabel1.setText("Stroke Size (Radius)");
GroupLayout jPanel4Layout = new GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton9)
.addComponent(jRadioButton10))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jSlider2, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(jSlider2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton9)
.addComponent(jLabel1))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton10)))
);
jPanel2.setBackground(new Color(128, 40, 128));
jPanel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
// add the listeners to the label that contains the canvas buffered image
canvasLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
jPanel2MousePressed(evt);
}
public void mouseReleased(MouseEvent evt) {
jPanel2MouseReleased(evt);
}
});
canvasLabel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evt) {
jPanel2MouseDragged(evt);
}
});
// this part of the code was not helping.
// either layout a container or custom paint it.
// only attempt both if you are very confident of what you are doing.
/*
GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 596, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 357, Short.MAX_VALUE)
);
*/
jPanel3.setBorder(BorderFactory.createTitledBorder("Color"));
buttonGroup2.add(jRadioButton3);
jRadioButton3.setText("Red");
jRadioButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton4);
jRadioButton4.setText("Black");
jRadioButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton4ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton5);
jRadioButton5.setText("Gray");
jRadioButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton5ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton6);
jRadioButton6.setText("Green");
jRadioButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton6ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton7);
jRadioButton7.setText("White");
jRadioButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton7ActionPerformed(evt);
}
});
buttonGroup2.add(jRadioButton8);
jRadioButton8.setText("Blue");
jRadioButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jRadioButton8ActionPerformed(evt);
}
});
GroupLayout jPanel3Layout = new GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(jRadioButton3, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton4, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton5, GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
.addComponent(jRadioButton6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(jRadioButton8, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton7, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5)
.addComponent(jRadioButton7))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 3, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton3)
.addComponent(jRadioButton6)
.addComponent(jRadioButton8)))
);
jButton1.setText("Clear");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("About");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jButton2, GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
.addComponent(jButton1, GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
// clear the canvas using the currently selected color.
private void jButton1ActionPerformed(ActionEvent evt) {
System.out.println("You cleared the canvas.");
Graphics g = canvas.getGraphics();
g.setColor( getColor() );
g.fillRect(0,0,canvas.getWidth(),canvas.getHeight());
repaint();
}
private void jButton2ActionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "JavaPaint is a simple java based painting application."
+ " Nick R 5/22/2011", "About", JOptionPane.INFORMATION_MESSAGE);
}
int currentX, currentY, oldX, oldY;
private void jPanel2MouseDragged(MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
updateCanvas();
if (tool == 1) {
oldX = currentX;
oldY = currentY;
System.out.println("Using pen at " + currentX + ", " + currentY);
}
}
private void jPanel2MousePressed(MouseEvent evt) {
oldX = evt.getX();
oldY = evt.getY();
if (tool == 2) {
currentX = oldX;
currentY = oldY;
}
}
//Tool Selection//
int tool = 0;
private void jRadioButton9ActionPerformed(ActionEvent evt) {
tool = 1;
System.out.println("Using the pen tool.");
}
private void jRadioButton10ActionPerformed(ActionEvent evt) {
tool = 2;
System.out.println("Using the line tool.");
}
//Slider Properties//
double value = 5;
private void jSlider2StateChanged(ChangeEvent evt) {
value = jSlider2.getValue();
System.out.println(value);
}
//COLOR CODE//
int color = 1;
private void jRadioButton4ActionPerformed(ActionEvent evt) {
color = 1; // Black
}
private void jRadioButton5ActionPerformed(ActionEvent evt) {
color = 2; // Grey
}
private void jRadioButton7ActionPerformed(ActionEvent evt) {
color = 3; // White
}
private void jRadioButton3ActionPerformed(ActionEvent evt) {
color = 4; // Red
}
private void jRadioButton6ActionPerformed(ActionEvent evt) {
color = 5; // Green
}
private void jRadioButton8ActionPerformed(ActionEvent evt) {
color = 6; // Blue
}
//mouse released//
private void jPanel2MouseReleased(MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
if (tool == 2) {
System.out.println("line from " + oldX +", "+ oldY + " to " + currentX + ", " + currentY);
}
}
//set ui visible//
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaPaintUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private ButtonGroup buttonGroup1;
private ButtonGroup buttonGroup2;
private JButton jButton1;
private JButton jButton2;
private JLabel jLabel1;
public JPanel jPanel2;
private JPanel jPanel3;
private JPanel jPanel4;
private JRadioButton jRadioButton10;
private JRadioButton jRadioButton3;
private JRadioButton jRadioButton4;
private JRadioButton jRadioButton5;
private JRadioButton jRadioButton6;
private JRadioButton jRadioButton7;
private JRadioButton jRadioButton8;
private JRadioButton jRadioButton9;
public JSlider jSlider2;
// End of variables declaration
}
关于java - 在 Swing 中的 BufferedImage 中绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6132988/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!