简答题 | 本题的功能是跟踪鼠标在窗口的操作,以及在窗口的坐标,包括移进、移出、移动、单击、释放和拖曳,并将这些信息显示在窗口的文字标签上。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java3 extends JFrame implements MouseL- istener MouseMotionListener { private JLabel statusBar; public java3() { super("java3"); statllsBar=new TLabel(); getContentPane.add(statusBar,BorderLayout.SOUTH); addMouseListener(this); addMouseMotionListener(this); setSize(275,100); show(); } public void mouseClicked(MouseEvent e) { statusBar.setText("Clicked at["+e.getX()+ ","+e.getY()+"]"); } public void mousePressed(MouseEvent e) { statusBar.setText("Pressed at["+e.getX()+ ","+e.getY()+"]"); } public void mouseReleased(MouseEvent e) { statusBar.setText("Released at["+e.getX() +","+e.getY()+"]"); } public void mouseln(MouseEvent e) { statusBar.setText("Mouse in window"); public void mouseExited(MouseEvent e) { statusBar.setText("Mouse outside window"); } public void mouseDragged(MouseEvent e) { statusBar.setText("Dragged at["+e.getX() +","+e.getY()+"]"); } public void mouseMoved(MouseEvent e) { statusBar.setText("Moved at["+e.getX()+ ","+e.getY()+"]"); } public static void main(String args[]) { java3 app=new java3(); app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; |
查看答案 |
简答题 | 本题定义了一个求两个数的最大值的方法max,并调用该方法计算67和23的最大值。 public class javal{ public static void main(String[]args){ javal temp=new javal(); int res=max(67,23); System.out.println("res="+res); } static int maX(___)} int maxNum; if(a> b) ______; else maxNum=b; ______; } } |
查看答案 |
简答题 | 本题的功能是,用户利用单选按钮选择性别,在下面的标签中显示用户的选择结果。 import java.awt.*; __________; public class java2{ public static void main(String args[]){ Frame f=new Frame("java2"); f.setLayout(new GridLayout(3,1)); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(O); } }); CheckboxGroup cg=new CheckboxGroup(); Checkbox male=new Checkbox("男",cg,false); Checkbox female=new Checkbox("女",cg,false); final Label la=new Label("请选择你的性别"); male.addItemListener(new hemListener(){ public void itemStateChanged(ItemEvent e){ la.setText("你是个男生?"); } }); female.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ la.setText("你是个女生?"); } }); f.add(male); f.add(female); f.add(la); f.setSize(200,200): _______; } } |
查看答案 |
简答题 | 本题的功能是对列表项的操作,包括删除、添加和反选。窗司中有两个列表框和5个按钮,按钮标签代表着移除列表项的方向,“>”代表只移除选中的列表项,“>>”代表移除所有的列表项,“!”代表反向选择列表项。 |
查看答案 |
简答题 | 本题定义了一个长度为l0的boolean型数组,并给数组元素赋值,要求如果数组元素下标为奇数,则数组元素值 为false,否则为true。 public class javal{ pubhc static void main(String[]args){ boolean b[]= ; for(int i=0;i<10;i++){ if( ) b[i]=false; else ; } for(int i=0;i<10;i++) System.Out.print("bE"+i+"]="+b[i]+","); } } |
查看答案 |
简答题 | 本题的功能是通过按钮来选择窗口显示的风格。窗口 中有三个按钮:“Metal”、“Motif”和“Windows”,单击任何一 个按钮,就能将窗口的风格改变为按钮名称所对应的风格。 import java.awt.*; import java.awt.event.*; import javax.swing.*; class PlafPanel extends JPanel implements ActionLis- tener {public () {metaIButton=new JButton("Metal"); motifButtOn=new J Button("Motif"); windowsButton=new JButton("Windows"); add(metalButton); add(motifButton); add(windowsButton); metalButton.addActionListener(this); motifButton.addActionListener(this); windowsButton.addActionListener(this); } Dublic void actionPerformed(ActionEvent evt) {Object source=evt.getSource(); String plaf="": if(source= =metalButton) plaf="javax.swing.plaf.metal.MetalLookAnd- Feel"; else if(source= =motifButton) plaf="com.sun.java.swing.plaf.motif.Moti- fLookAndFeel"; else if(source= =windowsButton) Dlaf="com.sun.java.swing.plaf.windows.Win- dowsLookAndFeel"; try {UIManager.setLookAndFeel( ); SwingUtilities.updateComponentTreeUI(this); } catch(Exception e){) } private JButton metalButton; private JButton motifButton; private JButton windowsButton; } class PlafFrame extends JFrame {public PlafFrame() { setTitle("simple"); setSize(300,200); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(O); } }); Container contentPane=getContentPane(); contentPane.add(new PlafPanel()); } } public class java2 {public static void main(String[]args) f JFrame frame=new PlafFrame(); frame.show(); } |
查看答案 |
简答题 | 本题中使用了选项卡,窗口中有一个选项卡,总共有“系统”、“声卡”、“显卡”、“网卡”和“帮助”5项选项面。单击各个选项的文字标签后,所选中的选项将为当前选项。“系统”选项上有3个复选按钮,分别控制“声卡”、“显卡”和“网卡”三个选项,选中某个按钮后,它所指示的选项就可用,否则不可用。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class java3 extends JFrame{ JTabbedPane config=new JTabbedPane(): public java3(){ super("java3"); setSize(500,300); setDefaultCloseoperation(EXIT_ON_CLOSE); JPanel configPane=new JPanel(); configPane.setLayout(new BoxLayout(config-Pane,BoxLayout.Y_AXIS)); JTextArea question=new JTextArea("下面的哪个选项\n"+"你想设置?"); question.setEditable(false); question.setMaximumSize(new Dimension(300,50)); question.setAlignmentX(0.Of); question.setBackground(configPane.getBack-ground()); JCheckBox audioCB=new JCheckBox ("声卡",true); JCheckBox nicCB=new JCheckBox("网卡",true); JCheckBox tvCB=new JCheckBox("显示卡",false); configPane.add(Box.createVerticalGlue()); configPane.add(question); configPane.add(audioCB); configPane.add(nicCB); configPane.add(tvCB); configPane.add(Box.ereateVerticalGlue()); JLabel audioPane=new JLabel("声卡页面"); JLabel niePane=new JLabel("网卡页面"); JLabel tvPane=new JLabel("显示卡页面"); JLabel helpPane=new JLabel("帮助信息"); audioCB.addItemListener(new TabManager(au-dioPane)); nicCB.addItemListener(new TabManager(nicPane)); tvCB.addItemListener(new TabManager(tvPane)): config.addTab("系统",null,configPane,"Choose Installed Options”); config.addTab("声卡",null,audioPane,"Audio system configuration”); config.addTab("网卡",null,nicPane;"Netwot-king configuration"); config.addTab("显示卡",null,tvPane,"Video system configuration"); config.addTab("帮助",null,helpPane,"How Do I…"); getContentPane().add(config,BorderLayout.CENTER); } class TabManger implements ActionListener{ Component tab; public TabManager(Component tabToManage){ tab=tabToManage; } public void ItemStateChanged(ItemEvent ie){ int index=config.indexOfComponent(tab); if(index!=-l){ config.setEnabledAt(index,ie.getStateChange() = =ItemEvent.SELECTED); } this.repaint(); } } public static void main(String args[]){ java3 SC=new java3(); se.setVisible(true); } } |
查看答案 |
简答题 | 本题提示输入年份,然后判断该年份是否为闰年。 importjava.io.*; public class javal{ public static void main(String[]args){ InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); in=new BufferedReader(ir); int year=1900; System.out.print("请输入年份:"); try{ String s=in.readLine(); ; } (Exception e){ } if( ) System.OUt.println(year+"是闰年"); else System.out.println(year+"不是闰年"); } } |
查看答案 |
简答题 | 本题的功能是用流式布局管理器来管理窗口中的按钮。在执行程序时指定生成按钮的个数,并把这些按钮都放置在流式布局管理器的窗口中。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java2 { public static void main(String[]args) { if(args.length!=1) { System.out.println("请指定按钮的个数"); System.exit(0); } String buttonString=args[O]; int buttonNumber= ; ButtonFrame frame=new ButtonFrame(button- Number); frame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLoSE); frame.show(); } } class ButtonFrame extends JFrame { public ButtonFrame( ) { buttons=buttonNumber: setTitle("java2"); setSize(WIDTH,HEIGHT); JPanel buttonPanel=new JPanel(); for(int i=0;i<BUTTONS;I++) { JButton addButton=new JButton("add"+i): buttonPanel.add(addButton); Container contentPane=getContentPane(); contentPane.add(buttonPanel); } public static final int WIDTH=350; public static final int HEIGHT=200: private int buttons; } |
查看答案 |
简答题 | 本题的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在单击的地方会画一个小矩形,如果将鼠标置于小矩形上,则鼠标光标状态改为小十字,按下鼠标左键可拖曳,双击鼠标左键(或右键)时,小矩形消失。 import java.awt.*; import java.awt.event.*; import java.util.*; import java.awt.geom.*; import javax.swing.*; public class java3 { public static void main(String[]args) { MouseFrame frame=new MouseFrame(): frame.setDefaultCloseOperation(JFrame.EXIT_ oN_CLOSE); frame.show(); } } class MouseFrame extends JFrame { public MouseFrame() { setTitle("java3"); setSize(DEFAULT WIDTH, DEFAULT HEIGHT): MousePanel panel=new MousePanel(); Container contentPane=getContentPane(); contentPane.add(panel); } public static final int DEFAULT_WIDTH=300; public static final int DEFAULT_HEIGHT=200; } class MousePanel extends JPanel { public MousePanel() { squares=new ArrayList(); current=null: addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHan- dler()); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g; for(int i=0;i<SQUARES.SIZE();I++) g2.draw((Rectangle2D)squares.get(i)); } public void find(Point2D p) { for(int i=0;i<SQUARES.SIZE();I++) { Rectangle2D r=(Rectangle2D)squares.get(i); if(r.contains(p))return r; } return null: } public void add(Point2D p) { double x=P.getX(); double Y=P.getY(): current=new Rectangle2D.Double( x-SIDELENGTH/2. y-SlDELENGTH/2. S1DELENGTH, SIDELENGTH); squares.add(current); repaint(); } public void remove(Rectangle2D s) { if(S= =null)return: if(S= =current)current=null; squares.remove(s): repaint(); } private static final int SIDELENGTH=10: private ArrayList squares; private Rectangle2D current; private class MouseHandter extends MouseAction- Listener { public void mousePressed(MouseEvent event) { current=find(event.getPoint()); if(current= =null) add(event.getPoint()); } public void mouseClicked(MouseEvent event) { current=find(event.getPoint()); if(current!=null&&event.getClickCount() >=2) remove(current); } } private class MouseMotionHandler implements Mouse- MotionListener { public void mouseMoved(MouseEvent event) { if(find(event.getPoint)= =null) setCursor(Cursor.getDefaultCursor()); else setCursor(Cursor.getPredefinedCursor (Cursor.CROSSHAIR_CURSOR)); } public void mouseDragged(MouseEvent event) { if(current!=null) { int x=event.getX(): int Y=event.getY(); current.setFrame( x-SIDELENGTH/2, Y-SlDELENGTH/2. SIDELENGTH, SIDELENGTH); repaint(); } } } } |
查看答案 |
简答题 | 本题中定义了一个长度为20的整数数组,然后将1~20分别赋给数组元素,计算该数组中所有下标为奇数的元素的和。 public class javal{ public static void main(String args[]){ int sum; ; int arrayList[]=new int[20]; for(int i=0;i<=19;i++) arrayList[i]=i+1; int pos=0; while(pos<20){ if( ) sum=sum+arrayList[pos]; ; } System.out.println("sum="+sum); } } |
查看答案 |
简答题 | 本题中,用表格表现某个月的月历,其中标题是从Sunday到Saturday,表格中的各项是可以修改的。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class java2 ( public static void main(String[]args) { try{ UIManager.setLookAndFeel(UIManager.getSys- temLookAndFeelClassName()): } catch(Exception e)() JFrame frame=new CalendarTableFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ oN CLOSE); frame.show(); } } clasgCalendarTableFrame extends JFrame { private static final int WIDTH=500; private static final int HEIGHT=150: privat ecells= { {null,null,null,new Integer(1),new Integer (2),new Integer(3),new Integer(4)), {new Integer(5),new Integer(6),new Integer (7).new Integer(8),new Integer(9),new Integer (10),new Integer(11)), {new Integer(12),new Integer(13),new Integer (14),new Integer(15),new Integer(16),new Integer (17),new Integer(18)), {new Integer(19),new Integer(20),new Integer (21),new Integer(22),new Integer(23),new Integer (24),new Integer(25)), {new Integer(26),new Integer(27),new Integer (28),new Integer(29),new Integer(30),new Integer (31),null} }; private String[]columnNames={ "Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday" }; public CalendarTableFrame(){ setTitle("java2"); setSize(WIDTH,HEIGHT); JTable table=new ; getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); } } |
查看答案 |
简答题 | 本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java.awt.*; import java.awt.event.*; import javax swing.*; class MousePanel extends JPanel extends MouseMo- tionListener {public MousePanel() {addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent evt) {int X=evt.getX(); int Y=evt.getY(); current=find(x,y); if(current<0) add(x,y); } public void mouseClicked(MouseEvent evt) {int X=evt.getX(); int Y=evt.getY(); if(evt.getClickCount()>=2) {remove(current); } } }); addMouseMotionListener(this); } public void paintComponent(Graphics g) {super.paintComponent(); for(int i=0;i<NSQUARES;I++) draw(g,i); } public int find(int X,int y) (for(int i=0;i<NSQUARES;I++) if(squares[i].x-SQUARELENGTH/2<=x&& X<=squares[i].x+SQuARELENGTH/2 &&squares[i].Y-SQUARELENGTH/2<=Y &&y<=squares[i].Y+SQUARELENGTH/2) return i ; return-1 ; } public void draw(Graphics g,int i) {g.drawRect(squares[i].X-SQUARE- LENGTH/2。 squares[i].Y-SQUARELENGTH/2, SQUARELENGTH, SQUARELENGTH); } public void add(int X,int Y) {if(nsquares<MAXNSQUARES) {squares[nsquares]=new Point(x,y); current=nsquares ; nsquares++; repaint(); } } public void remove(int n) {if(n<0 ‖ n>=nsquares)return; Nsquares- -; squares[n]=squares[nsquares]; if(current= =n)current= -l; repaint(); } public void mouseMoved(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) {} private static final int SQUARELENGTH=10: private static final int MAXNSQUARES=100; private Point[]squares=new Point[MAX-NSQUARES]; private int nsquares=0; private int current=-l; } class MouseFrame extends JFramc {public MouseFrame() {setTitle("java3"); setSize(300,200); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane(); contentPane.add(MousePanel()); } } public class java3 {public static void main(String[]args) {JFrame frame=new MouseFrame(); frame.show(); } } |
查看答案 |
简答题 | 本题的功能是获得字符串“China”的长度和最后一个字符,并将这些信息打印出来。 public class javal( public static void main(String[]args){ ; str="China"; int n=0: ; char C; ; System.out.println("字符串中共有"+n+"个字 符,最后一个字符是:"+c); } } |
查看答案 |
简答题 | 本题中,窗口的背景色为系统桌面的颜色,在窗口中分别画空心和实心的黑色矩形、深灰色圆角矩形和浅灰色椭 圆形,并且画白色粗体的“欢迎学习Java!”字符串。 import java.awt.*; import javax.swin9.*; public class java2 { public static void main(String[]args) { FillFrame frame=new FillFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE); frame.show(); } } class FillFrame extends JFrame { public FillFrame() { setTitle("java2"): setSize(WIDTH,HEIGHT); FillPanel panel=new FillPanel(); panel.setBackground(SystemColor.desktop); Container contentPane=getContentPane(); contentPane.add(panel); } public static final int WIDTH=400; public static final int HEIGHT=250; } class FitlPanel extends JPanel { public void paintComponent(Graphics g) { ; g.setColor(new Color(10,10,10)); g.drawRect(10,10,100,30); g.setColor(new Color(100,100,lOO)); g.drawRoundRect(150,10,100,30,15,15); g.setColor(new Color(150,150,150)); g.drawOval(280,10,80,30); g.setColor(new Color(10,10,10)); g.fillRect(10,ll0,100,30); g.setColor(new Color(100,100,100)); g.fillRoundRect(150,110,100,30,15,15); g.setColor(new Color(150,150,150)); g.fillOval(280,110,80,30); g.setColor(Color.white); Font f=new Font("宋体”, ,20); g.setFont(f); g.drawstring("欢迎学习Java!",l50,200); } } |
查看答案 |
简答题 | 本题的功能是监听鼠标的拖曳操作。窗口中有一个列表框,列表框中列出了当前目录的所有文件,鼠标选中一个或多个文件后拖曳出窗口,此操作的功能是将拖曳的文件复制一份在拖曳的目的目录下。 import java.awt.*; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class java3 { public static void main(String[]args) { JFrame frame=new DragSourceFrame(); frame.setDefauhCloseOperation(JFrame.EXIT_ 0N_CLoSE); frame.show(); } } class DragSoureeFrame extends JFrame { public DragSourceFrame() { setTitle("java3"); setSize(WlDTH,HElGHT); Container contentPane=getContentPane(); File f=new File(".").getabsoluteFile(); File[]files=f.listFiles(); model=new DefaultListModel(); for(int i=0;i<FILES.LENGTH();I++< u>) try { model.addElement(files[i].getCanonicalFile()); } catch(IOException exception) { JOptionPane.showMessageDialog(this,exeep- tion); } fileList=new JList(model); contentPane.add(new JScrollPane(fileList), BorderLayout.CENTER); contentPane.add(new JLabel("从列表中拖曳出文 件"), BorderLayout.NoRTH); DragSource dragSource=DragSource.getDefauh- DragSource(); dragSource.createDefaultDragGestureRecognizer (fileList, DnDConstants. ACTION_COPY_0R_ MOVE,new DragGestureListener() { public void dragGestureRecognized( DragGestureEvent event) { draggedValues=fileList.getSelectedValues(); Transferable transferable =new FiteListTransferable(draggedValues); evenr.startDrag(null,transferable, new FileListDragSourceListener()); } }); } private class FileListDragSourceListener imple-ments DragSourceAdapter { public void dragDropEnd(DragSourceDropEvent e- vent) { if(event.getDropSuccess()) { int action=event.getDropAction(); if(action= =DnDConstants.ACTl0N MOVE) { for(int i=0;i<DRAGGEDVALUES.LENGTH;I++) model.removeElement(draggedValues[i]); } } } } private JList fileList; private DefauhListModel model; private Object[]draggedValues; private static final int WIDTH=300; private static final int HEIGHT=200; } class FileListTransferable implements Transferable { public FileListTransferable(Object[]files) { fileList=new ArrayList(Arrays.asList(files)); } public DataFlavor[]getTransferDataFlavors() { return flavors; public boolean isDataFlavorSupported(DataFlavor flavor) { return Arrays. asList(flavors), contains(flavor) ; } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException if(flavor, equals(DataFlavor, javaFileListFlavor) ) return fileList; else if(flavor, equals(DataFlavor, stringFlavor)) return fileList, toString() ; else throw new UnsupportedFlavorException(flavor) ; } private static DataFlavor[] flavors = { DataFlavor. j avaFileListFlavor, DataFlavor. stringFlavor }; private java. util. List fileList; |
查看答案 |
简答题 | 本题的功能是计算二维数组各个元素的和。程序中定义了二维数组arr,arr有3行4列共l2个元素,程序中采用for循环语句的嵌套来计算数组中各个元素的和,并将结果保存在sum变量中,最后打印输出结果。 public class javal{ public static void main(String[] args){ int arr[][]={{l,2,3,4},{5,6,7,8},{9,10,11,12}}; int sum=0; int i=0,j=0; for(i=0; ) for( ) ; System.OUt.println("sum="+sum); } } |
查看答案 |
简答题 | 下面的程序中,有两个文本框,Input和Output,用户在Input文本框中输入字符串后回车,字符串会在Output文本框中出现。 import java.awt.*; import java.awt.event.*; public class java2 extends Frame{ public static void main(String args[]){ java2 f=new java2("java2"); Panel pan=new Panel(); f.init(); } pubhc java2(String str){ super(str); } public void init(){ addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(O); } }}; setSize(200,200); setLayout(new FlowlLayout()); final TextField tfl=new TextField(20): final TextField tf2=new TextField(20); final Label hi—new Label("Input:"); final Label la2一new Label("Output:"); tfl.addActionListener( { public void actionPerformed(ActionEvent e){ tf2.setTex( ); } }); add(la1); add(tfl); add(la2); add(tf2); setVisible(true); } } |
查看答案 |
简答题 | 本题的功能是对图形进行操作,包括旋转、移位、放大和剪切。窗口中有4个单选按钮和一个图形面板,图形面板中有一个矩形和该矩形移位操作后的图形,任选其中一个单选按钮,则图形面板中将显示该操作后的图形。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class java3 { public static void main(String[]args) { JFrame frame=new TransformTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ 0N_CLoSE); frame.show(); } } class TransformTest extends JFrame { public TransformTestFrame() { setTitle("java3"); setSize(WIDTH,HEIGHT); Container contentPane=getContentPane(); canvas=new TransformPanel(); contentPane.add(canvas,BorderLayout.CEN- TER); JPanel buttonPanel=new JPanel(): ButtonGroup group=new ButtonGroup(); JRadioButton rotateButton =new JRadioButton("旋转",true); buttonPanel.add(rotateButton); group.add(rotateButton); rotateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { canvas.setRotate(); } }); JRadioButton trans|ateButton =new JRadioButton("移位",false); buttonPanel.add(translateButton); group.add(translateButton); translateButton.addActionListener(new ActionLiatener() public void actionPerformed(ActionEvent event) { canvas.setTranslate(); } }); JRadioButton scaleButton =new JRadioButton("放大",false); buttonPanel.add(scaleButton): group.add(scaleButton); scaleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { canvas.setScale(); } }); JRadioButton shearButton =new JRadioButton("剪切",false); buttonPanel.add(shearButton): group.add(shearButton); shearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { canvas.setShear(); } }); contentPane.add(buttonPanel,BorderLayout. NORTH): } private TransformPanel canvas; private static final int WIDTH=300; private static final int HEIGHT=300; } class TransformPanel extend JPanel { public TransformPanel() { square=new Rectangle2D.Double(-50,-50, 100,100); t = new AffineTransform(); setRotate(); } public void paintComponent() { super.paintComponent(g); Graphics2D g2=(Graphies2D)g; g2.translate(getWidth()/2,getHeight()/2): g2.setPaint(Color.gray); g2.draw(square); g2.transform(t); g2.setPaint(Color.black); g2.draw(square); } public void setRotate() { t.setToRotation(Math.toRadians(30)); repaint(); } public void setTranslate() { t.setToTranslation(20,1 5); repaint(); } public void setScale() { t.setToScale(2.0,1.5): repaint(); } public void setShear() { t.setToShear(-0.2,O); repaint(); } private Rectangle2D square; private AffineTransform t; } |
查看答案 |