2015-10-05 50 views
0
/** 
* Created on Sep 3, 2012 
* @author cskim -- hufs.ac.kr, Dept of CSE 
* Copy Right -- Free for Educational Purpose 
*/ 
package assignment_01; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 

/** 
* @author cskim 
* 
    */ 


public class NxNImageLabel extends JFrame implements ActionListener { /////// 

private JPanel contentPane; 

private static final int PROWS = 16; ///////// 
private static final int PSIZE = PROWS*PROWS; 
private ImageIcon[] btnOneImage = new ImageIcon[PSIZE];////// 
private BufferedImage[] imageData = new BufferedImage[PSIZE];/////// 

private JButton[] tiles = new JButton[PSIZE]; ///////////////// 
private BufferedImage tileImage = null; 

int scrHeight = 0; 
int scrWidth = 0; 
int bwidth = 0; 
int bheight = 0; 
/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       NxNImageLabel frame = new NxNImageLabel(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public NxNImageLabel() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 500, 500); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(new GridLayout(PROWS, PROWS, 0, 0)); 
    initialize(); 
} 



void initialize(){ 
    scrHeight = this.getHeight(); 
    scrWidth = this.getWidth(); 
    //System.out.println("w="+scrWidth+" h="+scrHeight); 
    try { 
     tileImage = ImageIO 
       .read(getClass().getResource("/images/dambee.jpg"));///// 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    int pwidth = tileImage.getWidth()/PROWS; 
    int pheight = tileImage.getHeight()/PROWS; 
    bwidth = scrWidth/PROWS; 
    bheight = scrHeight/PROWS; 

    for (int i=0; i<PROWS; ++i){ 
     for (int j=0; j<PROWS; ++j){ 
     imageData[PROWS*i+j] = 
      tileImage.getSubimage(j*pwidth, i*pheight, pwidth, pheight); 
     } 
    } 

    ////////////////////////////////////////////////////// 
    for(int i=0;i<PSIZE;++i){ 
     tiles[i] = new JButton(); 
     tiles[i].setIcon(new ImageIcon(imageData[i])); 
     tiles[i].addActionListener(this);/////// 
     contentPane.add(tiles[i]); 
    }//end of actionperformed 

    } 
    ////////////////////////////////////////////////////// 

    /* ImageIcon tileIcon = new ImageIcon(tileImage.getScaledInstance(bwidth, bheight, Image.SCALE_SMOOTH)); 
    for (int i=0; i<PSIZE; ++i){ 
     tiles[i] = new JButton(); /////// 
     tiles[i].setIcon(tileIcon); 
     tiles[i].addActionListener(this);/////// 
     contentPane.add(tiles[i]); 
    }*/ 


public int clickCount = 0;///// 
@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    JButton button = (JButton) e.getSource(); 
     BufferedImage imageData2 = (BufferedImage) ((ImageIcon)button.getIcon()).getImage(); 
     Color bcol = getAverageColor(imageData2); 
     button.setIcon(new ImageIcon(OneColorBufferedImage.getBufferedImage(bwidth, bheight, bcol))); 

     clickCount++; 
     if (clickCount % 2 == 0) { 
      //button.setIcon(new ImageIcon(OneColorBufferedImage.getBufferedImage(bwidth, bheight, bcol))); 
      button.setIcon(new ImageIcon(OneImageButton.class.getResource("/images/dambee.jpg"))); 
     } else { 
      button.setIcon(new ImageIcon(OneImageButton.class.getResource("/images/kiss.jpg"))); 
     } 
     System.out.println(clickCount); 

} 
Color getAverageColor(BufferedImage tile){ 
     int twidth = tile.getWidth(); 
     int theight = tile.getHeight(); 
     double pixSize = twidth*theight; 
     double sumRed = 0; 
     double sumGreen = 0; 
     double sumBlue = 0; 
     Color pixColor = null; 
     for (int i=0; i<theight; ++i){ 
     for (int j=0; j<twidth; ++j){ 
     pixColor = new Color(tile.getRGB(i,j)); 
     sumRed += pixColor.getRed(); 
     sumGreen += pixColor.getGreen(); 
     sumBlue += pixColor.getBlue(); 
     } 
     } 
     int avgRed = (int)(sumRed/pixSize); 
     int avgGreen = (int)(sumGreen/pixSize); 
     int avgBlue = (int)(sumBlue/pixSize); 
     return new Color(avgRed, avgGreen, avgBlue); 
    } 
    } 

我试图使16x16图像标签可点击jbutton。如果我点击一次,应该改变马赛克图像,然后点击两次,应该改变原始图像,并点击三次,应该返回马赛克图像。 我在public void actionPerformed函数中有问题,if else部分。 我认为,如果部分被纠正,但我不知道如何完成其​​他部分。 任何人都可以帮助我吗?可点击jbutton,如果其他在java

+0

不知您烦恼吗? –

+0

你有什么困难? – Stultuske

+0

我编辑!我不知道如何完成其​​他语句... –

回答

2

也许更换的if/else与三元:

String imgUrl = clickCount % 2 == 0 ? "/images/dambee.jpg" : "/images/kiss.jpg"; 
button.setIcon(new ImageIcon(OneImageButton.class.getResource(imgUrl))); 

三元运算符(和:)可以通过英语单词thenelse理解相当多?

所以第一线的右侧写着“不会的clickCount%2等于0?如果是这样,则返回 “/images/dambee.jpg”,否则返回 “/images/kiss.jpg”。

您还可以插入几个换行符成一条线,为进一步明晰:

String imgUrl = clickCount % 2 == 0 
    ? "/images/dambee.jpg" 
    : "/images/kiss.jpg"; 

我认为无论哪种方式是好的,双方最好到的if/else解决方案,毕竟,一个图标的分配情况在两个逻辑分支中 - 只是依赖于clickCount的特定网址。

我记得当我开始编程时对三元运算符有点困惑,所以一开始感觉有点不舒服是完全没问题的。但是,养成使用它的习惯可能是一个好主意,因为在你的日子里你肯定会多次看到它作为编码器:)

+1

紧凑整洁的代码... gud + 1 ..但我认为你一定要写一点解释OP是一个初学者.. – SSH

0

你应该为所有按钮单独保留点击计数器。所以定义一个点击计数器阵列。

private int[] btnClicks = new int[PSIZE];////// 

,然后,你应该找哪个按钮被点击的actionPerformed

public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    JButton button = (JButton) e.getSource(); 
. 
. 
for(int i=0;i<PSIZE;++i){ 
    if (tiles[i] == button){ 
    btnClicks[i] ++; 
    if (btnClicks[i]t % 2 == 0) { 
      button.setIcon(new ImageIcon(OneImageButton.class.getResource("/images/dambee.jpg"))); 
     } else { 
      BufferedImage img = ImageIO.read(...); // learn how to load images from resource. "/images/kiss.jpg" 
      int w = button.getWidth(); 
      int h = button.getHeight(); 
      img = img.getSubimage(w*(i/16), h*(i%16), w, h); 
      button.setIcon(img); 
     } 

    } 
    } 
} 

裁判:How to draw part of a large BufferedImage?

+0

它没有工作... –

+0

哦...对不起,它的工作!谢谢!!! –

+0

我可以再问一件吗?如果我想在其他语句中用卡盘尺寸制作原始图像,我该怎么办? –

相关问题