パッケージテスト; importjava.awt。*; importjava.awt.event。*; java.awt.geom.Ellipse2Dをインポートします。 import java.awt.image.BufferedImage; インポートjavax.swing。*; パブリッククラスtest_bmpはJPanelを拡張し、MouseListener、MouseMotionListener、ActionListenerを実装します {{ 静的BufferedImage画像; カラーカラー; ポイントstart=new Point(); ポイント終了=新しいポイント(); JButton elipse = new JButton( "Elipse"); JButtonrectangle = new JButton( "Rectangle"); JButton line = new JButton( "Line"); 選択された文字列。 public test_bmp() {{ color = Color.black; setBorder(BorderFactory.createLineBorder(Color.black)); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) {{ //super.paintComponent(g); g.drawImage(image、0、0、this); Graphics2D g2 =(Graphics2D)g; g2.setPaint(Color.black); if(selected == "elipse") {{ g2.drawOval(start.x、start.y、(end.x-start.x)、(end.y-start.y)); System.out.println( "開始:" + start.x + "、" + start.y); System.out.println( "End:" + end.x + "、" + end.y); } if(selected == "line") g2.drawLine(start.x、start.y、end.x、end.y); } //バッファリングされた画像に描画します public void draw() {{ Graphics2D g2 = image.createGraphics(); g2.setPaint(color); System.out.println( "draw"); if(selected == "line") g2.drawLine(start.x、start.y、end.x、end.y); if(selected == "elipse") {{ g2.drawOval(start.x、start.y、(end.x-start.x)、(end.y-start.y)); System.out.println( "開始:" + start.x + "、" + start.y); System.out.println( "End:" + end.x + "、" + end.y); } repaint(); g2.dispose(); } public JPanel addButtons() {{ JPanel buttonpanel = new JPanel(); buttonpanel.setBackground(color.lightGray); buttonpanel.setLayout(new BoxLayout(buttonpanel、BoxLayout.Y_AXIS)); elipse.addActionListener(this); rectangle.addActionListener(this); line.addActionListener(this); buttonpanel.add(elipse); buttonpanel.add(Box.createRigidArea(new Dimension(15,15))); buttonpanel.add(長方形); buttonpanel.add(Box.createRigidArea(new Dimension(15,15))); buttonpanel.add(line); ボタンパネルを返す; } public static void main(String args []) {{ test_bmp application = new test_bmp(); //メインウィンドウ JFrame frame = new JFrame( "Whiteboard"); frame.setLayout(new BorderLayout()); frame.add(application.addButtons()、BorderLayout.WEST); frame.add(application); //ウィンドウのサイズ frame.setSize(600,400); frame.setLocation(0,0); frame.setVisible(true); int w = frame.getWidth(); int h = frame.getHeight(); image = new BufferedImage(w、h、BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setPaint(Color.white); g2.fillRect(0,0、w、h); g2.dispose(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @オーバーライド public void mouseClicked(MouseEvent arg0){ //TODO自動生成されたメソッドスタブ } @オーバーライド public void mouseEntered(MouseEvent arg0){ //TODO自動生成されたメソッドスタブ } @オーバーライド public void mouseExited(MouseEvent arg0){ //TODO自動生成されたメソッドスタブ } @オーバーライド public void mousePressed(MouseEvent event) {{ start = event.getPoint(); } @オーバーライド public void mouseReleased(MouseEvent event) {{ end = event.getPoint(); 描く(); } @オーバーライド public void mouseDragged(MouseEvent e) {{ end = e.getPoint(); repaint(); } @オーバーライド public void mouseMoved(MouseEvent arg0){ //TODO自動生成されたメソッドスタブ } @オーバーライド public void actionPerformed(ActionEvent e) {{ if(e.getSource()== elipse) selected = "elipse"; if(e.getSource()== line) selected = "line"; 描く(); } }
ペイントアプリケーションを作成する必要があります。マウスを左から右にドラッグして楕円を描くと、何も表示されません。なんで?ここで他の関数を使用する必要がありますか?