2011-09-28 114 views
2

所以下面我正在为类的绘画类型项目,让你绘制形状线等,现在我的教授希望我们添加一个橡皮擦工具,可以让你擦除部分图像,它是在缓冲的图像上有什么想法? IM刚从帮助用橡皮擦从缓冲图像

import javax.imageio.ImageIO; 
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.image.BufferedImage; 
    import java.io.File; 
    import java.io.IOException; 
    import java.util.*; 


     public class PaintProgram extends JPanel implements MouseListener,ActionListener 
    { 
public static int stroke = 0; 
private int xX1, yY1 , xX2, yY2, choice ; 

public static void main(String [] args) 
{ 
    new PaintProgram(); 
} 

PaintProgram() 
{ 


    JFrame frame = new JFrame("Paint Program"); 
    frame.setSize(1200, 800); 
    frame.getContentPane().add(this); 


    JMenuBar menuBar = new JMenuBar(); 
    frame.setJMenuBar(menuBar); 
    JMenu help = new JMenu("Help!!?"); 
    menuBar.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 
    about.addActionListener(this); 



    JButton button1 = new JButton("Clear"); 
    button1.addActionListener(this); 
    JButton button2 = new JButton("Filled rectangle"); 
    button2.addActionListener(this); 
    JButton button3 = new JButton("Filled oval"); 
    button3.addActionListener(this); 
    JButton button4 = new JButton("Empty rectangle"); 
    button4.addActionListener(this); 
    JButton button5 = new JButton("Empty oval"); 
    button5.addActionListener(this); 
    JButton button6 = new JButton("Line"); 
    button6.addActionListener(this); 
    JRadioButton thin = new JRadioButton("Thin Line"); 
    thin.addActionListener(this); 
    JRadioButton medium = new JRadioButton("Medium Line"); 
    medium.addActionListener(this); 
    JRadioButton thick = new JRadioButton("Thick Line"); 
    thick.addActionListener(this); 



    ButtonGroup lineOption = new ButtonGroup(); 
    lineOption.add(thin); 
    lineOption.add(medium); 
    lineOption.add(thick); 



    this.add(button1); 
    this.add(button2); 
    this.add(button3); 
    this.add(button4); 
    this.add(button5); 
    this.add(button6); 
    this.add(thin); 
    this.add(medium); 
    this.add(thick); 
    addMouseListener(this); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



} 

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D)g; 
    if(grid == null){ 
     int w = this.getWidth(); 
     int h = this.getHeight(); 
     grid = (BufferedImage)(this.createImage(w,h)); 
     gc = grid.createGraphics(); 
    } 
    g2.drawImage(grid, null, 0, 0); 
    check(); 
} 
BufferedImage grid; 
Graphics2D gc; 


public void draw() 
{ 
    Graphics2D g = (Graphics2D)getGraphics(); 
    int w = xX2 - xX1; 
     if (w<0) 
     w = w *(-1); 

    int h = yY2-yY1; 
     if (h<0) 
     h= h*(-1); 

    switch(choice) 
    { 
     case 1: 
      check(); 
      gc.setColor(Color.YELLOW); 
      gc.drawRect(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 2: 
      check(); 
      gc.setColor(Color.CYAN); 
      gc.drawOval(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 3: 
      check(); 
      gc.setColor(Color.ORANGE); 
      gc.drawRect(xX1, yY1, w, h); 
      gc.fillRect(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 4: 
      check(); 
      gc.drawOval(xX1, yY1, w, h); 
      gc.setColor(Color.PINK); 
      gc.fillOval(xX1, yY1, w, h); 
      repaint(); 
      break; 

     case 5: 
      gc.setColor(Color.MAGENTA); 
      if (stroke == 0) 
      gc.setStroke(new BasicStroke (1)); 
      if (stroke == 1) 
      gc.setStroke(new BasicStroke (3)); 
      if (stroke == 2) 
      gc.setStroke(new BasicStroke (6)); 
      gc.drawLine(xX1, yY1, xX2, yY2); 
      repaint(); 
      break; 

     case 6: 
      //Acquire clear screen or gay 
      break; 
    } 
} 

public void check() 
{ 
    if (xX1 > xX2) 
    { 
     int z = 0; 
     z = xX1; 
     xX1 = xX2; 
     xX2 =z; 
    } 
    if (yY1 > yY2) 
    { 
     int z = 0; 
     z = yY1; 
     yY1 = yY2; 
     yY2 = z; 
    } 
} 



public void actionPerformed(ActionEvent e) 
{ 


if (e.getActionCommand().equals("About")) 
{ 
    System.out.println("stfu"); 
    JFrame about = new JFrame("About"); 
    about.setSize(300, 300); 
    BufferedImage img = null; 
    try{ 
    img = ImageIO.read(new File("C:/Users/TehRobot/Desktop/Logo.png")); 
    }catch (IOException e1) 
    { 

    } 
    about.setVisible(true); 
} 

if (e.getActionCommand().equals("Empty rectangle")) 
{   
    System.out.println("Empty Rectangle Has Been Selected~"); 
    choice = 1; 

    } 

if (e.getActionCommand().equals("Empty oval")) 
{   
System.out.println("Empty Oval Has Been Selected!"); 
    choice = 2; 
    } 


if (e.getActionCommand().equals("Filled rectangle")) 
{   
    System.out.println("Filled Rectangle Has Been Selected"); 
    choice = 3; 
    } 


if (e.getActionCommand().equals("Filled oval")) 
{   
System.out.println("Filled Oval Has Been Selected"); 
    choice = 4; 
    } 


if (e.getActionCommand().equals("Line")) 
{ 
    System.out.println("Draw Line Has Been Selected"); 
    choice = 5; 
} 

if (e.getActionCommand().equals("Thin Line")) 
{ 
    stroke = 0; 
} 

if (e.getActionCommand().equals("Medium Line")) 
{ 
    stroke = 1; 
} 

if (e.getActionCommand().equals("Thick Line")) 
{ 
    stroke = 2; 
} 

if (e.getActionCommand().equals("Clear")) 
{   
System.out.println("Clear All The Things!!!"); 
    choice = 6; 
    repaint(); 
} 


} 

public void mouseExited(MouseEvent evt){} 
public void mouseEntered(MouseEvent evt){} 
public void mouseClicked(MouseEvent evt){} 
public void mousePressed(MouseEvent evt) 
{ 

    xX1 = evt.getX(); 
    yY1= evt.getY(); 

    } 
public void mouseReleased(MouseEvent evt) 
{ 
    xX2 =evt.getX(); 
    yY2=evt.getY(); 
    draw(); 
    } 

} 
+0

使用有意义的变量名称。到底是什么xx1,yy1,w,h? “图像的部分”是什么意思?你想删除一个特定的形状?你是否想通过像橡皮擦那样的鼠标“擦掉”不同形状的部分?你想能够点上一个点,并从这一点上删除一个矩形? – camickr

回答

4

方式一:Graphics2D的有clearRect(...)方法,可以让你设置一个矩形的背景色(通过的setBackground(...)方法设置)。为什么不将它与MouseListener和MouseMotionListener结合使用(在MouseAdapter中组合使用)?

+0

所以基本上做一个非常小的矩形,跟随鼠标并应用背景颜色? – CMOS

+0

@CalvinMoss:类似的东西。我会让它只在按下和/或拖动时应用背景颜色。 –