简体   繁体   English

Java-使用类对象进行绘制

[英]Java - Paint using class objects

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. 例如,如果用户说5 ,将有25圆圈。 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. 我想要做的是制作一个圆形类和矩形类,这样我就可以使用循环更改x和y坐标。

Is it possible to make such a class "circle" and class "rectangle" with a parameterized constructor (x and y coordinates) to accomplish this? 是否可以通过参数化的构造函数(x和y坐标)使此类"circle""rectangle"类实现这一目标?

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 https://i.ytimg.com/vi/b3B8qiXyTJ8/maxresdefault.jpg

Lastly, I must use only swing and/or javafx (which I am unfamiliar with) 最后,我必须仅使用swing和/或javafx (我不熟悉)

You can, indeed, create classes for Rectangle and Circle, each with their respective x and y coordinates. 实际上,您可以为Rectangle和Circle创建类,每个类分别具有各自的x和y坐标。 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. 上面的代码将创建一个Rectangle实例,该实例的x和y坐标分别设置为0和25。

Hope this helps. 希望这可以帮助。

EDIT 1 编辑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);
}

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. 对于我想做的事情,我只需要在另一个类中使用paintComponent()方法,然后通过在我的paintBall()方法的参数中请求一个Graphics对象,使另一个类Ball()使用此paintComponent()即可。图形对象来绘制我的Ball类对象。 My answer seems wordy, but look at the Ball class below and you'll get it. 我的回答似乎有些罗word,但是看看下面的Ball类,您会明白的。

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的实例,如下所示:

Ball myBall = new Ball(); Ball myBall =新Ball();

myBall.drawBall(g); myBall.drawBall(g); // where g is the graphics object from paintComponent() in the main class. //其中g是主类中paintComponent()的图形对象。

NO YOU'RE DOING IT WRONG NEVER EVER EVER USE paintComponent(); 没有,您从来没有使用过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
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM