一个画直线的JAVA小程序

我编写了一个JAVA小程序,是用来画直线的,可结果什么也画不出来,不知道哪里出了问题,哪位高手帮我看一下,谢谢了!这是源代码:importjava.awt.*;impor... 我编写了一个JAVA小程序,是用来画直线的,可结果什么也画不出来,不知道哪里出了问题,哪位高手帮我看一下,谢谢了!

这是源代码:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DrawLine {

public static void main(String[] args) {

new MyFrame(200,200,300,300);
}

}

class MyFrame extends Frame{

Point p=new Point();
Point q=new Point();
boolean flag;
ArrayList<Point> a1=new ArrayList<Point>();
ArrayList<Point> a2=new ArrayList<Point>();

MyFrame(int x,int y,int w,int h){
super("drawline");
setBounds(x,y,w,h);
setBackground(Color.white);
Button b=new Button("Line");
add(b,BorderLayout.NORTH);
b.addActionListener(new ButtonMonitor());
addMouseListener(new MouseMonitor());
addWindowListener(new WindowMonitor());
setVisible(true);
}

class ButtonMonitor implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="Line")
flag=true;
repaint();
}

}

public void paint(Graphics g){

Iterator<Point> i1=a1.iterator();
Iterator<Point> i2=a2.iterator();

while(i1.hasNext()&&i2.hasNext()){
p=i1.next();
q=i1.next();
Color c=g.getColor();
g.setColor(Color.red);
g.drawLine(p.x,p.y,q.x,q.y);
g.setColor(c);
}

}

public void addPoint1(Point p){
a1.add(p);
}

public void addPoint2(Point p){
a2.add(p);
}

class MouseMonitor extends MouseAdapter{

public void mousePressed(MouseEvent e) {

MyFrame my=(MyFrame)e.getSource();
addPoint1(new Point(my.getX(),my.getY()));
repaint();
}

public void mouseDragged(MouseEvent e) {

MyFrame my=(MyFrame)e.getSource();
addPoint2(new Point(my.getX(),my.getY()));
repaint();
}

}

class WindowMonitor extends WindowAdapter{

@Override
public void windowClosing(WindowEvent e) {

setVisible(false);
System.exit(0);
}

}
}
展开
 我来答
cooll87
2009-03-28 · TA获得超过105个赞
知道答主
回答量:243
采纳率:0%
帮助的人:229万
展开全部
这是我以前写的一个可以画多颜色多图形的东西,给你看看..

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.*;

public class DrawBorder extends JFrame implements MouseListener,
MouseMotionListener {

// private JComboBox;
private JLabel label;

private JLabel labelColor;

private JLabel labelShape;

private String[] colors = { "Black", "Blue", "Green", "Cyan", "Gray",
"Orange", "Red" };

private String[] shapes = { "Line", "Rectangle", "Oval", "TDRectangle" };

private Map mpColor;

private JComboBox comboboxColor;

private JComboBox comboboxShape;

ArrayList lstShape;

protected int x1;

protected int y1;

protected int x2;

protected int y2;

protected String color;

private String shape;

public void display() {
shape = "Line";
comboboxShape = new JComboBox(shapes);
comboboxShape.setMaximumRowCount(shapes.length);
comboboxShape.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
// determine whether check box selected
if (event.getStateChange() == ItemEvent.SELECTED) {
shape = shapes[comboboxShape.getSelectedIndex()];
}
}
});
color = "BLACK";
comboboxColor = new JComboBox(colors);
comboboxColor.setMaximumRowCount(colors.length);
comboboxColor.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
color = colors[comboboxColor.getSelectedIndex()];
}
}
});

label = new JLabel();
labelShape = new JLabel("图形:");
labelColor = new JLabel("颜色:");
label.setText(shape);
// getContentPane().add(label);

lstShape = new ArrayList();

Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(labelShape);
container.add(comboboxShape);
container.add(labelColor);
container.add(comboboxColor);
container.add(label);

getContentPane().setBackground(Color.WHITE);

addMouseListener(this);
addMouseMotionListener(this);
setSize(600, 600);
setVisible(true);
}

public void mouseClicked(MouseEvent e) {
label.setText("单击鼠标在: [" + e.getX() + ", " + e.getY() + "]");
}

public void mouseEntered(MouseEvent e) {
label.setText("鼠标在: [" + e.getX() + ", " + e.getY() + "]");

}

public void mouseExited(MouseEvent e) {
label.setText("鼠标在: [窗口外]");
}

