「java移动图形」java图片移动

博主:adminadmin 2023-03-18 03:55:07 546

本篇文章给大家谈谈java移动图形,以及java图片移动对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求Java 实现绘制图形并移动代码

代码如下:import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Graphics;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class Zfx extends MIDlet { private Display display; public Zfx() { display=Display.getDisplay(this); Zfxc qs=new Zfxc(); display.setCurrent(qs); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { }}class Zfxc extends Canvas implements Runnable{ private int x,y,x1,y1,i; private boolean flag; Zfxc(){ init(); } private void init(){ Thread thread=new Thread(this); thread.start(); } protected void paint(Graphics g) { g.setColor(255,255,255); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(0); g.fillRect(x, y, 10, 10); } private void logic(){ if(x=this.getWidth()-10){ x1=2; } if(x1==1){ x+=3; }else if(x1==2){ x-=3; } if(y=this.getHeight()-10){ y1=2; } if(y1==1){ y+=3; }else if(y1==2){ y-=3; } } protected void keyPressed(int keyCode) { if(keyCode==-5){ if(i==0){ flag=true; i=1; }else if(i==1){ flag=false; i=0; } } } public void run() { while(true){ if(flag){ logic(); } repaint(); try { Thread.sleep(80); } catch (InterruptedException e) { e.printStackTrace(); } } }}求Java 实现绘制图形并移动代码

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程序实现图片的移动(最好有例子)

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怎么让图片水平移动?

  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 中移动图片,并且在按键盘的“回车”时,切换到另外一张图片!!请在我下面的代码改进!!!谢谢!!

你好,按照你的要求代码如下,修改了三处

简单说明一下,就是加了一个标识boolean,用true/false来表示显示第一张/第二张图片

import java.awt.*;

import java.awt.event.*;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.*;

public class zhandou extends JFrame implements KeyListener {

Image roleImage, Image1;

int x, y;

public zhandou() {

super("MOVE");

Container c = getContentPane();

setSize(320, 240);

setVisible(true);

loadImage();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// setFocusable(true);

addKeyListener(this);

}

boolean key = true;// 加这样一个标识,true是第一张图片,false为第二张图片

public void paint(Graphics g) {

super.paint(g);

// drawRole(g);

if (key) {// 根据标识判断需要显示的图片

g.drawImage(roleImage, x, y, this);

} else {

g.drawImage(Image1, x, y, this);

}

}

public void loadImage() {

ImageIcon icon = new ImageIcon("tupian/草地.jpg");

roleImage = icon.getImage();

ImageIcon ic = new ImageIcon("tupian/right.gif");

Image1 = ic.getImage();

}

class Thread1 extends Thread {

}

/*

* private void drawRole(Graphics g) { g.drawImage(roleImage,100,100,this);

* }

*/

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_UP)

y = y - 5;

else if (e.getKeyCode() == KeyEvent.VK_DOWN)

y = y + 5;

else if (e.getKeyCode() == KeyEvent.VK_RIGHT)

x = x + 5;

else if (e.getKeyCode() == KeyEvent.VK_LEFT)

x = x - 5;

else if (e.getKeyCode() == KeyEvent.VK_ENTER)

key = !key;// 切换标识状态

repaint();

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

public static void main(String args[]) {

new zhandou();

}

}

关于java移动图形和java图片移动的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。