package three.day.frame;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
public class ImageGraphicsFrame extends Frame {
private Image img;
private Graphics imgGraphics;
private static ImageGraphicsFrame mainFrame;
public ImageGraphicsFrame(String label) {
super(label);
}
public static void main(String[] args) {
mainFrame = new ImageGraphicsFrame("用你鼠标画图吧");
mainFrame.init();
}
private void init() {
mainFrame.setBounds(0, 0, 800, 570);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
mainFrame.setBackground(Color.WHITE);
img = createImage(this.getWidth(), this.getHeight());
imgGraphics = img.getGraphics();
mainFrame.addWindowListener(new WinListener());
addMouseListener(new MouseAdapter(){
private int startX;
private int startY;
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
imgGraphics.setColor(Color.RED);
imgGraphics.setFont(new Font("楷体",Font.ITALIC|Font.BOLD,30));
imgGraphics.drawLine(startX, startY, e.getX(), e.getY());
repaint();
}
});
}
@Override
public void paint(Graphics g){
// imgGraphics.setColor(Color.WHITE);
// imgGraphics.drawRect(0, 0, getWidth(), getHeight());
System.out.println("paint");
g.drawImage(img, 0, 0, this);
}
}
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
public class ImageGraphicsFrame extends Frame {
private Image img;
private Graphics imgGraphics;
private static ImageGraphicsFrame mainFrame;
public ImageGraphicsFrame(String label) {
super(label);
}
public static void main(String[] args) {
mainFrame = new ImageGraphicsFrame("用你鼠标画图吧");
mainFrame.init();
}
private void init() {
mainFrame.setBounds(0, 0, 800, 570);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
mainFrame.setBackground(Color.WHITE);
img = createImage(this.getWidth(), this.getHeight());
imgGraphics = img.getGraphics();
mainFrame.addWindowListener(new WinListener());
addMouseListener(new MouseAdapter(){
private int startX;
private int startY;
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
imgGraphics.setColor(Color.RED);
imgGraphics.setFont(new Font("楷体",Font.ITALIC|Font.BOLD,30));
imgGraphics.drawLine(startX, startY, e.getX(), e.getY());
repaint();
}
});
}
@Override
public void paint(Graphics g){
// imgGraphics.setColor(Color.WHITE);
// imgGraphics.drawRect(0, 0, getWidth(), getHeight());
System.out.println("paint");
g.drawImage(img, 0, 0, this);
}
}