मैं एक साधारण जावा गेम बनाने की कोशिश करता हूं। यह ठीक काम नहीं करता है लेकिन मैं एक पृष्ठभूमि खींचता हूं लेकिन मैंने वर्ग के रंग को नहीं रोका, यह हमेशा बदलता रहता है
मैं उसके लिए एक और कक्षा लिखता हूं और मैं इसे मुख्य रूप से बुलाता हूं लेकिन मैंने इसे काम नहीं किया
@SuppressWarnings ("धारावाहिक")
class BackPan extends JFrame{
private JLayeredPane layers;
private JPanel down;
static int width = Board.boardWidth;
static int height = Board.boardHeight;
Random rnd = new Random();
public BackPan(){
layers = new JLayeredPane();
rnd =new Random();
down = new JPanel(){
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
//***************************************************************
int low = 50;
int high = 255;
for(int i = 0; i<= width; i+=50){
g2d.setColor(new Color(rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low));
g2d.fillRect(i, 50, 50, 50);
for(int j = 0; j<= height; j += 50 ){
g2d.setColor(new Color(rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low,rnd.nextInt(high-low)+low));
g2d.fillRect(i, j, 50, 50);
}
}
}
};
//****************************************************************
down.setBounds(0, 0, width, height);
layers.add(down, new Integer(1));
getContentPane().add(layers, BorderLayout.CENTER);
}
}
2 जवाब
अपने यादृच्छिकरण के लिए एक सेट बीज का उपयोग करने से बेहतर, बस यादृच्छिक छवि को दो में से एक तरीके से ठीक करें:
- रंगों की एक सरणी बनाएं जिसे आप जरूरत पड़ने पर यादृच्छिक करते हैं और फिर पृष्ठभूमि को चित्रित करते समय इस सरणी का उपयोग करें
- इससे भी बेहतर, बस अपने यादृच्छिक रंगों को BufferedImage पर ड्रा करें और उसे पेंटकंपोनेंट विधि में ड्रा करें। यदि इसे फिर से यादृच्छिक करने की आवश्यकता है, तो छवि को फिर से बनाएं।
बाद के उदाहरण के लिए, ध्यान दें कि छवि केवल बटन दबाए जाने पर फिर से यादृच्छिक होती है (createBackground()
विधि को कॉल करके):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class ColorSquares extends JPanel {
public static final int SQR_SIDE = 50;
public static final int COLUMNS = 20;
public static final int ROWS = 16;
private int columns;
private int rows;
private int sqrSide;
private Image backgroundImg;
public ColorSquares(int columns, int rows, int sqrSide) {
this.columns = columns;
this.rows = rows;
this.sqrSide = sqrSide;
backgroundImg = createBackground();
add(new JButton(new AbstractAction("New Background") {
@Override
public void actionPerformed(ActionEvent arg0) {
backgroundImg = createBackground();
repaint();
}
}));
}
public Image createBackground() {
int w = columns * sqrSide;
int h = rows * sqrSide;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
float hue = (float) Math.random();
float saturation = (float) (Math.random() * 0.5 + 0.5);
float brightness = (float) (Math.random() * 0.5 + 0.5);
Color randColor = Color.getHSBColor(hue, saturation, brightness);
g.setColor(randColor);
int x = c * sqrSide;
int y = r * sqrSide;
g.fillRect(x, y, sqrSide, sqrSide);
}
}
g.dispose();
return img;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, this);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(columns * sqrSide, rows * sqrSide);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Colors");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ColorSquares(COLUMNS, ROWS, SQR_SIDE));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
BufferedImage का उपयोग करने का एक अतिरिक्त लाभ यह है कि जैसे आप इसे कर रहे हैं, वैसे ही इसे आकर्षित करना तेज़ है।
सबसे आसान उपाय यह होगा कि एक निरंतर यादृच्छिक बीज को याद रखें और इसे rnd.setSeed() के माध्यम से सेट करें, हर बार paintComponent
कहा जाता है। उदाहरण के लिए:
private final static int SEED = 1000;
rnd.setSeed(SEED);
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।