3

Java仿写画图,内容不显示?

写了个功能简单的画图,可以使用画连续的曲线和圆,然而画曲线什么都不显示,画圆一松开鼠标圆就没了,求指出代码哪里错了
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class MyDraw extends JFrame{
static int current = 0;  // choice of different drawings, 0 means pencil 
 // 1 means circle
static int index=0; // the number of drawings
static int R,G,B;    // the color of drawings
static float Stroke=17.0f;  // the stroke of drawings
JLabel statusBar = new JLabel();// show the state of mouse
drawings []indexList=new drawings[5000];// store drawings to paint
DrawArea drawArea = new DrawArea();
public MyDraw(){
R=G=B=0;// initialize color
//add components to main window
add(drawArea);
setVisible(true);
setSize(1000,1000);
createNewItem();  // new a drawing
add(statusBar, BorderLayout.SOUTH);
 show();
}
// new a drawing
public void createNewItem(){
switch(current){
case 0 :indexList[index]=new pencil();break;
case 1 :indexList[index]=new circle();break;
}
}
public static void main(String args[]){
new MyDraw();
}
// the panel to paint on
class DrawArea extends JPanel{
public DrawArea(){
    setBackground(Color.white);
    setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    addMouseListener(new MousePolice1());
    addMouseMotionListener(new MousePolice2());
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d =(Graphics2D)g;
    for(int j=0;j<=index;j++){
        pass(indexList[index],g2d);
    }
}
public void pass(drawings i,Graphics2D g2d){
    i.draw(g2d);
}


}
class MousePolice2 extends MouseAdapter{
public void mouseDragged(MouseEvent e){


         statusBar.setText("     Mouse Dragged @:[" + e.getX() +
                    ", " + e.getY() + "]");
            if (current == 0) {

                indexList[index - 1].x1 = indexList[index].x2 = 
indexList[index].x1 = e.getX();
                indexList[index - 1].y1 = indexList[index].y2 = 
indexList[index].y1 = e.getY();
                index++;
                createNewItem();

            } else {
                indexList[index].x2 = e.getX();
                indexList[index].y2 = e.getY();
            }
            repaint();
        }

}
class MousePolice1 extends MouseAdapter{
public void mousePressed(MouseEvent e){
       statusBar.setText("     Mouse Pressed @:[" + e.getX() +
                ", " + e.getY() + "]");
        indexList[index].x1 = indexList[index].x2 = e.getX();
        indexList[index].y1 = indexList[index].y2 = e.getY();

        if (current == 0 ) {
            indexList[index].x1 = indexList[index].x2 = e.getX();
            indexList[index].y1 = indexList[index].y2 = e.getY();
            index++;
            createNewItem();
        }

}

public void mouseReleased(MouseEvent e){

       statusBar.setText("     Mouse Released @:[" + e.getX() +
                ", " + e.getY() + "]");
        if (current ==0) {
            indexList[index].x1 = e.getX();
            indexList[index].y1 = e.getY();
        }
        indexList[index].x2 = e.getX();
        indexList[index].y2 = e.getY();
        repaint();
        index++;
        createNewItem();
}
}


}

//the father 
class drawings implements Serializable{
int x1,x2,y1,y2;
void draw(Graphics2D g2d){}
}
// the drawing pencil
class pencil extends drawings{
void draw(Graphics2D g2d){
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke,
            BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
   g2d.drawLine(x1, y1, x2, y2);
}
}
class circle extends drawings
{
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(MyDraw.R,MyDraw.G,MyDraw.B));
g2d.setStroke(new BasicStroke(MyDraw.Stroke));
g2d.drawOval(Math.min(x1, x2), Math.min(y1, y2),
        Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)),
        Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2)));
}
}
查看全部
Inferior_G
Inferior_G
2017/06/09 14:36
  • components
  • swing
  • java
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复