62,612
社区成员
发帖
与我相关
我的任务
分享
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class cesh extends JFrame {
Graphics2D g2d;
public cesh(){
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
huatu();
}
public void huatu(){
this.g2d = (Graphics2D) this.getGraphics();
g2d.setColor(Color.GREEN);
g2d.drawLine(0,0,200,200);
g2d.fillOval(110, 340, 100, 100);
}
public static void main(String[] args){
new cesh();
}
}
import java.awt.*;
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(600, 600);
setVisible(true);
setResizable(false);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new DrawlineFrameClass();
}
}