1个回答
展开全部
可以了帮你解决了!!!代码如下:
//还有这个package 加上你的文件名!!!别忘记 不然你启动不来main方法
package com;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawLine1 extends Frame {
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public static void main(String[] args){
DrawLine1 mainFrame = new DrawLine1();
}
public void paint(Graphics g){
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
//这个先初始化!!
setSize(800,800);
setTitle("000");
setVisible(true);
Dimension d = getSize();
// oimg=new Image();
oimg = createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new DrawLine1().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 = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
至于第二个问题的话 你问的应该是引用吧?引用外部类?
其实和你的mian方法一样。。。。。直接 DrawLine1 mainFrame = new DrawLine1();
然后调用就可以
//还有这个package 加上你的文件名!!!别忘记 不然你启动不来main方法
package com;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
public class DrawLine1 extends Frame {
int orgX;
int orgY;
int endX;
int endY;
Image oimg = null;
Graphics og = null;
Vector vLines = new Vector();
public static void main(String[] args){
DrawLine1 mainFrame = new DrawLine1();
}
public void paint(Graphics g){
g.drawImage(oimg,0,0,this);
}
public DrawLine1() {
// Dimension d = getSize();
//这个先初始化!!
setSize(800,800);
setTitle("000");
setVisible(true);
Dimension d = getSize();
// oimg=new Image();
oimg = createImage(d.width,d.height);
og = oimg.getGraphics();
// Add window listener.
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new DrawLine1().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 = getGraphics();
g.setColor(Color.RED);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
g.drawString(orgX + "," + orgY,orgX,orgY);
g.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
g.drawLine(orgX,orgY,e.getX(),e.getY());
og.setColor(Color.RED);
og.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() +","+e.getY(),e.getX(),e.getY());
og.drawLine(orgX,orgY,e.getX(),e.getY());
}
});
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
至于第二个问题的话 你问的应该是引用吧?引用外部类?
其实和你的mian方法一样。。。。。直接 DrawLine1 mainFrame = new DrawLine1();
然后调用就可以
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询