java绘图
例1:将JPanel作为画布放在界面上
可视化控件(包括JPanel)都有一个从父类继承过来的函数:paint,该函数在界面更新时自动调用,可以绘图。
用repaint函数可以手工调用它。
public void paint(Graphics g){}
Graphics充当“画笔”功能
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel{
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(50));
g2d.drawLine(100,100,200,200);
g2d.setColor(Color.red);
g2d.drawOval(100,100,200,200);
g2d.setColor(Color.blue);
g2d.fillRect(200,200,100,100);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例2:鼠标拖动,界面上能够画图
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends JPanel implements MouseMotionListener{//����
int X=-100,Y=-100;
MyCanvas(){
this.addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
X = e.getX(); Y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLUE);
g2d.fillRect(X,Y,20,20);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
问题:当鼠标移动太快的时候,画的不连续
例3:界面上随机位置不断出现随机颜色的椭圆
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel implements Runnable{
MyCanvas(){
new Thread(this).start();
}
public void run(){
while(true){
repaint();
try{Thread.sleep(10);} catch(Exception e){}
}
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(new Color((int)(256*Math.random()),
(int)(256*Math.random()),
(int)(256*Math.random())));
g2d.drawOval((int)(600*Math.random()),
(int)(500*Math.random()),
(int)(100*Math.random()),
(int)(100*Math.random()));
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例4:将dog.jpg绘制在画布上
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel {
private Image img = Toolkit.getDefaultToolkit().createImage("dog.jpg");
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g.drawImage(img, 100,100, this);
g.drawImage(img, 200,200,50,60, this);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例5:将dog.jpg左边一半,填充界面左边一半;右边一半,填充界面的任意指定位置
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel {
private Image img = Toolkit.getDefaultToolkit().createImage("dog.jpg");
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g.drawImage(img, 0, 0, this.getWidth()/2, this.getHeight(),
0, 0, img.getWidth(this)/2, img.getHeight(this),
this);
g.drawImage(img, 300,300,400,400,
img.getWidth(this)/2, 0, img.getWidth(this), img.getHeight(this),
this);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例6鼠标拖动移动图片位置
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends JPanel implements MouseMotionListener{
int X=100,Y=100;
private Image img = Toolkit.getDefaultToolkit().createImage("dog.jpg");
MyCanvas(){ this.addMouseMotionListener(this); }
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
g2d.drawImage(img, X, Y, this);
}
public void mouseDragged(MouseEvent e) {
X = e.getX(); Y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例7旋转图片
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel {
private Image img = Toolkit.getDefaultToolkit().createImage("dog.jpg");
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.PI/4, 100,100);
g.drawImage(img, 100,100, this);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}
例8 不停的旋转图片
package javaPaint5_10;
import javax.swing.*;
import java.awt.*;
class MyCanvas extends JPanel implements Runnable{
private Image img = Toolkit.getDefaultToolkit().createImage("dog.jpg");
double t = 0;
MyCanvas(){ new Thread(this).start(); }
public void run(){
while(true){
t=t + 0.1;
repaint();
try{Thread.sleep(50);} catch(Exception e){}
}
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.yellow);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
g2d.rotate(t, 200,200);
g.drawImage(img, 200,200, this);
}
}
class MyFrame extends JFrame{
private MyCanvas mc = new MyCanvas();
public MyFrame(){
this.add(mc);
this.setSize(600,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyFrame().setVisible(true);
}
}