首页 > 编程知识 正文

java图形的移动,函数图形移动

时间:2023-12-26 02:46:16 阅读:322347 作者:QHHF

本文目录一览:

java中如何控制图形移动

图像移动效果是你第一眼看到他在这,第二眼看到他在那。

所以你要做两件事,,

1,不断绘图

2,改变要绘制的对象的位置

另一种方式,,

绘制一下,改变一下位置,绘制一下,改变,,,

相关类,,

窗体JFRAME

绘制面板PANEL

重写绘制面板的PAINT方法,用一个线程以一定频率不断调用重绘方法,

java如何实现图片拖动,放大缩小,旋转。

这个只是实现了移动,你参考以下吧 !

public class MoveImage {

static int x,y;

private static int num=0;

private static Icon icon=null;

public static void main(String[] args) throws Exception{

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().setLayout(null);//这个要设置成 null

//图片

icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一张图片

JLabel l = new JLabel(icon); //创建具有指定图像的 JLabel 实例。

l.setSize(icon.getIconWidth(),icon.getIconHeight());//设置面板的宽度和高度

l.setBorder(BorderFactory.createLineBorder(Color.red));//给图片加上红色外框

f.getContentPane().add(l);

f.setSize(900,700);

f.setVisible(true);

l.addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

x=e.getX();

y=e.getY();

}

});

l.addMouseMotionListener(new MouseMotionListener(){

public void mouseDragged(MouseEvent e) {

JLabel l = (JLabel)e.getSource();

l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);

}

public void mouseMoved(MouseEvent e) {}

});

}

在Java怎么让图片水平移动?

  java让图片水平移动,就是不断监听图片的位置,示例如下:

//给个实例

 import java.awt.Color; 

import java.awt.event.MouseAdapter; 

import java.awt.event.MouseEvent; 

import java.awt.event.MouseMotionListener; 

import java.net.URL;

 import javax.swing.BorderFactory; 

import javax.swing.Icon; 

import javax.swing.ImageIcon; 

import javax.swing.JFrame; 

import javax.swing.JLabel;

 public class ImageMove { 

 static int x,y; 

 public static void main(String[] args) throws Exception{ 

 JFrame f = new JFrame(); 

 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

 f.getContentPane().setLayout(null);//这个要设置成 null 

 //图标 

 Icon icon = new ImageIcon(new URL("")); 

// Icon icon = new ImageIcon("c:/logo-zhidao.gif");//本地图片文件

 JLabel l = new JLabel(icon);

 l.setSize(icon.getIconWidth(),icon.getIconHeight()); l.setBorder(BorderFactory.createLineBorder(Color.red)); 

 f.getContentPane().add(l); f.setVisible(true); 

 l.addMouseListener(

new MouseAdapter(){

 public void mousePressed(MouseEvent e){ x=e.getX();y=e.getY(); } });

 l.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { JLabel l = (JLabel)e.getSource();

 l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);

 }

 public void mouseMoved(MouseEvent e) {} }); } }

怎么编写java程序实现图片的移动(最好有例子)

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class DrawTest extends JFrame {

private int x = 50;

private int y = 50;

private Image offScreenImage = null;

@Override

public void paint(Graphics g) {

Color c = g.getColor();

g.setColor(Color.BLACK);

g.fillOval(x, y, 30, 30);

g.setColor(c);

}

public void update(Graphics g) {

if (offScreenImage == null) {

offScreenImage = this.createImage(500, 500);

}

Graphics gOffScreen = offScreenImage.getGraphics();

Color c = gOffScreen.getColor();

gOffScreen.setColor(Color.GREEN);

gOffScreen.fillRect(0, 0, 500, 500);

gOffScreen.setColor(c);

paint(gOffScreen);

g.drawImage(offScreenImage, 0, 0, null);

}

public static void main(String[] args) {

DrawTest d = new DrawTest();

}

public DrawTest() {

init();

addKeyListener(new KeyAdapter() {

public void keyPressed(final KeyEvent e) {

int code = e.getKeyCode();

switch (code) {

case KeyEvent.VK_UP:

y -= 5;

break;

case KeyEvent.VK_RIGHT:

x += 5;

break;

case KeyEvent.VK_DOWN:

y += 5;

break;

case KeyEvent.VK_LEFT:

x -= 5;

break;

}

}

});

}

public void init() {

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

this.setBackground(Color.GREEN);

this.setResizable(false);

this.setBounds(140, 140, 500, 500);

this.setVisible(true);

MyThread mt = new MyThread();

new Thread(mt).start();

}

class MyThread implements Runnable {

public void run() {

while (true) {

repaint();

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

以上

java 画了一个圆,怎么让它上下左右移动啊?

移动圆,改变它的圆心即可,可以通过给圆心设置一个运动轨迹函数实现,实例代码为;

public class joinDemo1 extends JFrame

{

 //继承    

 private int x=100, y=100, r=100; 

 

 //初始值   

 public joinDemo1() 

 {        

  super("小图形");       

  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

        

  this.setSize(800, 600);        

  this.setVisible(true); 

  Thread thread=new Thread(new Graphicss());

  thread.start();

}    

 public void paint(Graphics g)

 {        

  super.paint(g);        

  g.fillOval(x, y, r, r);    

 }    

 public static void main(String[] args)

 {        

  new joinDemo1();    

 }     

 

 class Graphicss implements Runnable{

  @Override

  public void run() {

   // TODO Auto-generated method stub

for (int j = 0; j = 240; j++) {

      revalidate();

  // System.out.println(j);                

                try {

                    Thread.sleep(1000);// 当前线程休眠0.01秒

                    

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

   

                y=y+j;

     repaint();           

                

}

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。