2014-02-23 74 views
0

我被赋予了一个简单的使用GUI的绘图程序,它具有基本的I/O功能。那是我的教授被告知要做的。不过,我以前只做过一个GUI程序,所以直接跳到这个绘图程序中一直很头疼。现在我差不多完成了,但程序并没有像我预期的那样行事。在面板上绘制新对象时,它们会在其下方的对象上绘制不可见的白色矩形,从而擦除这些对象。我认为这是repaint(xMin,yMin,xMax - xMin + 1,yMax - yMin + 1)的结果;方法在DrawShapes中,但不能想出一种方法来解决它。我如何获得这个Paintcomponent按照我的意图行事?

另一方面,对象也没有正确保存。我可以按照我的意图将它导出为jpg,但是,它只会导出最后一个绘制的图像,而不是导出到paintComponent画布上的所有图像。

最后,DrawShapes中的清除方法的工作方式非常类似。当清除方法被激活时,它将清除除最后绘制的图像之外的所有内容。

有没有人比我更熟悉这些工具,可以看到一种方法来解决这些问题?这只是我用过的第一个程序,和I/O。

这里是形状都应该上绘制的面板类:

/** 
* @author me 
*/ 
import java.util.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import java.io.File; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import javax.swing.event.*; 

public class DrawShapes extends JPanel{ 
    Point startPoint = null; 
    Point endPoint = null; 
    public int drawType = 1; 
    BufferedImage image; 
    Graphics2D g2d; 

public DrawShapes(){ 
    setBackground(Color.WHITE); 

    MyMouseListener m1 = new MyMouseListener(); 
    addMouseListener(m1); 
    addMouseMotionListener(m1); 
}//end constructor 

//sets draw type, which is the decider of what is being drawn. 
public void setType(int type){ 
    if(type == 1) 
    { 
     drawType = 1; 
    } 
    else if(type == 2) 
    { 
     drawType = 2; 
    } 
    else if(type == 3) 
    { 
     drawType = 3; 
    } 
}//end setType 


    public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    if (image == null) 
    { 
     createEmptyImage(); 
    } 

    g.drawImage(image, 0, 0, null); 

    if (startPoint != null && endPoint != null) 
    { 
      int x = Math.min(startPoint.x, endPoint.x); 
      int y = Math.min(startPoint.y, endPoint.y); 
      int width = Math.abs(startPoint.x - endPoint.x); 
      int height = Math.abs(startPoint.y - endPoint.y); 
      switch (drawType) 
       { 
        case 1: 
         g.drawRect(x, y, width, height); 
         break; 
        case 2: 
         g.drawOval(x, y, width, height); 
         break; 
        case 3: 
         g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y); 
         break; 
       } 
    } 
}//end paintComponent 

public void save() 
{ 
    BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB); 
    Graphics g = bi.createGraphics(); 
    this.paint(g); 
    g.dispose(); 
    try{ImageIO.write(bi, "png",new File("test.png")); 
    }catch (Exception e){} 
    } 

private void createEmptyImage() 
{ 
    image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); 
    g2d = (Graphics2D)image.getGraphics(); 
    g2d.setColor(Color.BLACK); 
    g2d.drawString("Add a shape by clicking and dragging.", 40, 15); 
} 

public void addRect(int x, int y, int width, int height, Color color) 
{ 
     g2d.setColor(color); 
     g2d.drawRect(x, y, width, height); 
     repaint(); 
} 

public void addOval(int x, int y, int width, int height, Color color) 
{ 
     g2d.setColor(color); 
     g2d.drawOval(x, y, width, height); 
     repaint(); 
} 

public void addLine(int x1, int y1, int x2, int y2, Color color) 
{ 
     g2d.setColor(color); 
     g2d.drawLine(x1, y1, x2, y2); 
     repaint(); 
} 

public void clear() 
{ 
     createEmptyImage(); 
     repaint(); 
} 

class MyMouseListener extends MouseInputAdapter 
{ 
    private int xMin; 
    private int xMax; 
    private int yMin; 
    private int yMax; 

    public void mousePressed(MouseEvent e) 
    { 
      startPoint = e.getPoint(); 
      endPoint = startPoint; 
      xMin = startPoint.x; 
      xMax = startPoint.x; 
      yMin = startPoint.y; 
      yMax = startPoint.y; 
    } 

    public void mouseDragged(MouseEvent e) 
    { 
     //This is code I found that should make it so the only area affected by the dragging is repainted. 
     endPoint = e.getPoint(); 
     xMin = Math.min(xMin, endPoint.x); 
     xMax = Math.max(xMax, endPoint.x); 
     yMin = Math.min(yMin, endPoint.y); 
     yMax = Math.max(yMax, endPoint.y); 
     repaint(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1); 

    } 

