Reputation:
I am building a game for a class I'm working on and I need to paint some circles and rectangles. My problem is that there will be an unknown number of each the circles and rectangles, depending on user input. For example,if the user says 5
, there will be 25
circles. What I wanted to do was make a circle class and rectangle class so I could just change the x and y coordinates using a loop.
Is it possible to make such a class "circle"
and class "rectangle"
with a parameterized constructor (x and y coordinates) to accomplish this?
FYI - This is a pegboard game where you drop a ball on the top and the ball bounces off pegs until being caught in the rectangle holders. I believe the true name of this game is called pachinko. Similar to this image, but just a rectangle instead of a triangle setup.
https://i.ytimg.com/vi/b3B8qiXyTJ8/maxresdefault.jpg
Lastly, I must use only swing
and/or javafx
(which I am unfamiliar with)
Upvotes: 0
Views: 3275
Reputation: 9
NO YOU'RE DOING IT WRONG NEVER EVER EVER USE paintComponent(); Play around with this. package pet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.*;
public class pet extends JPanel implements MouseListener{
public static JFrame frame = new JFrame("frame");
public pet() throws IOException{
setPreferredSize(new Dimension(870, 675)); //configuring panel
addMouseListener(this);
}
public static void main(String[] args) throws IOException{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new pet();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
frame.addMouseListener(new pet());
}
public void paintRectangleAtPoint(Graphics g, int x, int y, String shoutout){
g.setColor(Color.BLACK);
g.drawRect(x, y, 100,100);
System.out.println("See, you can send variables in, too "+ shoutout");
}
public void paintStuff(Graphics g, int x, int y){
g.setColor(Color.BLACK);
g.drawOval(x, y, 100,100);
}
@Override
public void mouseClicked(MouseEvent e) {
paintStuff(frame.getGraphics(),e.getX(), e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
PaintRectangleAtPoint(frame.getGraphics(),100, e.getY(), "entered");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Upvotes: 0
Reputation:
Okay Guys, I figured this one out. For what I wanted to do, I simply had needed a paintComponent() method inside another class, then made my other class Ball() use this paintComponent() by requesting a Graphics object inside a my paintBall() method's parameters, then using this graphics object to paint my Ball class object. My answer seems wordy, but look at the Ball class below and you'll get it.
package main;
import java.awt.Graphics;
public class Ball{
private int x = 5;
private int y = 30;
// This method will help to animate the current object,
// The xPos and yPos will change, then frame will be repainted
public void setPos(int xPos, int yPos)
{
x = xPos;
y = yPos;
}
public void drawBall(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(x, y, 30, 30);
}
}
So basically, i just had to create an instance of Ball inside my main class, like so:
Ball myBall = new Ball();
myBall.drawBall(g); // where g is the graphics object from paintComponent() in the main class.
Upvotes: 0
Reputation: 774
You can, indeed, create classes for Rectangle and Circle, each with their respective x and y coordinates. Such a class might look something like this:
public class Rectangle
{
int x;
int y;
public Rectangle(int x, int y)
{
this.x = x;
this.y = y;
}
}
When you want to create a new instance of this class, you would simply use:
Rectangle myRect = new Rectangle(0,25);
The above code would create an instance of Rectangle
which has the x and y coordinates set to 0 and 25 respectively.
Hope this helps.
EDIT 1
Your loop could then do something like this to initiate your rectangles in a loop as you suggested:
Rectangle[] myRectangles = new Rectangle[25];
int x = 0;
int y = 0;
for (int i = 0; i < 25; i++)
{
x = x+25;
y = y+20;
myRectangles[i] = new Rectangle(x,y);
}
Upvotes: 1