gpt4 book ai didi

java - 当到达某个点时滚动对象

转载 作者:行者123 更新时间:2023-12-01 13:50:02 27 4
gpt4 key购买 nike

我希望让对象在达到 y pos = 170 时向上滚动。然后当它达到 51 时,它会再次向下滚动。这是我的代码..

import java.awt.*;
import java.applet.*;

public class ani1 extends Applet implements Runnable{

Thread run01;
int spacex = 51,spacey = 91;
int score = 0;

public void start() {
if (run01 == null){
run01 = new Thread(this);
run01.start();
}
}
public void stop() {
if(run01 != null){
run01 = null;
}
}
public void run() {
while(true){
repaint();
try {
Thread.sleep(30);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g){
g.setColor(Color.gray);
g.fillRoundRect(35, 70, 250, 300, 10, 250);
g.fillRect(230,20,50,50);
g.setColor(Color.white);
g.fillRoundRect(45,85,230,100,10,250);
g.setColor(Color.black);
g.drawRect(50, 90, 150, 90);
g.drawString("SPACE-X",215,100);
g.drawString("Level: 1",210,120);
g.drawString("Score : "+ score, 210,135);
g.drawString("Life : - - -",210,150);
g.fillRect(spacex, spacey, 10, 10);
spacey++;
if(spacey >170){
spacey--;
}

}

}

最佳答案

基本上,您应该尝试对要移动的对象进行建模,即使这只是对进行建模,然后您可以使用它来渲染某些任意形状。

这意味着您不会在绘制过程中做出逻辑决策。绘制过程应该只专注于绘制模型。

例如...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Spacy {

public static void main(String[] args) {
new Spacy();
}

public Spacy() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private List<Movable> movables;

public TestPane() {
movables = new ArrayList<>(25);
movables.add(new SpaceObject());

Timer timer;
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Movable m : movables) {
m.move(getSize());
}
repaint();
}
});
timer.start();

}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Paintable p : movables) {
p.paint(g);
}
g2d.dispose();
}
}

public interface Paintable {

public void paint(Graphics g);

}

public interface Movable extends Paintable {

public void setLocation(Point p);
public Point getLocation();
public void move(Dimension size);

}

public class SpaceObject implements Movable {

private Point p;

public SpaceObject() {
setLocation(new Point(150, 50));
}

@Override
public void setLocation(Point p) {
this.p = p;
}

@Override
public Point getLocation() {
return p;
}

@Override
public void paint(Graphics g) {

g.setColor(Color.RED);
Point p = getLocation();
int radius = 10;
g.fillOval(p.x - (radius / 2), p.y - (radius / 2), radius, radius);

}

@Override
public void move(Dimension size) {
Point p = getLocation();
int delatX = 0;
int delatY = 0;
int gap = size.height / 4;
if (p.y == gap) {
delatX = 2;
} else if (p.y == size.height - gap) {
delatX = -2;
}
gap = size.width / 4;
if (p.x == gap) {
delatY = -2;
} else if (p.x == size.width - gap) {
delatY = 2;
}
p.x += delatX;
p.y += delatY;

if (p.x < (size.width / 4)) {
p.x = size.width / 4;
} else if (p.x > (size.width - (size.width / 4))) {
p.x = size.width - (size.width / 4);
}
if (p.y < (size.height / 4)) {
p.y = size.height / 4;
} else if (p.y > (size.height - (size.height / 4))) {
p.y = size.height - (size.height / 4);
}

setLocation(p);
}
}
}

您的代码有两个基本问题。

  1. 您正在尝试决定如何在绘制方法中绘制输出。一般来说,这是不可取的。当你养成其他坏习惯时
  2. 在绘制之前,您不会更新Graphics上下文。基本上发生的情况是,您获得了与上一个绘制周期相同的Graphics上下文,这意味着当您到达末尾并想要开始向上滚动时,您之前绘制的内容仍然是在那里。

我建议这样做;

  1. 您避免使用 Applet 并使用 JApplet,事实上,我会完全避免使用 applet,而只是使用 JFrame 作为基本容器,它们问题较少。
  2. 基于 JPanel 之类的内容创建自定义组件,并重写其 paintComponent 方法并在其中执行自定义绘制。
  3. 确保您首先调用 super.paintComponent,因为这将为您准备 Graphics 上下文
  4. 就我个人而言,在这种情况下我会避免使用Thread,它会带来额外的复杂性,相反,我会使用javax.swing.Timer,但这只是我。

您可能想看看Performing Custom Painting了解更多详情

关于java - 当到达某个点时滚动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com