3D graphics movement

3D graphics movement

  • In fact, the more you know about java, the more you find that many special effects that we think are amazing can be run through our code, such as the fractal in the previous article, we use the code to run the formula, and then draw the magical fractal pattern , But there is a good saying, as long as it is a symmetrical figure, any painting can look good.

Still old rules, the whole interface comes out.

public class AA {

	public void showUI() {
		//创建窗体
		JFrame drawFrame = new JFrame("画板");
		drawFrame.setSize(1000,600);
		drawFrame.setLocationRelativeTo(null);
		drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		//设置窗体可见
        drawFrame.setVisible(true);
        }
	public static void main(String[] args) {  
  	  AA ui = new AA();
  	  ui.showUI();
    }
}

Then write our method

public class DrawListene implements MouseListener{
	    int startx,starty;
	    int x;
		int y;
		Graphics g;
		public void mousePressed(MouseEvent e) {
			//获取坐标
			startx = e.getX();
			starty = e.getY();
		}
		public void mouseReleased(MouseEvent e) { 
			int r=100;
			//颜色不断加深
			for(int i=0;i<255;i++) {
			Color c = new Color(i,255-i,255-i);
			g.setColor(new Color(i,i,i));
			//画圆的位置不断变化
		    g.fillOval(startx+i/3, starty+i/3, 255-i, 255-i);
			}
			//放慢动作慢慢看
            try {
				Thread.sleep(50);
			} catch (InterruptedException a) {
				a.printStackTrace();
			}
		System.out.println(startx+","+starty);
	}
		public void mouseClicked(MouseEvent e) {}
	    public void mouseEntered(MouseEvent e) {}
	    public void mouseExited(MouseEvent e) {}
	}

Add a listener to the interface

//创建监听器
        DrawListene drawL = new DrawListene();
        //获取窗体的画布
        drawL.g= drawFrame.getGraphics();
        
        //添加监听器
        drawFrame.addMouseListener(drawL);

See the effect
Insert picture description here
and then let it move

Insert picture description here
Here are the final results, in fact, drew a circle around the ball thirty times, in our eyes is like looking at moving up, like
after the color and then adjust
the method of adjusting the color tone just like in setColor there
Insert picture description here
understand this Method, we can draw other things, such as drawing rectangles and lines, using your imagination and creativity to create your favorite patterns ~
over ~

Published 13 original articles · won 1 · views 303

Guess you like

Origin blog.csdn.net/Alagagaga/article/details/103606737