what is anchoring?
When designing the frame, you put components and the IDE automatically puts them in a precise position, seems that if you move the component in execution time (like with 4 buttons editing their bounds) the component seems to move yes, but it usually automatically returns to it’s starting position…
The solution for this, is to write yourself the code of the JFrame
Btw… what is a layout?
still don’t know what is anchoring… when you try to move/resize something it goes back like you did nothing?
layout? …this is all basic of swing, read a few swing tutorials and always have JavaDoc accessible, I’m not gonna teach you basics every time you need something. And just to answer this one: layout is code (object) that controls how components are rendered (shown) in some container. Swing components have BorderLayout by default, that layout has sides (north, south, west, east) and center, so when you add components you specify where you add them.
.add(button, BorderLayout.CENTER); will add button to center of container
thank you kova
EDIT: i believe that the problem of anchoring is because the layout says what position must the components be in. I think i’ll make my game without a layout until i’ve “played” more with layouts
I thought it was FlowLayout by default… not that it matters. At all, just looking for an excuse to post something.
to put the cards in the field i won’t use a layout but for the other components i’ll use a layout, because the cards will have a more or less arbitrary position and no layout can help me. Anyways, the user wont be able to resize the main frame…
Well, after some tries, i will tell what i’ve got:
I’ve got a frame of 800x600 which it should show, a JPanel that shows an image of about 600x600, in the right side of the screen i have 3 textpanels that show different information, until here everything is okay.
The 3 textpanels are organized in different layouts but they show everything i want like i want. The problem comes with the main image.
Over this image, there should appear 16 different pannels on a distribution like:
http://img236.imageshack.us/img236/2096/cardlayoutta0.jpg
The problem is: if i use no layout (JFrame.setLayout(null)) then only the components with layout will be shown (the info on the right side of the screen and with their right size) but no cards and no background image is shown.
If, in the other hand, i use the default layout (a box one?) then a big black square appears instead of the background (this is, i believe, because when the cards are init they look like black rectangles).
Can someone help me in choosing the correct layout? if absolute posicioning must be used, can someone help me use it? because i’ve done what java tutorials say but with no success
every container (JFrame, JPanel,…) has a layout. You don’t need to set layout of JFrame to null, and please don’t. Null layout (FlowLayout, are these the same?) is used for absolute positioning. Set it for JPanel you draw into. Your components will only display when you set their bounds (size and location). I’ve done that in my example code I’ve posted earlier.
Again to remind you… you could always forget stupid swing and just use images and draw. Much simpler and faster to do.
Edit: maybe it’s FlowLayout by default for swing components and not BorderLayout like I said… dunno, but I’m sticking to my original thought
My point is, i think that the best solution is put my cards with absolutie posicioning, the user won’t be able to resize the window anyway…
But i dont understand why the cards aren’t shown…
Edit: now no big black rectangle is shown…
public MesaGrafica() {
//Note: X offset = 4, Y offset = 30
super("Mus");
fondomesa = new Fondo();
ssalida = new JScrollPane();
salida = new JTextPane();
puntuaciones = new JPanel();
puntuacion1 = new JTextPane();
puntuacion2 = new JTextPane();
infoderecha = new JPanel();
this.setLayout(null); //For the absolute posicioning
//The next lines are to create the infomation panels that are shown correctly (there should be no problem from here
SimpleAttributeSet centrado = new SimpleAttributeSet();
StyleConstants.setAlignment(centrado,StyleConstants.ALIGN_CENTER);
puntuacion1.setParagraphAttributes(centrado,true);
puntuacion2.setParagraphAttributes(centrado,true);
puntuaciones.setLayout(new BoxLayout(puntuaciones,BoxLayout.X_AXIS));
puntuaciones.add(puntuacion1);
puntuaciones.add(puntuacion2);
infoderecha.setLayout(new BoxLayout(infoderecha,BoxLayout.Y_AXIS));
ssalida.setPreferredSize(new Dimension(196,266));
puntuaciones.setPreferredSize(new Dimension(192,260));
JLabel estado = new JLabel("Estado");
estado.setForeground(new Color(0,0,255));
estado.setAlignmentX(Component.CENTER_ALIGNMENT);
infoderecha.add(puntuaciones);
infoderecha.add(Box.createRigidArea(new Dimension(0,20)));
infoderecha.add(estado);
infoderecha.add(ssalida);
ssalida.setViewportView(salida);
this.setBounds((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2 - 400,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 - 300,800,600);
//to here
cartasEnMesa = new CartaGrafica[4][4];
cartasEnMesa[0][0] = new CartaGrafica(156,428,57,85);
cartasEnMesa[0][1] = new CartaGrafica(233,428,57,85);
//(...)
for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{
cartasEnMesa[i][j].setLayout(null);
this.add(cartasEnMesa[i][j]);
}
}
this.add(infoderecha);
this.add(fondomesa);
infoderecha.setBounds(600,0,192,566);
puntuacion1.setBorder(BorderFactory.createLineBorder(new Color(0,0,0),1));
puntuacion2.setBorder(BorderFactory.createLineBorder(new Color(0,0,0),1));
puntuacion1.setEditable(false);
puntuacion2.setEditable(false);
this.actualizarPuntuaciones(null);
salida.setEditable(false);
salida.setFont(new Font("Arial",Font.PLAIN,10));
this.setVisible(true);
this.setResizable(false);
fondomesa.setVisible(true);
}
CartaGrafica “code” the important thing is the paint method, the gets and sets are omitted here…
public class CartaGrafica extends javax.swing.JPanel {
/** Creates a new instance of CartaGrafica */
private StringBuffer rutaImagen;
private int jugador;
private boolean tapada;
public CartaGrafica() {
}
public CartaGrafica(int x, int y, int width, int height)
{
super();
this.setBounds(x,y,width,height);
}
public void paint(Graphics g)
{
if (rutaImagen == null)
{
g.setColor(new Color(0,0,0));
g.fillRect((int)this.getBounds().getX(),(int)this.getBounds().getY(),(int)this.getBounds().getWidth(),(int)this.getBounds().getHeight());
return;
}
if (this.estaTapada())
{
g.setColor(new Color(0,0,255));
g.fillRect((int)this.getBounds().getX(),(int)this.getBounds().getY(),(int)this.getBounds().getWidth(),(int)this.getBounds().getHeight());
}
else
{
try
{
//((Graphics2D)g).setTransform(java.awt.geom.AffineTransform.getQuadrantRotateInstance(1,this.getWidth()/2,this.getHeight()/2));
g.drawImage((Image)ImageIO.read(new File("C:\\" + rutaImagen.toString() + ".jpg")),0,0,this.getWidth(),this.getHeight(),null);
}
catch(IOException e)
{
g.setColor(new Color(0,0,0));
g.fillRect((int)this.getAlignmentX(),(int)this.getAlignmentY(),this.getWidth(),this.getHeight());
System.out.println("Ha fallado al leer la imagencita");
}
}
}
}
It could be nice to print directly into the frame but the image to print depends on the values of another class (a non graphical Card) and this way (with a JPanel) i think is easier of implement…
didn’t you read the tutorials!? You need to override paintComponent(), not paint() in CartaGrafica. Ok, everything you basicly need is in that swing/awt painting tutorial. I won’t help you much more, it’s useless, you must try yourself so you can learn.
kova, you “win”, i’ll draw directly the cards without using a panel for every card (there is no way i’ve been able to get the panels painted on the image :()
I think it will be harder to manage, but i wont do miracles nor a wow game with them so is worth the extra code
I think i’ve read all of the tutorials, (i dont have time to read ALL the tutorials of everything '^^)
Now what i do is draw some rectangles over the panel with the image, BUT after they are drawn, they dissapear… ive tryed to overwrite the paint method too but with no success (although the tutorials say that this is a no no…)
I’m starting to getting embarrased of asking here… :-[ :-[
now you didn’t even ask a question and expect help Just saying what is happening isn’t nearly enough, you need to say how do you try to do it.
http://java.sun.com/products/jfc/tsc/articles/painting/ … this is all you need to know, it’s all here and it’s not long either. Don’t reply until you’ve read this
You might be interested in the Evver Games (http://www.evver.com) card platform. It is an API for creating multi-player card games. The project contains a servlet front end, but that is completely separate from the ‘card platform’ library, which you could use in your game with a swing / java2d front end.