本文目錄一覽:
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();
}
}
}
}
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程序實現圖片的移動(最好有例子)
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();
}
}
}
}
}
以上
原創文章,作者:SYNQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/148301.html