0

Hi there, I am experimenting with game design and have met an obstacle. I am trying to paint the background of this simple game I have coded but cannot seem to find out how! any help would be much appreciated!

Attachments
/* A game that is Saucer-like
 * Phillip Wells
 */

//to do - make more balls when 100 points met

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;

public class Saucer extends JFrame {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    
    private PaintSurface canvas;
    
    public void init() {
        this.setSize(WIDTH, HEIGHT);
        this.setTitle("Saucer");
        canvas = new PaintSurface();
        this.getContentPane().add(canvas, BorderLayout.CENTER);
        ScheduledThreadPoolExecutor executor =
                new ScheduledThreadPoolExecutor(3);
        executor.scheduleAtFixedRate(new AnimationThread(this),
                0L, 20L, TimeUnit.MILLISECONDS);
        setVisible(true);
    }
    
    
    
    class AnimationThread implements Runnable {
        JFrame c;
        
        public AnimationThread(JFrame c) {
            this.c = c;
            
        }
        
        public void run() {
            c.repaint();
        }
    }
    
    class PaintSurface extends JComponent {
        int saucer_x = 0; // start position of saucer
        int saucer_y = 180;
        
        int score = 0;
              
        Ball Ball;
        
        Color[] color = {Color.RED, Color.ORANGE,
        Color.MAGENTA, Color.ORANGE,
        Color.CYAN, Color.BLUE};
        int colorIndex;
        
        public PaintSurface() {
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    saucer_x = e.getX() -30;
                    saucer_y = e.getY() -0;
                }
            } );
            Ball = new Ball(30);
        }
        
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                    Shape saucer = new Ellipse2D.Float(
                    saucer_x, saucer_y, 60, 10); // saucer dimensions
            
            g2.setColor(color[colorIndex % 6]);
            
            if (Ball.intersects(saucer_x, saucer_y, 60, 10)
            && Ball.y_speed > 0) {
            
                Ball = new Ball(20);
                score -= 100;
                colorIndex = 0;
            }
            
            else if (Ball.getY() + Ball.getHeight() >= Saucer.HEIGHT)
            {
                Ball = new Ball(20);
                score += 10;
                colorIndex = 0;
            }
            Ball.move();
            g2.fill(Ball);
            
            g2.setColor(Color.GREEN);
            g2.fill(saucer);
            g2.drawString("Score: " + score, 250, 20);
            
            if (score == 20)
            {
                 
            }
        }
    }
    
    class Ball extends Ellipse2D.Float {
        public int x_speed, y_speed;
        private int d;
        private int width = Saucer.WIDTH;
        private int height = Saucer.HEIGHT;
        
        public Ball(int diameter) {
            super((int)(Math.random() * (Saucer.WIDTH - 20) + 1),
                    0, diameter, diameter);
            this.d = diameter;
            this.x_speed = (int)(Math.random() * 5 + 5);
            this.y_speed = (int)(Math.random() * 5 + 5);
        }
        
        public void move() {
            if (super.x < 0 || super.x > width - d)
                x_speed = -x_speed;
            if (super.y < 0 || super.y > height - d)
                y_speed = -y_speed;
            super.x += x_speed;
            super.y += y_speed;
        }
    }
}
1
Contributor
2
Replies
3
Views
9 Years
Discussion Span
Last Post by majestic0110
0

ok have found out how to put a picture on, only trouble is that it takes over everything (ie. obscures the saucer and other objects) any ideas people? cheers

Attachments
/* A game that is Saucer-like
 * Phillip Wells
 */

//to do - make more balls when 100 points met

import java.awt.*;
import java.awt.Toolkit.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;

public class Saucer extends JFrame {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    Image img;
    private PaintSurface canvas;
    
    public void init() {
        this.setSize(WIDTH, HEIGHT);
        this.setTitle("Saucer");
        canvas = new PaintSurface();
        img = Toolkit.getDefaultToolkit().getImage("space.gif");
        this.getContentPane().add(canvas, BorderLayout.CENTER);
        ScheduledThreadPoolExecutor executor =
                new ScheduledThreadPoolExecutor(3);
        executor.scheduleAtFixedRate(new AnimationThread(this),
                0L, 20L, TimeUnit.MILLISECONDS);
        setVisible(true);
    }
    
    
    
    class AnimationThread implements Runnable {
        JFrame c;
        
        public AnimationThread(JFrame c) {
            this.c = c;

        }
        
        public void run() {
            c.repaint();
            
        }
    }
    
    class PaintSurface extends JComponent {
        int saucer_x = 0; // start position of saucer
        int saucer_y = 180;
        
        int score = 0;
              
        Ball Ball;
        
        Color[] color = {Color.RED, Color.ORANGE,
        Color.MAGENTA, Color.ORANGE,
        Color.CYAN, Color.BLUE};
        int colorIndex;
        
        public PaintSurface() {
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    saucer_x = e.getX() -30;
                    saucer_y = e.getY() -0;
                }
            } );
            Ball = new Ball(30);
        }
        
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                    Shape saucer = new Ellipse2D.Float(
                    saucer_x, saucer_y, 60, 10); // saucer dimensions
            g2.drawImage(img, 0, 0, this);
            g2.setColor(color[colorIndex % 6]);
            
            if (Ball.intersects(saucer_x, saucer_y, 60, 10)
            && Ball.y_speed > 0) {
            
                Ball = new Ball(20);
                score -= 100;
                colorIndex = 0;
            }
            
            else if (Ball.getY() + Ball.getHeight() >= Saucer.HEIGHT)
            {
                Ball = new Ball(20);
                score += 10;
                colorIndex = 0;
            }
            Ball.move();
            g2.fill(Ball);
            
            g2.setColor(Color.GREEN);
            g2.fill(saucer);
            g2.drawString("Score: " + score, 250, 20);
            g2.drawImage(img, 0, 0, this);


            
            if (score == 20)
            {
                 
            }
        }
    }
    
    class Ball extends Ellipse2D.Float {
        public int x_speed, y_speed;
        private int d;
        private int width = Saucer.WIDTH;
        private int height = Saucer.HEIGHT;
        
        public Ball(int diameter) {
            super((int)(Math.random() * (Saucer.WIDTH - 20) + 1),
                    0, diameter, diameter);
            this.d = diameter;
            this.x_speed = (int)(Math.random() * 5 + 5);
            this.y_speed = (int)(Math.random() * 5 + 5);
        }
        
        public void move() {
            if (super.x < 0 || super.x > width - d)
                x_speed = -x_speed;
            if (super.y < 0 || super.y > height - d)
                y_speed = -y_speed;
            super.x += x_speed;
            super.y += y_speed;
        }
    }
}
0

have figured out the problem now! had put
g2.drawImage(img, 0, 0, this);
after I had asked java to display the other object ( saucer etc) and therefore it was obscuring them. anyone interested in seeing the repaired version drop me a PM.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.