public void mousePressed(MouseEvent e) {
label.setText("按住鼠标在: [" + e.getX() + ", " + e.getY() + "]");
x1 = e.getX();
y1 = e.getY();
}

public void mouseReleased(MouseEvent e) {
label.setText("松开鼠标在: [" + e.getX() + ", " + e.getY() + "]");
// 判断是否按住鼠标并拖动过,如果不则不产生图形
if (((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0)) == false) {
if (this.shape.equals("Line")) {
lstShape.add(new Line(x1, y1, x2, y2, color));
}
else if (this.shape.equals("Rectangle")) {
if (x2 > x1 && y2 > y1) {
lstShape.add(new Rectangle(x1, y1, x2 - x1, y2 - y1, color));
}
else if (x2 < x1 && y2 > y1) {
lstShape.add(new Rectangle(x2, y1, x1 - x2, y2 - y1, color));
}
else if (x2 > x1 && y2 < y1) {
lstShape.add(new Rectangle(x1, y2, x2 - x1, y1 - y2, color));
}
else if (x2 < x1 && y2 < y1) {
lstShape.add(new Rectangle(x2, y2, x1 - x2, y1 - y2, color));
}
}
else if (this.shape.equals("Oval")) {
if (x2 > x1 && y2 > y1) {
lstShape.add(new Oval(x1, y1, x2 - x1, y2 - y1, color));
}
else if (x2 < x1 && y2 > y1) {
lstShape.add(new Oval(x2, y1, x1 - x2, y2 - y1, color));
}
else if (x2 < x1 && y2 < y1) {
lstShape.add(new Oval(x2, y2, x1 - x2, y1 - y2, color));
}
else if (x2 > x1 && y2 < y1) {
lstShape.add(new Oval(x1, y2, x2 - x1, y1 - y2, color));
}
}
else if (this.shape.equals("TDRectangle")) {
if (x2 > x1 && y2 > y1) {
lstShape.add(new TDRectangle(x1, y1, x2 - x1, y2 - y1, color));
}
else if (x2 < x1 && y2 > y1) {
lstShape.add(new TDRectangle(x2, y1, x1 - x2, y2 - y1, color));
}
else if (x2 > x1 && y2 < y1) {
lstShape.add(new TDRectangle(x1, y2, x2 - x1, y1 - y2, color));
}
else if (x2 < x1 && y2 < y1) {
lstShape.add(new TDRectangle(x2, y2, x1 - x2, y1 - y2, color));
}
}
}
// 初始化图形坐标
x1 = y1 = x2 = y2 = 0;
repaint();
}

public void mouseDragged(MouseEvent e) {
label.setText("拖动鼠标在: [" + e.getX() + ", " + e.getY() + "]");
x2 = e.getX();
y2 = e.getY();
label.setText("图形:" + shape);
repaint();
}

public void mouseMoved(MouseEvent e) {
label.setText("移动动鼠标在: [" + e.getX() + ", " + e.getY() + "]");
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(this.toColor());
if (this.shape.equals("Line")) {
g.drawLine(x1, y1, x2, y2);
}
else if (this.shape.equals("Rectangle")) {
if (x2 > x1 && y2 > y1) {
g.drawRect(x1, y1, x2 - x1, y2 - y1);
}
else if (x2 < x1 && y2 > y1) {
g.drawRect(x2, y1, x1 - x2, y2 - y1);
}
else if (x2 < x1 && y2 < y1) {
g.drawRect(x2, y2, x1 - x2, y1 - y2);
}
else if (x2 > x1 && y2 < y1) {
g.drawRect(x1, y2, x2 - x1, y1 - y2);
}
}
else if (this.shape.equals("Oval")) {
if (x2 > x1 && y2 > y1) {
g.drawOval(x1, y1, x2 - x1, y2 - y1);
}
else if (x2 < x1 && y2 > y1) {
g.drawOval(x2, y1, x1 - x2, y2 - y1);
}
else if (x2 < x1 && y2 < y1) {
g.drawOval(x2, y2, x1 - x2, y1 - y2);
}
else if (x2 > x1 && y2 < y1) {
g.drawOval(x1, y2, x2 - x1, y1 - y2);
}
}
else if (this.shape.equals("TDRectangle")) {
if (x2 > x1 && y2 > y1) {
g.draw3DRect(x1, y1, x2 - x1, y2 - y1, true);
}
else if (x2 < x1 && y2 > y1) {
g.draw3DRect(x2, y1, x1 - x2, y2 - y1, true);
}
else if (x2 < x1 && y2 < y1) {
g.draw3DRect(x2, y2, x1 - x2, y1 - y2, true);
}
else if (x2 > x1 && y2 < y1) {
g.draw3DRect(x1, y2, x2 - x1, y1 - y2, true);
}
}

// 画出存放在list中的图形
for (Object object : lstShape) {
if (object.getClass().toString().equals("class DrawBorder$Line")) {
Line line = (Line) object;
g.setColor(line.toColor());
g.drawLine(line.x1, line.y1, line.x2, line.y2);
}
else if (object.getClass().toString()
.equals("class DrawBorder$Rectangle")) {
Rectangle rectangle = (Rectangle) object;
g.setColor(rectangle.toColor());
g.drawRect(rectangle.x1, rectangle.y1, rectangle.x2, rectangle.y2);
}
else if (object.getClass().toString().equals("class DrawBorder$Oval")) {
Oval oval = (Oval) object;
g.setColor(oval.toColor());
g.drawOval(oval.x1, oval.y1, oval.x2, oval.y2);
}
else if (object.getClass().toString().equals(
"class DrawBorder$TDRectangle")) {
TDRectangle tDRectangle = (TDRectangle) object;
g.setColor(tDRectangle.toColor());
g.draw3DRect(tDRectangle.x1, tDRectangle.y1, tDRectangle.x2,
tDRectangle.y2, false);
}
}
}

