1个回答
展开全部
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
class DrawlinePanelClass extends JPanel {
int x1, x2, y1, y2;
Color color = Color.BLACK;
public DrawlinePanelClass() {
this.setBackground(Color.WHITE);
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent event) {
};
@Override
public void mouseDragged(MouseEvent event) {
Graphics2D g = (Graphics2D) getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
x2 = event.getX();
y2 = event.getY();
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
g.dispose();
}
});
addMouseListener(new MouseAdapter() {
Random r = new Random();
@Override
public void mousePressed(MouseEvent event) {
x1 = event.getX();
y1 = event.getY();
color = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
}
});
}
}
public class DrawlineFrameClass extends JFrame {
JPanel panel1;
JButton cButton;
public DrawlineFrameClass() {
super("Drawline绘制工具");
final DrawlinePanelClass panel = new DrawlinePanelClass();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel1 = new JPanel();
cButton = new JButton("清除");
panel1.add(cButton);
cButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
});
getContentPane().add(panel);
getContentPane().add(panel1, "South");
setSize(800, 600);
setVisible(true);
setResizable(false);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new DrawlineFrameClass();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询