网站建设合同书 虚拟,怎么优化网站,成都自助建站软件,网页怎么做成网站我一直试图找到一个解决方法#xff0c;但没有找到一个#xff0c;特别是对于getGraphics()方法#xff1a;如何将图形添加到面板#xff1f;你记得需要绘制的变量是什么#xff0c;并在paintComponent()中使用它。 例如#xff0c;您在其他问题中尝试做的事情如下#…我一直试图找到一个解决方法但没有找到一个特别是对于getGraphics()方法如何将图形添加到面板你记得需要绘制的变量是什么并在paintComponent()中使用它。 例如您在其他问题中尝试做的事情如下import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PaintRectangle extends JPanel {private Point mouseLocation;public PaintRectangle() {setPreferredSize(new Dimension(500, 500));MouseAdapter listener new MouseAdapter() {Overridepublic void mousePressed(MouseEvent e) {updateMouseRectangle(e);}private void updateMouseRectangle(MouseEvent e) {mouseLocation e.getPoint();repaint();}Overridepublic void mouseDragged(MouseEvent e) {updateMouseRectangle(e);}Overridepublic void mouseReleased(MouseEvent e) {mouseLocation null;repaint();}};addMouseListener(listener);addMouseMotionListener(listener);}private Rectangle getRectangle() {if(mouseLocation ! null) {return new Rectangle(mouseLocation.x - 5, mouseLocation.y - 5, 10, 10);}else {return null;}}Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g);Rectangle rectangle getRectangle();if(rectangle ! null) {Graphics2D gg (Graphics2D) g;gg.setColor(Color.BLUE);gg.fill(rectangle);gg.setColor(Color.BLACK);gg.draw(rectangle);}}public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {Overridepublic void run() {JFrame frame new JFrame(Test);frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);frame.getContentPane().add(new PaintRectangle());frame.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}}