下面这个程序不知道为什么画不出来。求指教,谢谢!
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;/**
* 简单的 Java 铅笔
*/
public class JPencil extends JFrame { // 程序执行入口
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} new JPencil("JPencil").setVisible(true);
} public JPencil(String title) throws HeadlessException {
super(title);
setupWindow();
setupControls();
} private void setupControls() {
this.setLayout(new BorderLayout()); final DrawPanel drawPanel = new DrawPanel();
this.add(drawPanel, BorderLayout.CENTER); final JPencil jPencil = this; // 当窗口打开的时候初始化 drawPanel(否则无法获取 width 和 height)
this.addHierarchyListener(new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) {
boolean isShowingUp = jPencil.isVisible() &&
e.getChangeFlags() == HierarchyEvent.SHOWING_CHANGED; if (isShowingUp) {
drawPanel.init();
}
}
});
} private void setupWindow() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(800, 600);
this.setLocation(50, 50);
} ///////////////////////////////////////// /**
* 绘图板
*/
private class DrawPanel extends JPanel { private BufferedImage image; // 缓存 Image 对象 private Pencil pencil; public void init() {
this.image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
this.pencil = new Pencil(this, this.image);
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, null); // 在这里绘图
}
} ///////////////////////////////////////// /**
* 铅笔工具
*/
private class Pencil extends MouseAdapter { private BufferedImage image; private Component component; private int lastX = -1, lastY = -1; /**
* 构造函数
*
* @param component 可绘图的 Component 对象
* @param image 要绘制的 Image 对象
*/
public Pencil(Component component, BufferedImage image) {
this.image = image;
this.component = component;
this.clear(); component.addMouseListener(this); // MouseListener 负责处理鼠标键按下和松开事件
component.addMouseMotionListener(this); // MouseMotionListener 负责处理鼠标拖拽事件
} private void clear() {
this.image.getGraphics().setColor(Color.white);
this.image.getGraphics().fillRect(0, 0, getWidth(), getHeight());
this.image.getGraphics().setColor(Color.black);
} private void draw(int x, int y) {
this.image.getGraphics().drawLine(lastX, lastY, x, y);
this.component.repaint();
} @Override
public void mouseDragged(MouseEvent e) {
if (this.lastX != -1 && this.lastY != -1) {
draw(e.getX(), e.getY());
} this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mousePressed(MouseEvent e) {
// 设置绘图的起始点
this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mouseReleased(MouseEvent e) {
// 结束绘图
this.lastX = -1;
this.lastY = -1;
}
}
}
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;/**
* 简单的 Java 铅笔
*/
public class JPencil extends JFrame { // 程序执行入口
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} new JPencil("JPencil").setVisible(true);
} public JPencil(String title) throws HeadlessException {
super(title);
setupWindow();
setupControls();
} private void setupControls() {
this.setLayout(new BorderLayout()); final DrawPanel drawPanel = new DrawPanel();
this.add(drawPanel, BorderLayout.CENTER); final JPencil jPencil = this; // 当窗口打开的时候初始化 drawPanel(否则无法获取 width 和 height)
this.addHierarchyListener(new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) {
boolean isShowingUp = jPencil.isVisible() &&
e.getChangeFlags() == HierarchyEvent.SHOWING_CHANGED; if (isShowingUp) {
drawPanel.init();
}
}
});
} private void setupWindow() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(800, 600);
this.setLocation(50, 50);
} ///////////////////////////////////////// /**
* 绘图板
*/
private class DrawPanel extends JPanel { private BufferedImage image; // 缓存 Image 对象 private Pencil pencil; public void init() {
this.image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
this.pencil = new Pencil(this, this.image);
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, null); // 在这里绘图
}
} ///////////////////////////////////////// /**
* 铅笔工具
*/
private class Pencil extends MouseAdapter { private BufferedImage image; private Component component; private int lastX = -1, lastY = -1; /**
* 构造函数
*
* @param component 可绘图的 Component 对象
* @param image 要绘制的 Image 对象
*/
public Pencil(Component component, BufferedImage image) {
this.image = image;
this.component = component;
this.clear(); component.addMouseListener(this); // MouseListener 负责处理鼠标键按下和松开事件
component.addMouseMotionListener(this); // MouseMotionListener 负责处理鼠标拖拽事件
} private void clear() {
this.image.getGraphics().setColor(Color.white);
this.image.getGraphics().fillRect(0, 0, getWidth(), getHeight());
this.image.getGraphics().setColor(Color.black);
} private void draw(int x, int y) {
this.image.getGraphics().drawLine(lastX, lastY, x, y);
this.component.repaint();
} @Override
public void mouseDragged(MouseEvent e) {
if (this.lastX != -1 && this.lastY != -1) {
draw(e.getX(), e.getY());
} this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mousePressed(MouseEvent e) {
// 设置绘图的起始点
this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mouseReleased(MouseEvent e) {
// 结束绘图
this.lastX = -1;
this.lastY = -1;
}
}
}
private void draw(int x, int y) {
Graphics2D graphics2d = this.image.createGraphics();
graphics2d.setColor(Color.black);
graphics2d.drawLine(lastX, lastY, x, y);
this.component.repaint();
}
知道怎么回事了。每次调用 getGraphics() 都是获得一个新的对象。
this.image.getGraphics().drawLine(lastX, lastY, x, y);
this.component.repaint();
}
把你的draw方法改成这样就行了
private void draw(int x, int y) {
this.component.getGraphics().drawLine(lastX, lastY, x, y);
}
因为是要在你的组件上画,所以要从组件中获取graphics
没有保存绘图对象。应该这样改:
(1)在Pencil类中private BufferedImage image;下一行增加一行申明一个内在绘图对象:
private java.awt.Graphics imageGraphics;(2)Pencil构造方法的this.image = image;后增加一行:
imageGraphics = this.image.getGraphics(); // 获取绘图对象(3)Pencil.clear方法内的3条语句改为:
imageGraphics.setColor(Color.white);
imageGraphics.fillRect(0, 0, getWidth(), getHeight());
imageGraphics.setColor(Color.black);(4)Pencil.draw方法内this.image.getGraphics().drawLine(lastX, lastY, x, y);改为:
imageGraphics.drawLine(lastX, lastY, x, y); // 内存绘图,传说中的双缓冲修改后的代码是这样的:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;/**
* 简单的 Java 铅笔
*/
public class JPencil extends JFrame { // 程序执行入口
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} new JPencil("JPencil").setVisible(true);
} public JPencil(String title) throws HeadlessException {
super(title);
setupWindow();
setupControls();
} private void setupControls() {
this.setLayout(new BorderLayout()); final DrawPanel drawPanel = new DrawPanel();
this.add(drawPanel, BorderLayout.CENTER); final JPencil jPencil = this; // 当窗口打开的时候初始化 drawPanel(否则无法获取 width 和 height)
this.addHierarchyListener(new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) {
boolean isShowingUp = jPencil.isVisible() &&
e.getChangeFlags() == HierarchyEvent.SHOWING_CHANGED; if (isShowingUp) {
drawPanel.init();
}
}
});
} private void setupWindow() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(800, 600);
this.setLocation(50, 50);
} ///////////////////////////////////////// /**
* 绘图板
*/
private class DrawPanel extends JPanel { private BufferedImage image; // 缓存 Image 对象 private Pencil pencil; public void init() {
this.image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
this.pencil = new Pencil(this, this.image);
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, null); // 在这里绘图
}
} ///////////////////////////////////////// /**
* 铅笔工具
*/
private class Pencil extends MouseAdapter { private BufferedImage image;
private java.awt.Graphics imageGraphics; private Component component; private int lastX = -1, lastY = -1; /**
* 构造函数
*
* @param component 可绘图的 Component 对象
* @param image 要绘制的 Image 对象
*/
public Pencil(Component component, BufferedImage image) {
this.image = image;
imageGraphics = this.image.getGraphics();
this.component = component;
this.clear(); component.addMouseListener(this); // MouseListener 负责处理鼠标键按下和松开事件
component.addMouseMotionListener(this); // MouseMotionListener 负责处理鼠标拖拽事件
} private void clear() {
imageGraphics.setColor(Color.white);
imageGraphics.fillRect(0, 0, getWidth(), getHeight());
imageGraphics.setColor(Color.black);
} private void draw(int x, int y) {
imageGraphics.drawLine(lastX, lastY, x, y);
this.component.repaint();
} @Override
public void mouseDragged(MouseEvent e) {
if (this.lastX != -1 && this.lastY != -1) {
draw(e.getX(), e.getY());
} this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mousePressed(MouseEvent e) {
// 设置绘图的起始点
this.lastX = e.getX();
this.lastY = e.getY();
} @Override
public void mouseReleased(MouseEvent e) {
// 结束绘图
this.lastX = -1;
this.lastY = -1;
}
}
}