1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.ArrayList; import javax.imageio.ImageIO; public class MandarinTrainer extends JFrame{ private JPanel pmain=null; private BufferedImage bufimage[]=null; private int width=0; private int height=0; private ArrayList<Integer> px=null; private ArrayList<Integer> py=null; private int imgorder=0; private boolean ori=true; private float fcompo[]; private boolean areaclicked=false; public MandarinTrainer(){ super("Mandarin Trainer"); setSize(450,450); pmain=new DrawPanel(); pmain.setPreferredSize(new Dimension(450,450)); pmain.setBackground(Color.WHITE); pmain.addMouseListener(new MouseHandler()); pmain.addMouseMotionListener(new MouseHandler()); add(pmain); bufimage=new BufferedImage[4]; try{ bufimage[0]=ImageIO.read(new File("earth1.png")); bufimage[1]=ImageIO.read(new File("earth2.png")); bufimage[2]=ImageIO.read(new File("earth3.png")); }catch(Exception ex){ } px=new ArrayList<Integer>(); py=new ArrayList<Integer>(); fcompo=new float[bufimage.length]; for(int i=0;i<fcompo.length;i++){ fcompo[i]=0.1f; } setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; for(int i=0;i<bufimage.length;i++){ g2.setColor(new Color(0,0,0)); Composite c=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,fcompo[i]); g2.setComposite(c); g2.drawImage(bufimage[i],0,0,null); } } } void getPixel(int imageordertemp){ width=bufimage[imgorder].getWidth(); height=bufimage[imgorder].getHeight(); px.clear(); py.clear(); for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ int currentpixel=bufimage[imgorder].getRGB(j,i); if(currentpixel!=-1){ px.add(j); py.add(i); } } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mousePressed(MouseEvent e){ if(imgorder>=3){ return; } getPixel(imgorder); for(int i=0;i<px.size();i++){ if(e.getX()==px.get(i)&&e.getY()==py.get(i)){ areaclicked=true; break; } } } public void mouseDragged(MouseEvent e){ if(imgorder>=3||areaclicked==false){ return; } for(int i=0;i<px.size();i++){ if(e.getX()==px.get(i)&&e.getY()==py.get(i)){ areaclicked=true; } } } public void mouseReleased(MouseEvent e){ if(imgorder>=3||areaclicked==false){ return; } areaclicked=false; for(int i=0;i<px.size();i++){ if(e.getX()==px.get(i)&&e.getY()==py.get(i)){ areaclicked=true; break; } } if(areaclicked==true){ fcompo[imgorder]=0.5f; imgorder++; areaclicked=false; repaint(); } } public void mouseClicked(MouseEvent e){ } } public static void main(String[] args){ new MandarinTrainer(); } } |
Monday, May 2, 2016
Mandarin Training Tool
This is not a real training tool. The main purpose of this program is to demonstrate the use of AlphaComposite class that implements the Composite interface to display partly transparent images.
Subscribe to:
Posts (Atom)