在桌面上绘图(小程序)ITeye - 众发娱乐

在桌面上绘图(小程序)ITeye

2019年02月25日10时07分33秒 | 作者: 尔曼 | 标签: 程序,运转,色彩 | 浏览: 1428

主张先将此程序运转,然后打包成java文件包,这样就能够直接在电脑屏幕上点击运转,就相当于在电脑屏幕上画画了,这个程序只能画出各种色彩的实心圆与空心圆。




package Fengxing;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test2 extends JFrame {
JButton jb;
Image image;
Graphics g;
int i = 0, o = 0, c = 0;
static int j = 0;
int x;
int y;
// serialVersionUID作用是序列化时坚持版别的兼容性,即在版别晋级时反序列化仍坚持目标的唯一性。
private static final long serialVersionUID = 1L;
static BackPane pane;

public Test2() {
setDefaultCloseOperation(3);
setUndecorated(true);
setSize(1366, 750);
setLocationRelativeTo(null);
// 截图函数
try {
Robot robot = new Robot();
// 对在窗体所在位置截屏
image = robot.createScreenCapture(getBounds());
} catch (AWTException e) {
e.printStackTrace();
}
pane = new BackPane();
jb = new JButton("Start");
pane.add(jb);
setVisible(true);
g = this.getGraphics();
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("Start")) {
i = 1;
o = 0;
c = 0;
}
}
});
this.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseDragged(MouseEvent e) {
  x = e.getX();
  y = e.getY();
if (1 i) { // 得到鼠标当时的坐标

if (1 c) {
Random rand = new Random();
// 每次调用时都能够随机选取一种色彩
g.setColor(new Color(rand.nextInt(256), rand
.nextInt(256), rand.nextInt(256)));
} else {
g.setColor(Color.BLACK);
}
if (1 o) {
g.drawOval(x, y, 40, 40);
} else {
// 画一个实心的圆圈
g.fillOval(x, y, 40, 60);
}
}
}

@Override
public void mouseMoved(MouseEvent e) {

}
});
JButton jb1 = new JButton("Clean");
jb1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// 清空屏幕
pane.getGraphics().clearRect(0, 0, 1500, 1500);
// 清空后再重画一个面板
new BackPane().paintComponent(g);
// 另i为0,再一次点击开始时才能够持续画
i = 0;
}
});
pane.add(jb1);
JButton jb2 = new JButton("Exit");
jb2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("Exit")) {
int i = JOptionPane.showConfirmDialog(null, "确定要退出该程序?");
if (i 0) {
System.exit(0);
}
}
}
});
pane.add(jb2);
JButton jb3 = new JButton("Oval");
jb3.addActionListener(new ActionListener() {



@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("Oval")) {
g.drawOval(x, y, 40, 40);
o = 1;
c = 0;
}
}
});
pane.add(jb3);
JButton jb4 = new JButton("Colorful");
jb4.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("Colorful")) {
c = 1;

}
}
});
pane.add(jb4);
setContentPane(pane);
}

class BackPane extends JPanel {
private static final long serialVersionUID = 1L;

@Override
// 界说一个在截取的屏幕上绘画的函数
protected void paintComponent(Graphics g) {
// 调用父类的办法
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new Test2();
}
});
}

}
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表众发娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章