【求助】这个简单的画图程序画不出来

捏造的信仰 2011-11-14 02:32:19
下面这个程序不知道为什么画不出来。求指教,谢谢!


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;
}
}
}
...全文
139 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
edward168855 2011-11-14
  • 打赏
  • 举报
回复
都是高手啊!
lfp001 2011-11-14
  • 打赏
  • 举报
回复

没有保存绘图对象。应该这样改:
(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;
}
}
}
jlu_lamp_lamp 2011-11-14
  • 打赏
  • 举报
回复
private void draw(int x, int y) {
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
捏造的信仰 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ybbps1109 的回复:]
private void draw(int x, int y) {
Graphics2D graphics2d = this.image.createGraphics();
graphics2d.setColor(Color.black);
graphics2d.drawLine(lastX, lastY, x, y);
……
[/Quote]

知道怎么回事了。每次调用 getGraphics() 都是获得一个新的对象。
捏造的信仰 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xiongyu2006 的回复:]

结贴率101.92%,这是怎么搞出来的!
[/Quote]

被删帖删多了就这样。
YBBPS1109 2011-11-14
  • 打赏
  • 举报
回复

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();
}
xiongyu2006 2011-11-14
  • 打赏
  • 举报
回复
结贴率101.92%,这是怎么搞出来的!

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