使用eclipse,我编写了一个试图在Windows中复制mspaint的程序,在构建或运行时没有错误,我只完成了几个函数来测试它是否可以绘制。
我写了一个名为"抽象" sotre绘制的图纸,
如果我设置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)));
}
}
答案 0 :(得分:0)
我没有看到它或方法show()
在哪里?
否则我只是看到了一些程序需要用windows做这样的事情:
setVisible(true);