import java.awt.*;
import java.awt.event.*;/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/09/07
*/
public class DrawLine1Frame extends Frame {
/**
* The constructor.
*/
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
public void paint(Graphics g)
{
g.drawImage(oimg, 0, 0, this);
}
public DrawLine1Frame() {
setVisible(true);
Dimension d = getSize();
oimg = createImage(d.width, d.height);
og = oimg.getGraphics();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawLine1Frame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("DrawLine1");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
DrawLine1Frame.this.windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = DrawLine1Frame.this.getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 30));
g.drawString("(" + orgX + ", " + orgY + ")", orgX, orgY);
g.drawString("(" + endX + ", " + endY + ")", endX, endY);
g.drawLine(orgX, orgY, endX, endY);
og.setColor(Color.RED);
og.setFont(new Font(null, Font.ITALIC|Font.BOLD, 30));
og.drawString("(" + orgX + ", " + orgY + ")", orgX, orgY);
og.drawString("(" + endX + ", " + endY + ")", endX, endY);
og.drawLine(orgX, orgY, endX, endY);
}
});
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}下面为启动运行类
/**
* AWT Sample application
*
* @author
* @version 1.00 05/09/07
*/
public class DrawLine1 {
public static void main(String[] args) {
// Create application frame.
DrawLine1Frame frame = new DrawLine1Frame();
// Show frame
}
}
为什么不能重绘呢?
import java.awt.event.*;/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/09/07
*/
public class DrawLine1Frame extends Frame {
/**
* The constructor.
*/
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
public void paint(Graphics g)
{
g.drawImage(oimg, 0, 0, this);
}
public DrawLine1Frame() {
setVisible(true);
Dimension d = getSize();
oimg = createImage(d.width, d.height);
og = oimg.getGraphics();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawLine1Frame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("DrawLine1");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
DrawLine1Frame.this.windowClosed();
}
}
);
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics g = DrawLine1Frame.this.getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 30));
g.drawString("(" + orgX + ", " + orgY + ")", orgX, orgY);
g.drawString("(" + endX + ", " + endY + ")", endX, endY);
g.drawLine(orgX, orgY, endX, endY);
og.setColor(Color.RED);
og.setFont(new Font(null, Font.ITALIC|Font.BOLD, 30));
og.drawString("(" + orgX + ", " + orgY + ")", orgX, orgY);
og.drawString("(" + endX + ", " + endY + ")", endX, endY);
og.drawLine(orgX, orgY, endX, endY);
}
});
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}下面为启动运行类
/**
* AWT Sample application
*
* @author
* @version 1.00 05/09/07
*/
public class DrawLine1 {
public static void main(String[] args) {
// Create application frame.
DrawLine1Frame frame = new DrawLine1Frame();
// Show frame
}
}
为什么不能重绘呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货