62,616
社区成员
发帖
与我相关
我的任务
分享
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Awt_Ui extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage im = null;
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private JFrame jf = new JFrame("AWT练习!");
private JLabel jl = new JLabel();
private JLabel jl2 = new JLabel();
private JTextField jt = new JTextField();
private JButton jb = new JButton("发送");
private int mX = 0;
private int mY = 0;
private int newX = 0;
private int newY = 0;
private Stack<BufferedImage> list=new Stack<BufferedImage>();
private DBoard db;
public void init() {
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(WIDTH, HEIGHT);
jf.setVisible(true);
jf.setLocation(220, 90);
jf.setResizable(false);
jf.setLayout(new BorderLayout());
try {
im = ImageIO.read(new File("image/2.jpg"));
list.add(im);
} catch (IOException e) {
e.printStackTrace();
}
jf.add(jl, BorderLayout.NORTH);
jl2.add(jt);
jl2.add(jb);
jf.setLayout(new BorderLayout());
jf.add(jl2, BorderLayout.SOUTH);
jf.addMouseListener(new MyMouseListener());
jf.addMouseMotionListener(new MyMouseMotion());
db = new DBoard();
db.setPreferredSize(new Dimension(WIDTH, HEIGHT));
jf.add(db);
jf.pack();
}
public static void main(String[] args) {
Awt_Ui t = new Awt_Ui();
t.init();
}
private class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
BufferedImage bfim=null;
try {
bfim = ImageIO.read(new File("image/2.jpg"));
list.push(bfim);
} catch (IOException e) {
e.printStackTrace();
}
newX = event.getX();
newY = event.getY();
mX = newX;
mY = newY;
db.repaint();
}
}
private class DBoard extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Color c = g.getColor();
g.drawImage(list.pop(), mX, mY, null);
g.setColor(c);
}
}
private class MyMouseMotion extends MouseMotionAdapter {
public void mouseMoved(MouseEvent e) {
}
}
}
package test.commom;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Awt_Ui extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage im = null;
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private JFrame jf = new JFrame("AWT练习!");
private JLabel jl = new JLabel();
private JLabel jl2 = new JLabel();
private JTextField jt = new JTextField();
private JButton jb = new JButton("发送");
private int mX = 0;
private int mY = 0;
private int newX = 0;
private int newY = 0;
public void init() {
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(WIDTH, HEIGHT);
jf.setVisible(true);
jf.setLocation(220, 90);
jf.setResizable(false);
jf.setLayout(new BorderLayout());
try {
im = ImageIO.read(new File("image/2.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
jf.add(jl,BorderLayout.NORTH);
jl2.add(jt);
jl2.add(jb);
jf.setLayout(new BorderLayout());
jf.add(jl2,BorderLayout.SOUTH);
jf.addMouseListener(new MyMouseListener());
jf.addMouseMotionListener(new MyMouseMotion());
DBoard board = new DBoard();
board.setPreferredSize(new Dimension(WIDTH, HEIGHT));
jf.add(board);
jf.pack();
}
public static void main(String[] args) {
Awt_Ui t = new Awt_Ui();
t.init();
}
private class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
newX = event.getX();
newY = event.getY();
mX = newX;
mY = newY;
jf.repaint();
}
}
private class DBoard extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Color c = g.getColor();
g.drawImage(im, mX, mY, null);
g.setColor(c);
}
}
private class MyMouseMotion extends MouseMotionAdapter {
public void mouseMoved(MouseEvent e) {
}
}
}