package testpaint;import javax.swing.*; import java.awt.*; import java.util.Observable; import java.util.Observer; import java.awt.event.*; import java.awt.event.ActionListener; import javax.swing.event.MouseInputAdapter;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ //应用程序类 public class Sketcher { private SketchModel model; public SketchView view; private static Sketcher the; JFrame awindow = new JFrame("画图窗口"); public static void main(String[] args) { the = new Sketcher(); the.init(); } public void init() { Toolkit kit = awindow.getToolkit(); Dimension size = kit.getScreenSize(); awindow.setBounds(size.width / 2, size.height / 2, 400, 300); awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); model = new SketchModel(); view = new SketchView(this); model.addObserver( (Observer) view); Box botton = Box.createHorizontalBox(); JButton a = new JButton("画立体矩形"); Sketcher.Handle1 lin = the.new Handle1(); a.addActionListener(lin); botton.add(a); awindow.getContentPane().add(botton, BorderLayout.NORTH); awindow.setVisible(true); } class Handle1 implements ActionListener { public void actionPerformed(ActionEvent e) { awindow.getContentPane().add(view,BorderLayout.CENTER ) ; awindow.validate(); } } }自己看看吧 只改了这个文件就可以了
TO jokerjava(冷血): 我按你改的,加了awindow.validate();这句,这是问题依旧,还是 一点击就出现一堆错误: java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:621) at java.awt.Container.add(Container.java:518) at testpaint.Sketcher$Handle1.actionPerformed(Sketcher.java:56) 。等 有点晕。。
//注册监听器
a.addActionListener(new Handle1())
botton.add(a) ;
awindow.getContentPane() .add(botton,BorderLayout.NORTH ) ;
//加上视图
awindow.getContentPane().add(new SketchView(),BorderLayout.center);
一般不要在事件处理函数中增删组件,在初始化时添加。(当然有特殊要求除外)
view没有定义改一下
awindow.getContentPane() .add(new SketchView(),BorderLayout.CENTER ) ;
public class Sketcher {
JFrame awindow = new JFrame("画图窗口");
view=new SketchView(this);//定义view
public static void main(String[] args){
}。
}
如果按几位师兄的写的那样,我要先将SketchView的原先构造器去掉,
即将 public SketchView(Sketcher theapp){
this.theapp=theapp;
}删除才可用new SketchView()。
可是这样写完点击按钮倒是没有错误显示了,可是也没有任何动作,为什么?TO liaomingxue() :
师兄说的,小妹也感同身受,可是你那样写法,是在窗口一打开就已经把图
画好了(这个我已经实现了),我是想在点击按钮时,才进行画图。这应该如何做呢?请各位师兄赐教。谢谢。
package testpaint;
import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//应用程序类
public class Sketcher { private SketchModel model;
public SketchView view;
private static Sketcher the;
JFrame awindow = new JFrame("画图窗口");
public static void main(String[] args){
the=new Sketcher();
the.init() ;
} public void init(){ Toolkit kit=awindow.getToolkit() ;
Dimension size=kit.getScreenSize() ;
awindow.setBounds(size.width/2,size.height/2,400,300) ;
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ) ;
model=new SketchModel();
view=new SketchView(this);
model.addObserver((Observer)view) ;
Box botton=Box.createHorizontalBox() ;
JButton a=new JButton("画立体矩形");
Sketcher n=new Sketcher();
Sketcher.Handle1 lin=n.new Handle1(); a.addActionListener(lin) ; botton.add(a) ;
awindow.getContentPane() .add(botton,BorderLayout.NORTH ) ;
awindow.getContentPane() .add(view,BorderLayout.CENTER ) ; awindow.setVisible(true) ; } class Handle1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
awindow.getContentPane() .add(view,BorderLayout.CENTER ) ;
}
}
}
//模型类(是否一定要声明?)
public class SketchModel extends Observable{}
//视图类
public class SketchView extends JComponent implements Observer{
private Sketcher theapp;
static Graphics2D d;
int width1=60;
int heigh1=80;
int width2=90;
int heigh2=110; public SketchView(Sketcher theapp){
this.theapp=theapp;
}
public void update(Observable o,Object rectangle){ } public void paint(Graphics g){
d=(Graphics2D)g;
d.setPaint(Color.red) ;
//先确定点
Point2D.Double p1=new Point2D.Double(50.0,10.0);
Point2D.Double p2=new Point2D.Double(150.0,100.0);
//根据点画出矩形
Rectangle2D.Double r1=new Rectangle2D.Double(p1.x,p1.y,width1,heigh1);
d.draw(r1) ;
Rectangle2D.Double r2=new Rectangle2D.Double(p2.x,p2.y,width1,heigh2);
d.draw(r2) ;
//最后画两矩形之间的各四点的联线
Line2D.Double line1=new Line2D.Double(p1,p2);
//定位下两个点
p1.setLocation(p1.x+width1,p1.y) ;
p2.setLocation(p2.x+width1,p2.y) ;
Line2D.Double line2=new Line2D.Double(p1,p2); p1.setLocation(p1.x,p1.y+heigh1) ;
p2.setLocation(p2.x,p2.y+heigh2) ;
Line2D.Double line3=new Line2D.Double(p1,p2); p1.setLocation(p1.x-width1,p1.y) ;
p2.setLocation(p2.x-width1,p2.y) ;
Line2D.Double line4=new Line2D.Double(p1,p2);
d.draw(line1);
d.draw(line2);
d.draw(line3);
d.draw(line4); }
}
谢谢了先,希望师兄能指点的详细些,(哪些代码可要,哪些不需要)
小妹初学JAVA,较笨。麻烦你们了。
awindow.getContentPane() .add(view,BorderLayout.CENTER ) ;这句已经注释掉了,
不然,窗口一打开图就画好了。
import java.awt.*;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
//应用程序类
public class Sketcher { private SketchModel model;
public SketchView view;
private static Sketcher the;
JFrame awindow = new JFrame("画图窗口"); public static void main(String[] args) {
the = new Sketcher();
the.init();
} public void init() { Toolkit kit = awindow.getToolkit();
Dimension size = kit.getScreenSize();
awindow.setBounds(size.width / 2, size.height / 2, 400, 300);
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
model = new SketchModel();
view = new SketchView(this);
model.addObserver( (Observer) view);
Box botton = Box.createHorizontalBox();
JButton a = new JButton("画立体矩形"); Sketcher.Handle1 lin = the.new Handle1(); a.addActionListener(lin); botton.add(a);
awindow.getContentPane().add(botton, BorderLayout.NORTH);
awindow.setVisible(true);
} class Handle1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
awindow.getContentPane().add(view,BorderLayout.CENTER ) ;
awindow.validate();
}
}
}自己看看吧 只改了这个文件就可以了
我按你改的,加了awindow.validate();这句,这是问题依旧,还是
一点击就出现一堆错误:
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:621)
at java.awt.Container.add(Container.java:518)
at testpaint.Sketcher$Handle1.actionPerformed(Sketcher.java:56)
。等
有点晕。。
你这个程序问题太多了! 不是你自己写得吧?慢慢学吧,不能着急!
谢谢你的回答,我知道Graphic.drawRect(x1, y1, x2, y2); 这个方法可以画矩形。
我的程序是根据《JAVA 2入门 》里的例子来的,不过里面的例子是在打开窗口时就已经
画好了,我想变动一下,点击按钮再画。
当然把Graphic.drawRect(x1, y1, x2, y2);放进按钮处理事件里是可以完成的。
我只是奇怪那为什么还要生明什么模型类、视图类,做什么啊?
请赐教。谢谢。
呵呵,jokerjava(冷血)的改了3个地方,楼主只改了一个public void init() 里面
Sketcher n=new Sketcher();//去掉
awindow.getContentPane() .add(view,BorderLayout.CENTER ) ;//去掉class Handle1 implements ActionListener 里面
awindow.validate();//加上to:jokerjava(冷血)
你干脆告诉她你改了哪里好了,呵呵
import java.awt.*;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Sketcher extends JFrame
{
private SketchModel model;
public static SketchView view;
private static Sketcher the;
JFrame awindow = new JFrame("画图窗口");
Container cp = awindow.getContentPane();
public static void main(String[] args)
{
the = new Sketcher();
the.init();
} public void init()
{
Toolkit kit = awindow.getToolkit();
Dimension size = kit.getScreenSize();
awindow.setBounds(size.width / 2, size.height / 2, 400, 300);
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
model = new SketchModel();
view = new SketchView(this);
model.addObserver((Observer)view);
Box botton = Box.createHorizontalBox();
JButton a = new JButton("画立体矩形");
Sketcher n = new Sketcher(); a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cp.add(view, BorderLayout.CENTER);
awindow.validate();
}
});
botton.add(a);
cp.add(botton, BorderLayout.NORTH); awindow.setVisible(true); }
}//模型类(是否一定要声明?)
class SketchModel
extends Observable
{
}//视图类
class SketchView
extends JComponent
implements Observer
{
private Sketcher theapp;
static Graphics2D d;
int width1 = 60;
int heigh1 = 80;
int width2 = 90;
int heigh2 = 110; public SketchView(Sketcher theapp)
{
this.theapp = theapp;
} public void update(Observable o, Object rectangle)
{ } public void paint(Graphics g)
{
d = (Graphics2D)g;
d.setPaint(Color.red); //先确定点
Point2D.Double p1 = new Point2D.Double(50.0, 10.0);
Point2D.Double p2 = new Point2D.Double(150.0, 100.0);
//根据点画出矩形
Rectangle2D.Double r1 = new Rectangle2D.Double(p1.x, p1.y, width1,
heigh1);
d.draw(r1);
Rectangle2D.Double r2 = new Rectangle2D.Double(p2.x, p2.y, width1,
heigh2);
d.draw(r2);
//最后画两矩形之间的各四点的联线
Line2D.Double line1 = new Line2D.Double(p1, p2);
//定位下两个点
p1.setLocation(p1.x + width1, p1.y);
p2.setLocation(p2.x + width1, p2.y);
Line2D.Double line2 = new Line2D.Double(p1, p2); p1.setLocation(p1.x, p1.y + heigh1);
p2.setLocation(p2.x, p2.y + heigh2);
Line2D.Double line3 = new Line2D.Double(p1, p2); p1.setLocation(p1.x - width1, p1.y);
p2.setLocation(p2.x - width1, p2.y);
Line2D.Double line4 = new Line2D.Double(p1, p2);
d.draw(line1);
d.draw(line2);
d.draw(line3);
d.draw(line4);
}
}