Reputation: 57
I am developing a game using java swing. I want to set angry birds images for my hadaf1 and h1 and the ball in my code which is below (whole code). As you can see in the code I am using Rectangle class to draw hadaf1 and h1 rectangles, and because I use their names to change their x and y and move them, I am not using g.fillRect, becuase I need the rectangles to have names. I search a lot to find out a solution to assign an image to my rectangles but the only thing i found was using setpaint() which should be used with g.fillRect. Can you give a solution to assing images to hadaf1 and h1 Rectangles please?
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);
}
}
}
Upvotes: 0
Views: 1000
Reputation: 51445
I copied your code into my Eclipse. It runs, although the msg definition is missing. I just commented it out.
It would take me hours to straighten out your code. As MadProgrammer said, you're doing too much in your paintComponent method. The paintComponent method should paint. Period. Full stop. Nothing else.
To answer the question in your title, you create a Java object that contains the image and the rectangle.
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;
}
}
Upvotes: 3