public static void main(String[] args) {
DrawBorder db = new DrawBorder();
db.display();
db.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public Color toColor() {
if (this.color.equals("Blue")) {
return Color.BLUE;
}
else if (this.color.equals("Green")) {
return Color.GREEN;
}

else if (this.color.equals("Cyan")) {
return Color.CYAN;
}

else if (this.color.equals("Gray")) {
return Color.GRAY;
}

else if (this.color.equals("Orange")) {
return Color.ORANGE;
}

else if (this.color.equals("Red")) {
return Color.RED;
}
else
return Color.BLACK;
}

class Line extends DrawBorder {
Line(int x1, int y1, int x2, int y2, String color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}

class Rectangle extends DrawBorder {
Rectangle(int x1, int y1, int x2, int y2, String color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}

class Oval extends DrawBorder {
Oval(int x1, int y1, int x2, int y2, String color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}

class TDRectangle extends DrawBorder {
TDRectangle(int x1, int y1, int x2, int y2, String color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
}
}
怀新垒fb
2009-03-28 · TA获得超过378个赞
知道小有建树答主
回答量:391
采纳率:100%
帮助的人:361万
展开全部
//你的类我改了错误,现在可以用了

//有错的地方我标了“//有错”

//请认真看,我花了时间改的
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DrawLine {

public static void main(String[] args) {

new MyFrame(200, 200, 300, 300);
}
}

class MyFrame extends Frame {

Point p = new Point();
Point q = new Point();
boolean flag;
ArrayList<Point> a1 = new ArrayList<Point>();
ArrayList<Point> a2 = new ArrayList<Point>();

MyFrame(int x, int y, int w, int h) {
super("drawline");
setBounds(x, y, w, h);
setBackground(Color.white);
Button b = new Button("Line");
add(b, BorderLayout.NORTH);
b.addActionListener(new ButtonMonitor());
addMouseListener(new MouseMonitor());
addWindowListener(new WindowMonitor());
setVisible(true);
}

class ButtonMonitor implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "Line") {
flag = true;
}
repaint();
}
}

public void paint(Graphics g) {

Iterator<Point> i1 = a1.iterator();
Iterator<Point> i2 = a2.iterator();

while (i1.hasNext() && i2.hasNext()) {
p = i1.next();
q = i2.next();//有错
Color c = g.getColor();
g.setColor(Color.red);
g.drawLine(p.x, p.y, q.x, q.y);
g.setColor(c);
}

}

public void addPoint1(Point p) {
a1.add(p);
}

public void addPoint2(Point p) {
a2.add(p);
}

class MouseMonitor extends MouseAdapter {

public void mousePressed(MouseEvent e) {

MyFrame my = (MyFrame) e.getSource();
addPoint1(new Point(e.getX(), e.getY()));//有错 my->e
repaint();
}

public void mouseReleased(MouseEvent e) {//有错mouseDragged->mouseReleased

MyFrame my = (MyFrame) e.getSource();
addPoint2(new Point(e.getX(), e.getY()));//有错
repaint();
}
}

class WindowMonitor extends WindowAdapter {

@Override
public void windowClosing(WindowEvent e) {

setVisible(false);
System.exit(0);
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式