    public void mouseRelease(MouseEvent e) 
    { 
     //This code paints the shapes on the Buffered Image created as a canvas 
     int x = Math.min(startPoint.x, endPoint.x); 
     int y = Math.min(startPoint.y, endPoint.y); 
     int width = Math.abs(startPoint.x - endPoint.x); 
     int height = Math.abs(startPoint.y - endPoint.y); 

     if (width != 0 || height != 0) 
    { 
     g2d.setColor(e.getComponent().getForeground()); 
//  g2d.drawRect(x, y, width, height); 
       switch (drawType) 
       { 
        case 1: 
         addRect(x, y, width, height, e.getComponent().getForeground()); 
         break; 
        case 2: 
         addOval(x, y, width, height, e.getComponent().getForeground()); 
         break; 
        case 3: 
         addLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, e.getComponent().getForeground()); 
         break; 
       }//end switch statement. 
    } 

    startPoint = null; 
// repaint(); 
    } 
} 

}//end class 

这里是对UI代码:

/*@author Me*/ 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class MyDrawUI extends JFrame 
{ 
    private DrawShapes draw = new DrawShapes(); 
    private JPanel ButtonPanel = new JPanel(); 
    private JFrame window = new JFrame("Draw!"); 
    //constructor 
    MyDrawUI(){ 
     buildUI(); 
    } 

void buildUI() 
{ 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setLayout(new GridLayout(2,2)); 
    window.add(draw); 
    window.add(ButtonPanel, BorderLayout.SOUTH); 
    ButtonPanel.setBackground(Color.LIGHT_GRAY); 
    draw.setBackground(Color.WHITE); 

    //define buttons 
    JButton rectangle = new JButton("Rectangle"); 
    JButton oval = new JButton("Oval"); 
    JButton line = new JButton("Line"); 
    JButton exit = new JButton("Exit"); 
    JButton save = new JButton("Save"); 
    JButton clear = new JButton("Clear"); 

    //add buttons 
    ButtonPanel.add(rectangle, BorderLayout.SOUTH); 
    ButtonPanel.add(oval, BorderLayout.SOUTH); 
    ButtonPanel.add(line, BorderLayout.SOUTH); 
    ButtonPanel.add(clear, BorderLayout.SOUTH); 
    ButtonPanel.add(save, BorderLayout.SOUTH); 
    ButtonPanel.add(exit, BorderLayout.SOUTH); 
    ButtonPanel.setSize(100, 100); 

    save.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
     draw.save(); 
     } 
    }); 

    clear.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
     draw.clear(); 
     } 
    }); 

    rectangle.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      draw.setType(1); 
     } 
    }); 

    oval.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      draw.setType(2); 
     } 
    }); 

    line.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      draw.setType(3); 
     } 
    }); 

    exit.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    }); 

    window.setVisible(true); 
    window.setSize(1024, 800); 
} 
} 
+0

*“最后,..”* SO不是一站式'修理我的代码商店'。这是一个问答网站,理想情况下,每个线程有一个接受答案的问题。那么..你对此线程的问题是什么? –

+0

你最好将形状放入'List'并重新绘制列表... – MadProgrammer

+0

我想问的是,当你试图绘制多个物体时,paintcomponent如何工作。原来我看着错误的地方。尽管如此,感谢MadProgrammer,它现在按预期工作。有时多个问题可以通过一个答案来解决,并纠正错字。 – user3344561

回答

3

有几个问题,我可以看,主要的一个事实是,你“认为”你已经重写了MouseAdaptor类中的方法,但没有

mouseRelease不是会导致任何事件触发它。你之后的方法是mouseReleased

当覆盖方法时,请使用@Override注释,如果您认为重写的方法不存在于任何父类中,则会导致编译器错误。

@Override 
public void mouseReleased(MouseEvent e) { 

其他一些东西弹出。

  1. MyDrawUI类从JFrame延伸,但你创造的另一个JFrame称为window,在你创建你的UI一个实例。在这种情况下,从MyDrawUI类中删除extends JFrame,因为它只是增加了混淆...
  2. 保持对Graphics上下文的引用,即使创建了上下文,也不建议在此上下文中使用。在某些系统上,除非您致电dispose,否则可能没有任何内容会落实到底层实现中。相反,只需在需要时使用image.getGraphics,并在完成之后致电g2d.dispose
相关问题