以下是我的代码,请各位帮忙调试!谢谢啦!我的想用CANVAS画一条直线!接受输入数据,然后画线!
import java.awt.*;
import java.awt.event.*; public class PaintLine { public static void main(String [] args)
{
int x1=0,y1=0,x2=0,y2=0;
CanvasD cd=new CanvasD(x1,y1, x2, y2);
new FrameInOut(cd );
}
} class FrameInOut extends Frame implements MouseListener
{
Label prompt1,prompt2;
TextField t1,t2;
Button btn1;
public FrameInOut(CanvasD cd)
{
super("画直线");
prompt1=new Label("请输入x2:");
prompt2=new Label("y2:");
t1=new TextField(10);
t2=new TextField(10);
btn1=new Button("画线");
int x1=0,y1=0,x2=100,y2=100;
CanvasD cd1=new CanvasD(x1,y1, x2, y2); setLayout(new FlowLayout());
add(prompt1);
add(t1);
add(prompt2);
add(t2);
add(btn1);
add(cd);
btn1.addMouseListener(this);
addWindowListener(new HandleWin());
setSize(500,500);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==btn1)
{
x2=Integer.parseInt(t1.getText());
y2=Integer.parseInt(t2.getText());
}
} public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class CanvasD extends Canvas
{
static int a1,b1,a2,b2;
static
{
a1=0;
a2=0;
b1=0;
b2=0;
}
static int x1,y1,x2,y2;
CanvasD(int a1,int b1,int a2,int b2)
{
x1=a1;
y1=b1;
x2=a2;
y2=b2;
setBackground(Color.gray);
setSize(300,300); } public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
repaint();
} setSize(300,300);
} } }
class HandleWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
(e.getWindow()).dispose();
System.exit(0);
}
}
import java.awt.*;
import java.awt.event.*; public class PaintLine { public static void main(String [] args)
{
int x1=0,y1=0,x2=0,y2=0;
CanvasD cd=new CanvasD(x1,y1, x2, y2);
new FrameInOut(cd );
}
} class FrameInOut extends Frame implements MouseListener
{
Label prompt1,prompt2;
TextField t1,t2;
Button btn1;
public FrameInOut(CanvasD cd)
{
super("画直线");
prompt1=new Label("请输入x2:");
prompt2=new Label("y2:");
t1=new TextField(10);
t2=new TextField(10);
btn1=new Button("画线");
int x1=0,y1=0,x2=100,y2=100;
CanvasD cd1=new CanvasD(x1,y1, x2, y2); setLayout(new FlowLayout());
add(prompt1);
add(t1);
add(prompt2);
add(t2);
add(btn1);
add(cd);
btn1.addMouseListener(this);
addWindowListener(new HandleWin());
setSize(500,500);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==btn1)
{
x2=Integer.parseInt(t1.getText());
y2=Integer.parseInt(t2.getText());
}
} public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class CanvasD extends Canvas
{
static int a1,b1,a2,b2;
static
{
a1=0;
a2=0;
b1=0;
b2=0;
}
static int x1,y1,x2,y2;
CanvasD(int a1,int b1,int a2,int b2)
{
x1=a1;
y1=b1;
x2=a2;
y2=b2;
setBackground(Color.gray);
setSize(300,300); } public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
repaint();
} setSize(300,300);
} } }
class HandleWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
(e.getWindow()).dispose();
System.exit(0);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;public class PaintLine { public static void main(String[] args) {
new FrameInOut();
}
}class FrameInOut extends Frame/* implements MouseListener ,ActionListener*/ { Label prompt1, prompt2;
TextField t1, t2;
Button btn1;
int x1 = 0, y1 = 0, x2 = 100, y2 = 100;
CanvasD cd1; public FrameInOut() {
super("画直线");
prompt1 = new Label("请输入x2:");
prompt2 = new Label("y2:");
t1 = new TextField(10);
t2 = new TextField(10);
btn1 = new Button("画线");
cd1 = new CanvasD(x1, y1, x2, y2); setLayout(new FlowLayout());
add(prompt1);
add(t1);
add(prompt2);
add(t2);
add(btn1);
add(cd1);
t1.setText("" + x2);
t2.setText("" + y2);
//btn1.addMouseListener(this);
btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
click();
}
});
addWindowListener(new HandleWin());
setSize(500, 500);
setVisible(true);
} public void click() {
x2 = Integer.parseInt(t1.getText());
y2 = Integer.parseInt(t2.getText());
this.cd1.repaint();
cd1.set(x2, y2);
cd1.repaint(0, 0, cd1.getWidth(), cd1.getHeight());
System.out.println("click");
}
}class CanvasD extends Canvas { int x1, y1, x2, y2; CanvasD(int a1, int b1, int a2, int b2) {
setBackground(Color.gray);
setSize(300, 300);
set(a1,b1,a2,b2);
} public void set(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
repaint();
}
public void set(int a1, int b1) {
x2 = a1;
y2 = b1;
repaint();
} public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.yellow);
g.drawLine(x1, y1, x2, y2);
g.setColor(c);
}
}class HandleWin extends WindowAdapter { public void windowClosing(WindowEvent e) {
(e.getWindow()).dispose();
System.exit(0);
}
}