Java - 使用 class 个对象绘画
Java - Paint using class objects
我正在为我正在开发的 class 制作游戏,我需要绘制一些圆形和矩形。我的问题是每个圆圈和矩形的数量未知,具体取决于用户输入。例如,如果用户说 5
,就会出现 25
个圆圈。我想做的是制作一个圆 class 和一个矩形 class,这样我就可以使用循环更改 x 和 y 坐标。
是否可以使用参数化构造函数(x 和 y 坐标)来实现这样的 class "circle"
和 class "rectangle"
?
仅供参考 - 这是一款钉板游戏,您将球放在顶部,球会从钉子上弹开,直到被矩形固定器卡住。我相信这个游戏的真名叫做柏青哥。类似于此图像,但只是一个矩形而不是三角形设置。
https://i.ytimg.com/vi/b3B8qiXyTJ8/maxresdefault.jpg
最后,我必须只使用swing
and/or javafx
(我不熟悉)
您确实可以为矩形和圆形创建 classes,每个都有各自的 x 和 y 坐标。这样的 class 可能看起来像这样:
public class Rectangle
{
int x;
int y;
public Rectangle(int x, int y)
{
this.x = x;
this.y = y;
}
}
如果您想创建此 class 的新实例,您只需使用:
Rectangle myRect = new Rectangle(0,25);
以上代码将创建一个 Rectangle
的实例,其 x 和 y 坐标分别设置为 0 和 25。
希望这对您有所帮助。
编辑 1
然后您的循环可以执行类似这样的操作,按照您的建议在循环中启动矩形:
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);
}
好的,伙计们,我想通了。对于我想做的事情,我只需要在另一个 class 中使用一个 paintComponent() 方法,然后让我的另一个 class Ball() 通过在我的 paintBall 中请求一个 Graphics 对象来使用这个 paintComponent() () 方法的参数,然后使用此图形对象绘制我的 Ball class 对象。我的回答似乎很啰嗦,但看看下面的 Ball class,你就会明白了。
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);
}
}
所以基本上,我只需要在我的主 class 中创建一个 Ball 实例,就像这样:
球 myBall = new Ball();
myBall.drawBall(克); // 其中 g 是主要 class.
中 paintComponent() 的图形对象
不,你做错了永远不要使用 paintComponent();
玩这个。
包裹宠物;
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
}
}
我正在为我正在开发的 class 制作游戏,我需要绘制一些圆形和矩形。我的问题是每个圆圈和矩形的数量未知,具体取决于用户输入。例如,如果用户说 5
,就会出现 25
个圆圈。我想做的是制作一个圆 class 和一个矩形 class,这样我就可以使用循环更改 x 和 y 坐标。
是否可以使用参数化构造函数(x 和 y 坐标)来实现这样的 class "circle"
和 class "rectangle"
?
仅供参考 - 这是一款钉板游戏,您将球放在顶部,球会从钉子上弹开,直到被矩形固定器卡住。我相信这个游戏的真名叫做柏青哥。类似于此图像,但只是一个矩形而不是三角形设置。
https://i.ytimg.com/vi/b3B8qiXyTJ8/maxresdefault.jpg
最后,我必须只使用swing
and/or javafx
(我不熟悉)
您确实可以为矩形和圆形创建 classes,每个都有各自的 x 和 y 坐标。这样的 class 可能看起来像这样:
public class Rectangle
{
int x;
int y;
public Rectangle(int x, int y)
{
this.x = x;
this.y = y;
}
}
如果您想创建此 class 的新实例,您只需使用:
Rectangle myRect = new Rectangle(0,25);
以上代码将创建一个 Rectangle
的实例,其 x 和 y 坐标分别设置为 0 和 25。
希望这对您有所帮助。
编辑 1
然后您的循环可以执行类似这样的操作,按照您的建议在循环中启动矩形:
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);
}
好的,伙计们,我想通了。对于我想做的事情,我只需要在另一个 class 中使用一个 paintComponent() 方法,然后让我的另一个 class Ball() 通过在我的 paintBall 中请求一个 Graphics 对象来使用这个 paintComponent() () 方法的参数,然后使用此图形对象绘制我的 Ball class 对象。我的回答似乎很啰嗦,但看看下面的 Ball class,你就会明白了。
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);
}
}
所以基本上,我只需要在我的主 class 中创建一个 Ball 实例,就像这样:
球 myBall = new Ball();
myBall.drawBall(克); // 其中 g 是主要 class.
中 paintComponent() 的图形对象不,你做错了永远不要使用 paintComponent(); 玩这个。 包裹宠物;
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
}
}