-1

Using eclipse, I wrote a program trying to copy mspaint in Windows,there is no errors when build or run, I only complete several functions to test if it can be paint.
I wrote the class named "drawings" to sotre drawings which has been paint, if I set current=0,it shows nothing ; if I set current=1, a circle shows up but when I release mouse,the circle disappear.

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
  • 23
  • 2
  • 6
  • 2
    1) *"I didn't wrote all the actionlisteners to buttons"* Then exclude those buttons, they are noise. In fact.. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Here is [code with with basic paint functtionaiity](https://stackoverflow.com/a/6133827/418556) on which you might base your own. – Andrew Thompson Jun 08 '17 at 16:51
  • 2
    Adding something else to @AndrewThompson's comment is: the MCVE or SSCCE should be correctly indented to make it easier for you and us to read and understand – Frakcool Jun 08 '17 at 16:54
  • I've changed the code,now it only contains necessary parts that possibly has problems.@Andrew – Inferior_G Jun 09 '17 at 13:40

1 Answers1

0

Didn't I see it or where is the method show()?

Otherwise I just saw some programs which they need to do something like this with the windows:

setVisible(true);
  • 2
    *"where is the method show() ?"* [`show()`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#show()) is a method that is deprecated, and belongs to `java.awt.Window` where `JFrame` inherits, as of Java 1.5, you need to call `setVisible(...)` method. He can use it because his class extends `JFrame` – Frakcool Jun 08 '17 at 16:51