// 打开选择器面板 int returnVal = chooser.showOpenDialog(downPanel); if (returnVal == JFileChooser.APPROVE_OPTION) { selectedFiles = chooser.getSelectedFiles(); repaint(); } } });
jbtStr1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c=Color.red; flag=10; } });
jbtStr2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c=Color.green; flag=10; } });
jbtStr3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c=Color.red; flag=11; } });
jbtStr4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c=Color.green; flag=11; } });
downPanel.addMouseListener(new MouseListener(){ public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); } public void mouseReleased(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); Graphics g = downPanel.getGraphics(); // System.out.println(\ switch (flag){ case 0: Draw d =proFactory.produce(name);
d.draw(g, x, y, x1, y1, c);break; case 1: con = new Rec(); con.paint(g, x, y, x1, y1, c); break; case 2: con = new Oval(); con.paint(g, x, y, x1, y1, c);break; case 3: con = new Tri(); con.paint(g, x, y, x1, y1, c);break; case 4: con = new Rec(); con.paint(g, x, y, x1, y1, c);break; case 5: con = new Tri(); con.paint(g, x, y, x1, y1, c);break; case 6: con = new Oval(); con.paint(g, x, y, x1, y1, c);break; case 7: con = new Rec(); con.paint(g, x, y, x1, y1, c);break; case 8: con = new Oval(); con.paint(g, x, y, x1, y1, c);break; case 9: con = new Tri(); con.paint(g, x, y, x1, y1, c);break; }
if(flag==1||flag==2||flag==3){ Tri_Tri tr = new Tri_Tri(con); tr.paint(g, x, y, x1, y1, c); flag=0; }
if(flag==4||flag==5||flag==6){ Oval_Oval tr = new Oval_Oval(con); tr.paint(g, x, y, x1, y1, c); flag=0; }
if(flag==7||flag==8||flag==9){ Rec_Rec tr = new Rec_Rec(con); tr.paint(g, x, y, x1, y1, c); flag=0; }
if(flag==10){ size s = new redbig(); float size; size = s.bepaint(); pen p = new bigpen(); p.draw(name, g, x, y, x1, y1, c,size); }
if(flag==11){
size s = new greensmall(); float size; size = s.bepaint(); pen p = new bigpen(); p.draw(name, g, x, y, x1, y1, c,size); }
/* */ if (flag==-1){ Point point = MouseInfo.getPointerInfo().getLocation(); int thisX = x1; int thisY = y1; System.out.println(\ if ((y - thisY < 20 && y - thisY > 0) || (y - thisY < 0 && y - thisY > -20)) { // Y 在20范围内移动认为是水平移动 if (x < thisX) { // right if (selectedFiles != null && fileIndex < selectedFiles.length - 1) { fileIndex++; } } else { // left if (selectedFiles != null && fileIndex > 0) { fileIndex--; } } } else { if (x < thisX) { // 右下 width += 20; height += 20; } else { // 左上 width -= 20; height -= 20; } } repaint();
class ImagePanel extends JPanel { public void paint(Graphics g) { super.paint(g);
if (selectedFiles != null) { ImageIcon icon = new ImageIcon(selectedFiles[fileIndex] .getPath()); g.drawImage(icon.getImage(), 0, 0, width, height, this); } } } } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); ///// } public void showFrame(){
this.setTitle(\ this.setLayout(null); this.setSize(1000, 700); this.setLocation(600, 300);
this.setDefaultCloseOperation(3); this.setVisible(true);
}
public static void main(String[] args) { new MainFrame(); } }
package 界面;
import java.awt.Graphics;
public class Jpanel { public int n; public int x[] = null; public int y[] = null; public void paintComponent(Graphics g) { g.drawPolygon(x, y, n);
} public void paintComponent1(Graphics g){ g.drawOval(x[0], x[1], x[2], x[3]); } }
简单工厂模式:
package 简单工厂模式;
import java.awt.Color; import java.awt.Graphics; import 装饰模式.Decorator; public interface Draw{ public abstract void draw(Graphics g,int x,int y,int x1,int y1,Color c); public abstract void draw(Graphics g,int x,int y,int x1,int y1,Color c,float size); }
package 简单工厂模式;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D;
import 界面.*;
import 装饰模式.Conpotent;
public class DrawOval implements Draw{ public void draw(Graphics g, int x1, int y1, int x2, int y2, Color c) { Jpanel jp = new Jpanel(); jp.n = 3; jp.x = new int[]{x1