Inferior_G 2017-06-09 14:36 采纳率: 100%
浏览 1050
已采纳

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)));
}
}
  • 写回答

2条回答 默认 最新

  • 冷咖啡15 2017-06-10 12:15
    关注

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    System.out.println(index);
    for (int j = 0; j <= index; j++) {
    pass(indexList[j], g2d);
    }
    }

    把这个函数里面的 index 改为 j
    
    如果有问题请追问,没有问题请采纳
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序