Help with RPG..
Im trying to create a RPG, but, Im trying to create multiple sprites inside the JFrame. But im limited while using BorderLayout..Any suggestions on this topic? Espiecially for RPG creating.
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Image;
publicclass Client{
publicstaticvoid main(String args[])throws IOException{
try{
Socket s =new Socket("localhost", 1337);
System.out.println("Server is up!");
createFrame();
}
catch(Exception e){
System.out.println("Server offline.");
}
}
publicstaticvoid createFrame(){
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(new Dimension(500, 500));
JLabel aLabel =new JLabel(new ImageIcon("Grass.jpg"), JLabel.CENTER);
frame.getContentPane().add(aLabel, BorderLayout.CENTER);
}
publicstatic JFrame frame =new JFrame("Fendrak - Shadow - ");
}
# 1
You probably don't want to go using the LayoutManager classes for making games. It's really for applications. What you might want to do is make your Client class extend JFrame. Then override the paint method with your stuff. Also, you probably don't need to throw IOExceptions in your main method, because
A) Where are is gonna be thrown to?
B) You're entire main method is in a try catch block
Try something like this
public class Client extends JFrame {
public static void main(String args[]){
try{
//Make your sockets and what have you
Client client = new Client();
}
catch(Exception e) {
system.out.println("Server offline");
}
}
public Client(){
super("My RPG");
}
public void paint(Graphics g){
g.drawImage(new ImageIcon("Grass.jpg"), x, y);
}
}
# 2
That gives me errors Client.java:29: cannot find symbol
symbol : variable x
location: class Client
g.drawImage(new ImageIcon("Grass.jpg"), x, y);
^
Client.java:29: cannot find symbol
symbol : variable y
location: class Client
g.drawImage(new ImageIcon("Grass.jpg"), x, y);
^
2 errors
Press any key to continue . . .
, And if i add ints it gives me Client.java:29: cannot find symbol
symbol : method drawImage(javax.swing.ImageIcon,int,int)
location: class java.awt.Graphics
g.drawImage(new ImageIcon("Grass.jpg"), 50, 50);
^
1 error
Press any key to continue . . .
any help?
Message was edited by:
Shadow1222
Message was edited by:
Shadow1222
# 3
> What you might want to do is make your Client class extend JFrame.
> Then override the paint method with your stuff.
No you don't want to do that. You should never need to override the paint() method of JFrame.
If you need to do custom painting, then you override the paintComponent(...) method of JComponent or JPanel and add the component to the content pane of the frame. You either use a layout manager which will position the component based on the rules of the layout manager. Or in the case of a game with animation you would more likely use a null layout manager (called absolute positioning) and postion the components manually. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html]Using Layout Managers[/url] for more information.
# 4
Without using a 500 x 500 grass format, I want to place Grass sprites on my JFrame that is 500 x 500, im using absolute positioning and it looks a bit like this.import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
public class Client {
public static void main(String args[]) throws IOException {
try {
Socket s = new Socket("localhost", 1337);
System.out.println("Server is up!");
createFrame();
}
catch(Exception e) {
System.out.println("Server offline.");
}
}
public static void createFrame() {
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(new Dimension(500, 500));
addComponentsToPane(frame.getContentPane());
}
public static void addComponentsToPane(Container pane) {
for(int i = 0; i < 1000; i++) {
pane.setLayout(null);
Insets insets = pane.getInsets();
ImageIcon icon = new ImageIcon("Grass.gif");
JLabel Grass = new JLabel(icon);
Dimension size = Grass.getPreferredSize();
Grass.setBounds(i + insets.left, insets.top, size.width, size.height);
pane.add(Grass);
}
}
public static JFrame frame = new JFrame("Fendrak - Shadow - ");
}
# 5
1) You add the components to the frame before making the frame visible.
2) Your code is currently adding 1000 pixel images along the top row of the content pane. The location of each image is only changed by one pixel each time. You should probably be changing the location by the width of the image.
3) You don't need to create 1000 ImageIcons. The icon can be shared by each label.
# 6
Can someone just tell me how?lol
# 7
> Can someone just tell me how?
I just did.
If you mean, "Can someone just WRITE the code for me", the answer is no.
This is a basic concept. If you are having problems then start with something simple. Add 1 image to the frame, then add two images to the frame. Then write a simple loop to add "x" images to the frame. Thats how you learn.
# 8
I don't want it written, I just don't understand how to create multiple images with a loop between a set amount of pixels.