【问题标题】:Java program to paint shows nothing用于绘制的 Java 程序不显示任何内容
【发布时间】:2017-06-08 16:37:58
【问题描述】:

我用eclipse写了一个程序试图在windows下复制mspaint,构建或运行都没有报错,我只完成了几个函数来测试是否可以画图。
我写了一个名为“drawings”的类来写一些已经画过的画, 如果我设置current=0,它什么也不显示;如果我设置current=1,会出现一个圆圈,但是当我释放鼠标时,圆圈会消失。

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)));
}
}

【问题讨论】:

  • 1) “我没有将所有的动作监听器都写到按钮上” 然后排除那些按钮,它们是噪音。事实上.. 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 这是code with with basic paint functtionaiity,您可以根据它自己。
  • 在@AndrewThompson 的评论中添加其他内容是:MCVE 或 SSCCE 应正确缩进,以便您和我们更容易阅读和理解
  • 我已经更改了代码,现在它只包含可能有问题的必要部分。@Andrew

标签: java swing jpanel draw


【解决方案1】:

我没看到还是show()的方法在哪里?

否则我只是看到了一些他们需要在 windows 上执行类似操作的程序:

setVisible(true);

【讨论】:

  • "show() 方法在哪里?" show() 是一种已被弃用的方法,属于 java.awt.Window,其中 JFrame 继承自 Java 1.5,需要调用setVisible(...)方法。他可以使用它,因为他的类扩展了JFrame
猜你喜欢
  • 2012-12-30
  • 2013-06-11
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多