我正在使用Java swing开发游戏。我想为我的hadaf1和h1以及下面的代码(整个代码)设置球的愤怒的小鸟图像。
如您在代码中所看到的,我正在使用Rectangle类绘制hadaf1和h1矩形,并且由于我使用它们的名称来更改其x和y并移动它们,所以我不使用g.fillRect,因为我需要矩形具有名称。我进行了大量搜索,以找到将图像分配给我的矩形的解决方案,但是我发现的唯一发现是使用了setpaint(),它应该与g.fillRect一起使用。您能否提供一种将图像添加到hadaf1和h1矩形的解决方案?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
public class ProjectileShooterTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,600);
final ProjectileShooter projectileShooter =
new ProjectileShooter();
ProjectileShooterPanel projectileShooterPanel =
new ProjectileShooterPanel(projectileShooter);
projectileShooter.setPaintingComponent(projectileShooterPanel);
JPanel controlPanel = new JPanel(new GridLayout(1,0));
controlPanel.add(new JLabel("Angle"));
final JSlider angleSlider = new JSlider(0, 90, 45);
controlPanel.add(angleSlider);
controlPanel.add(new JLabel("Power"));
final JSlider powerSlider = new JSlider(0, 100, 50);
controlPanel.add(powerSlider);
JButton shootButton = new JButton("Shoot");
shootButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int angleDeg = angleSlider.getValue();
int power = powerSlider.getValue();
projectileShooter.setAngle(Math.toRadians(angleDeg));
projectileShooter.setPower(power);
projectileShooter.shoot();
}
});
controlPanel.add(shootButton);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(controlPanel, BorderLayout.NORTH);
f.getContentPane().add(projectileShooterPanel, BorderLayout.CENTER);
f.setVisible(true);
}
}
class ProjectileShooter
{
private double angleRad = Math.toRadians(45);
private double power = 50;
private Projectile projectile;
private JComponent paintingComponent;
void setPaintingComponent(JComponent paintingComponent)
{
this.paintingComponent = paintingComponent;
}
void setAngle(double angleRad)
{
this.angleRad = angleRad;
}
void setPower(double power)
{
this.power = power;
}
int i=0;
void shoot()
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
if(i<10)
{
i=i+1;
executeShot();
}
if(i==10)
{
msg.infoBox("you lost","target score=20");
System.exit(0);
}
}
});
t.setDaemon(true);
t.start();
}
private void executeShot()
{
if (projectile != null)
{
return;
}
projectile = new Projectile();
Point2D velocity =
AffineTransform.getRotateInstance(angleRad).
transform(new Point2D.Double(1,0), null);
velocity.setLocation(
velocity.getX() * power * 0.5,
velocity.getY() * power * 0.5);
projectile.setVelocity(velocity);
long prevTime = System.nanoTime();
while (projectile.getPosition().getY() >= 0)
{
long currentTime = System.nanoTime();
double dt = 3 * (currentTime - prevTime) / 1e8;
projectile.performTimeStep(dt);
prevTime = currentTime;
paintingComponent.repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
return;
}
}
projectile = null;
paintingComponent.repaint();
}
Projectile getProjectile()
{
return projectile;
}
}
class Projectile
{
private final Point2D ACCELERATION = new Point2D.Double(0, -9.81 * 0.1);
private final Point2D position = new Point2D.Double();
private final Point2D velocity = new Point2D.Double();
public Point2D getPosition()
{
return new Point2D.Double(position.getX(), position.getY());
}
public void setPosition(Point2D point)
{
position.setLocation(point);
}
public void setVelocity(Point2D point)
{
velocity.setLocation(point);
}
void performTimeStep(double dt)
{
scaleAddAssign(velocity, dt, ACCELERATION);
scaleAddAssign(position, dt, velocity);
System.out.println("Now at "+position+" with "+velocity);
}
private static void scaleAddAssign(
Point2D result, double factor, Point2D addend)
{
double x = result.getX() + factor * addend.getX();
double y = result.getY() + factor * addend.getY();
result.setLocation(x, y);
}
}
class ProjectileShooterPanel extends JPanel
{
boolean check1 = false;
boolean check2=false;
int x3=0;
int x1=0; int x2=420;
int score=0;
boolean hit1=false;
boolean hit2=false;
private TexturePaint paint;
private final ProjectileShooter projectileShooter;
public void TexturePanel(BufferedImage bi) {
this.paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
}
public ProjectileShooterPanel(ProjectileShooter projectileShooter)
{
this.projectileShooter = projectileShooter;
}
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
Projectile projectile = projectileShooter.getProjectile();
int x,y;
Rectangle hadaf1= new Rectangle(x3+400,450,50,50);
g.draw(hadaf1);
Rectangle hadaf2= new Rectangle(400+x1,x2,30,30);
g.draw(hadaf2);
Rectangle h1= new Rectangle(350,480,20,20);
g.draw(h1);
if (projectile != null)
{
g.setColor(Color.RED);
Point2D position = projectile.getPosition();
x = (int)position.getX();
y = getHeight() - (int)position.getY();
g.fillOval(x-01, y-10, 20, 20);
if((projectile.getPosition().getX()>h1.getX() && projectile.getPosition().getX()<h1.getX()+30) && (projectile.getPosition().getY()>0 && projectile.getPosition().getY()<25) )
{ System.out.println("barkhooooooord");
System.out.println(projectile.getPosition().getY());
if(hit1==false)
{
hit1=true;
score=score+10;
}
}
if((projectile.getPosition().getX()>hadaf1.getX() && projectile.getPosition().getX()<hadaf1.getX()+50) && (projectile.getPosition().getY()>hadaf1.getY()-500 && projectile.getPosition().getY()<hadaf1.getY()-450))
{
System.out.println("paeen collision at");
System.out.println(projectile.getPosition().getX()+x3);
System.out.println(projectile.getPosition().getY());
check1=true;
}
if(projectile.getPosition().getX()>hadaf2.getX() && projectile.getPosition().getX()<hadaf2.getX()+30 )
{
if(x2==420)
{
if(projectile.getPosition().getY()>hadaf1.getY()-450 && projectile.getPosition().getY()<100)
{
System.out.println("bala collision at"); check2=true;
System.out.println(projectile.getPosition().getX()+x1);
System.out.println(projectile.getPosition().getY());
}
}
}
}
if(check1==true){
x3=x3+1;
check2=true;
check1=false;
}
if(check2==true)
{
x1=x1+1;
check2=false;
}
if(hadaf1.getX()!=hadaf2.getX())
{
if(hadaf1.getX()-hadaf2.getX()<-50)
{
x2=470;
if(hit2==false)
{
hit2=true;
score=score+10;
}
}
}
if(hit1==hit2==true && score==20)
{
msg.infoBox("you won","score=20");
System.exit(0);
}
}
}
最佳答案
我将您的代码复制到了Eclipse中。它运行,尽管缺少味精定义。我只是注释掉了。
我需要花费几个小时来整理您的代码。正如MadProgrammer所说,您在paintComponent方法中做的太多。 paintComponent方法应该绘制。期。句号没有其他的。
要回答标题中的问题,您将创建一个包含图像和矩形的Java对象。
package com.ggl.testing;
import java.awt.Image;
import java.awt.Rectangle;
public class ImageRectangle {
private final Image image;
private Rectangle rectangle;
public ImageRectangle(Image image, Rectangle rectangle) {
this.image = image;
this.rectangle = rectangle;
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
public Image getImage() {
return image;
}
}