我如何在面板中绘制两个不同的class?
How i can paint in the Panel with two difference class?
我正在 Java 做一个项目,我必须构建一个 Leemings 游戏的克隆,我在尝试构建世界时遇到了问题:
我创造了 class“fenetre”(windows 英文)、class Leemings 和 class Carreau(leemings 可以行走的平台)
问题来了,我在面板上画了一只旅鼠,我还想画一个平台,旅鼠可以在同一个面板上行走,但我不能在同一个面板上画,所以我只能看到平台但是我再也看不到leemings了。
我在这里分享代码:
package lemmingsLight;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class Fenetre extends JComponent {
public Fenetre() {
// Prepare a panel to display the lemmings
JPanel lemmings = new JPanel();
lemmings.setLayout(new BorderLayout());
lemmings.setBounds(0, 0, 500, 250);
lemmings.setBackground(Color.WHITE);
add(lemmings);
// Add the lemmings to the panel
Lemmings myTestLemming = new Lemmings();
lemmings.add(myTestLemming, BorderLayout.CENTER);
// Add the plateform to the same panel
Carreau pf = new Carreau();
lemmings.add(pf);
MouseAdapter m = new MyMouse(this);
addMouseListener(new MyMouse(this));
JPanel lave = new JPanel(); //fire -> if the leemings fall the leemings die (or burn)
lave.setBounds(0, 250, 500, 75);
lave.setBackground(Color.RED);
add(lave);
JPanel panCom = new JPanel(); //Control or command
panCom.setBounds(0, 320, 500, 200);
panCom.setBackground(Color.GRAY);
add(panCom);
JButton b1 = new JButton("pause");
JButton b2 = new JButton("bloquer");
JButton b3 = new JButton("creuser");
JButton b4 = new JButton("tunnel");
JButton b5 = new JButton("bombe");
JButton b6 = new JButton("escalier");
JButton b7 = new JButton("grimper");
JButton b8 = new JButton("parachute");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Lemmings.setState(NORMAL);
}
});
panCom.add(b1);
panCom.add(b2);
panCom.add(b3);
panCom.add(b4);
panCom.add(b5);
panCom.add(b6);
panCom.add(b7);
panCom.add(b8);
}
}
class“卡洛”:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
//Pour l'instant class inutile, on va reflechir de ce qu'on peut faire ce Carreau
public class Carreau extends JComponent{
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillRect(55, 200, 200, 20);
}
}
和class旅鼠:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Lemmings extends JComponent {
private LemmingsState state;
public LemmingsState getState() {
return state;
}
public void setState(LemmingsState state) {
this.state = state;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(45, 150, 20, 20);
}
}
如果你想看主线:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main{
public static void main(String[] args) {
JFrame frame = new JFrame("LemmingsLight");
Fenetre f = new Fenetre();
f.setBackground(Color.WHITE);
frame.setContentPane(f);
frame.setSize(500, 410);
//frame.setSize(f.getSize());
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我正在添加 class LemmingState 来尝试代码:
package lemmingsLight;
public enum LemmingsState { //états des Lemmings
NORMAL,
BOMBEUR,
TUNNELIER,
BLOQUEUR,
GRIMPEUR,
CHARPENTIER,
FOREUR,
PARACHUTEUR,
GLemmingState() {
}
}
感谢您阅读我的问题
您需要一个 class 来扩展 JPanel (fenetre) 和 array/list 对象(在您的例子中是 Lemmings 和 Carreau 列表),这两个对象都应该有一个 void 函数,您调用并传递图形:
public class Lemmings{
public void draw(Graphics2D g2d)
{
//draw something here
}
}
然后在 fenetre 的 paint 函数中,你只需调用数组中每个对象的函数 draw
public class Fenetre extends JPanel{
List<Lemmings> lems = new ArrayList<>(); //add the objects into this one
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
for(int i = 0; i < lems.size(); i++)
{
lems.get(i).draw(g2d);
}
}
}
我正在 Java 做一个项目,我必须构建一个 Leemings 游戏的克隆,我在尝试构建世界时遇到了问题:
我创造了 class“fenetre”(windows 英文)、class Leemings 和 class Carreau(leemings 可以行走的平台)
问题来了,我在面板上画了一只旅鼠,我还想画一个平台,旅鼠可以在同一个面板上行走,但我不能在同一个面板上画,所以我只能看到平台但是我再也看不到leemings了。
我在这里分享代码:
package lemmingsLight;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class Fenetre extends JComponent {
public Fenetre() {
// Prepare a panel to display the lemmings
JPanel lemmings = new JPanel();
lemmings.setLayout(new BorderLayout());
lemmings.setBounds(0, 0, 500, 250);
lemmings.setBackground(Color.WHITE);
add(lemmings);
// Add the lemmings to the panel
Lemmings myTestLemming = new Lemmings();
lemmings.add(myTestLemming, BorderLayout.CENTER);
// Add the plateform to the same panel
Carreau pf = new Carreau();
lemmings.add(pf);
MouseAdapter m = new MyMouse(this);
addMouseListener(new MyMouse(this));
JPanel lave = new JPanel(); //fire -> if the leemings fall the leemings die (or burn)
lave.setBounds(0, 250, 500, 75);
lave.setBackground(Color.RED);
add(lave);
JPanel panCom = new JPanel(); //Control or command
panCom.setBounds(0, 320, 500, 200);
panCom.setBackground(Color.GRAY);
add(panCom);
JButton b1 = new JButton("pause");
JButton b2 = new JButton("bloquer");
JButton b3 = new JButton("creuser");
JButton b4 = new JButton("tunnel");
JButton b5 = new JButton("bombe");
JButton b6 = new JButton("escalier");
JButton b7 = new JButton("grimper");
JButton b8 = new JButton("parachute");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Lemmings.setState(NORMAL);
}
});
panCom.add(b1);
panCom.add(b2);
panCom.add(b3);
panCom.add(b4);
panCom.add(b5);
panCom.add(b6);
panCom.add(b7);
panCom.add(b8);
}
}
class“卡洛”:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
//Pour l'instant class inutile, on va reflechir de ce qu'on peut faire ce Carreau
public class Carreau extends JComponent{
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillRect(55, 200, 200, 20);
}
}
和class旅鼠:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Lemmings extends JComponent {
private LemmingsState state;
public LemmingsState getState() {
return state;
}
public void setState(LemmingsState state) {
this.state = state;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(45, 150, 20, 20);
}
}
如果你想看主线:
package lemmingsLight;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main{
public static void main(String[] args) {
JFrame frame = new JFrame("LemmingsLight");
Fenetre f = new Fenetre();
f.setBackground(Color.WHITE);
frame.setContentPane(f);
frame.setSize(500, 410);
//frame.setSize(f.getSize());
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我正在添加 class LemmingState 来尝试代码:
package lemmingsLight;
public enum LemmingsState { //états des Lemmings
NORMAL,
BOMBEUR,
TUNNELIER,
BLOQUEUR,
GRIMPEUR,
CHARPENTIER,
FOREUR,
PARACHUTEUR,
GLemmingState() {
}
}
感谢您阅读我的问题
您需要一个 class 来扩展 JPanel (fenetre) 和 array/list 对象(在您的例子中是 Lemmings 和 Carreau 列表),这两个对象都应该有一个 void 函数,您调用并传递图形:
public class Lemmings{
public void draw(Graphics2D g2d)
{
//draw something here
}
}
然后在 fenetre 的 paint 函数中,你只需调用数组中每个对象的函数 draw
public class Fenetre extends JPanel{
List<Lemmings> lems = new ArrayList<>(); //add the objects into this one
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
for(int i = 0; i < lems.size(); i++)
{
lems.get(i).draw(g2d);
}
}
}