Java JLabel setHorizontalAlignment with different results - java

I want to center an JLabel inside an BorderLayout. For now I use label.setHorizontalAlignment(SwingConstants.CENTER); and label.setVerticalAlignment(SwingConstants.BOTTOM);.
here the full Code:
public class JSector extends JRenderPanel implements WarpGateConstants {
private Sector sector;
private JLabel jLabelSectorName;
private JLabel[] jLabelWarpGate;
public JSector(Sector s) {
super(new BorderLayout());
this.setSector(s);
setBorder(new EmptyBorder(5, 5, 5, 5));
}
#Override
public void paintView(Graphics2D g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
}
private void setSector(Sector s) {
this.sector = s;
drawSectorInfo(s);
}
public Sector getSector() {
return sector;
}
private void drawSectorInfo(Sector s) {
removeAll();
if (jLabelSectorName == null || jLabelWarpGate == null) {
this.jLabelWarpGate = new JLabel[WARPGATE_MAX_VALUE + 1];
this.jLabelWarpGate[WARPGATE_NORTH] = new JLabel("N");
this.jLabelWarpGate[WARPGATE_EAST] = new JLabel("E");
this.jLabelWarpGate[WARPGATE_SOUTH] = new JLabel("S");
this.jLabelWarpGate[WARPGATE_WEST] = new JLabel("W");
for (byte i = 0; i < jLabelWarpGate.length; i++) {
setupLabel(jLabelWarpGate[i], i);
}
this.jLabelSectorName = new JLabel("SectorName");
add(this.jLabelWarpGate[WARPGATE_NORTH], BorderLayout.NORTH);
add(jLabelWarpGate[WARPGATE_EAST], BorderLayout.EAST);
add(jLabelWarpGate[WARPGATE_SOUTH], BorderLayout.SOUTH);
add(jLabelWarpGate[WARPGATE_WEST], BorderLayout.WEST);
add(jLabelSectorName, BorderLayout.CENTER);
}
for (byte i = 0; i < jLabelWarpGate.length; i++) {
WarpGate gate = s.getWarpGate(i);
if (gate != null && gate.exists()) {
jLabelWarpGate[i].setToolTipText("TargetSector: " + gate.getTargetGridPos());
jLabelWarpGate[i].setVisible(true);
} else {
jLabelWarpGate[i].setVisible(false);
}
}
jLabelSectorName.setText(s.getName());
}
private static JLabel setupLabel(JLabel label, byte warpGateID) {
Font font = label.getFont();
font = new Font(font.getName(), Font.BOLD, font.getSize() + 2);
label.setFont(font);
label.setForeground(new Color(255, 150, 0));
label.setBackground(new Color(0, 100, 255, 150));
label.setOpaque(true);
switch (warpGateID) {
case WARPGATE_NORTH:
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.TOP);
break;
case WARPGATE_EAST:
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setVerticalAlignment(SwingConstants.CENTER);
break;
case WARPGATE_SOUTH:
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.BOTTOM);
break;
case WARPGATE_WEST:
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.CENTER);
break;
}
return label;
}
}
It works good but the West and East Gates have different Vertical positions:
I hope you can help me to solve this problem
EDIT: I think I found the Problem. I set some Lables visible(false) and this causes the problem. But the problem is still there, how do I get these Jlabels on same line.

I found (one) solution that works good for me:
label.setForeground(new Color(255, 255, 255, 0));
this let the Component "appear" but displays nothing => Every JLabel is on the same level

If there are no components in the North or South 'slot' of the BorderLayout, the East and West components will expand to fill that space, causing the center of that component to depend on whether the other components are visible or not.
Three possible solutions come to mind, listed in order of complexity:
Show all of the labels all the time, but replace the contents to make them appear invisible if there's no gate on that edge. You would have to set heights manually to keep them consistent, but that's ok for prototyping.
Use a different LayoutManager (though no particularly good fit comes to mind)
Draw the sector without relying on nested components to represent gates, doing the necessary calculations by hand.
I would probably stick with the first option for now and transition to the third one later on.

Related

What is wrong with the .add in my GUI program?

I am trying to make a GUI with a Grid layout that presents 3 random non repeatable cards within a file. I named all the cards 1-53.png and am trying to insert it into the panels of left, center, and right. When I try adding the files into my panels, there is an error withe the .add, and I do not know how to fix it.
I have already tried to change the .add and the index. I even tried to turn the int into a component, but nothing works.
public class Question_2 {
static String location = "cards/";
public static void main(String[] args) {
JFrame frmMyWindow = new frmMyWindow("Random Cards");
frmMyWindow.setSize(300, 200);
frmMyWindow.setLocationRelativeTo(null);
frmMyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMyWindow.setVisible(true);
}
}
class frmMyWindow extends JFrame {
JLabel lblName, l;
JPanel panelMain, panelLeft, panelCenter, panelRight;
JFrame f;
public frmMyWindow(String Cards) {
super("Random Cards");
lblName = new JLabel("Cards");
panelMain = new JPanel(new GridLayout(1, 3, 10, 10));
setLayout(new BorderLayout(20, 10));
add(lblName, BorderLayout.NORTH);
add(panelMain, BorderLayout.CENTER);
panelLeft = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));
panelCenter = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
panelRight = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 10));
panelMain.add(panelLeft);
panelMain.add(panelCenter);
panelMain.add(panelRight);
panelLeft.setBorder(new TitledBorder("Card 1"));
panelCenter.setBorder(new TitledBorder("Card 2"));
panelRight.setBorder(new TitledBorder("Card 3"));
int index = (int) Math.round(Math.random() * 53);
int index2 = (int) Math.round(Math.random() * 53);
int index3 = (int) Math.round(Math.random() * 53);
while (index == index2) {
index2 = (int) Math.round(Math.random() * 53);
}
while (index3 == index2 || index3 == index)
;
{
index3 = (int) Math.round(Math.random() * 53);
}
String image = index + ".png";
String image2 = index2 + ".png";
String image3 = index3 + ".png";
panelLeft.add(index);
panelCenter.add(index2);
panelRight.add(index3);
}
}
I want the program to present 3 random cards into the panels, but there is an error with the .add.
The problem is indeed to the add method and how you call it. Container#add method takes as arguments Components. You call it with int arguments.
I even tried to turn the int into a component, but nothing works.
The simplest way (I guess) to "add a number in a container" is to create a JLabel and add to it the number as text. By seeing your first attempt, I guess that you again messed up the methods. Probably in JLabels constructor. You did something like new JLabel(index) where index is an Integer. Which again fails because there is no constructor with int argument. The solution is to create a JLabel AND convert the integer to text:
panelLeft.add(new JLabel(String.valueOf(index)));
After that program can be compiled and run. However some notes are:
Always run your application using SwingUtilities#invokeLater since all Swing applications must run on their own thread.
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
JFrame frmMyWindow = new frmMyWindow("Random Cards");
frmMyWindow.setSize(300, 200);
frmMyWindow.setLocationRelativeTo(null);
frmMyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMyWindow.setVisible(true);
});
}
All class names "should" (well, its the standard convention) start with an Uppercase letter. Rename frmMyWindow to FrmMyWindow.

Adding a JScrollPane to an overriden JPanel

I am trying to create a program that computes for the Round Robin algorithm. The logic works fine. My problem is with the overriden JPanel that I use to draw the timeline. The timeline goes on and on without definite line length. I want to add the overriden panel to a scroll pane so it can be scrollable.
SampleGPane.class
import java.awt.*;
import javax.swing.*;
public class
SampleGPane
{
/* Timeline elements */
Container timelineContainer;
JFrame timelineFrame = new JFrame ();
JPanel pnlDraw = new JPanel ();
JScrollPane timelineScroll;
public void
launchFrame ()
{
GPanel gpane = new GPanel ();
timelineContainer = timelineFrame.getContentPane ();
timelineScroll = new JScrollPane (gpane);
timelineContainer.add (timelineScroll);
timelineFrame.setSize (500, 250);
timelineFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
timelineFrame.setVisible (true);
}
private class
GPanel extends JPanel
{
#Override
public void
paintComponent (Graphics g)
{
super.paintComponent (g);
int runningLineX = 0;
int runningLineY = 0;
// g.drawLine (50, 50, orderCount * 5, 50);
runningLineX += 50;
runningLineY += 50;
for (int count = 0; count < 35; count++) {
g.drawString ("J" + (count + 1), runningLineX + 50, 25);
runningLineX += 50;
// runningLineY += 50;
g.drawLine (runningLineX, runningLineY, runningLineX + 50, runningLineY);
}
}
}
}
SampleGPane.class is called by SampleLaunch.class
public class
SampleLaunch
{
public static void main (String args[]) {
SampleGPane sgp = new SampleGPane ();
sgp.launchFrame ();
}
}
The problem is, the JScrollPane won't work. It doesn't seem to detect the line. How do I fix this?
You need to override the getPreferredSize() method of your custom panel to return a reasonable size.
The scrollbars will only appear when the preferred size of the component added to the viewport of the scroll pane is greater than the size of the scroll pane.
The timeline goes on and on without definite line length.
The line length will need to match your painting code. So you need parameters to control what to paint. These parameters will also be used in the calculation of the size of the component. In your example you iterate 35 times and increment the x by 50 so the width would be 1750 plus the starting x offset.

Number and loop issue

I am new to programming and am having difficulty with this problem. I am trying to build a wall based on the number entered in the JTextField but the number cannot exceed 20. I cannot get my error message to display nor can I get my brick wall to build. Could someone please help me figure out what I am doing wrong?
public class Wall extends JApplet implements ActionListener {
JTextField enter;
Boolean submit;
JLabel bricks;
JButton build;
JPanel top;
Image zombie;
int value;
public void init() {
setLayout(new BorderLayout());
top = new JPanel();
build = new JButton("AHHHHHH...ZOMBIES!"); //button for building wall
bricks = new JLabel("Enter between 1 & 20 rows to contruct:");
enter = new JTextField(2);
top.add(build); //add zombie button
top.add(bricks); //add intructions
top.add(enter); //add text field
add(top, BorderLayout.NORTH);
build.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == build) {
int value = Integer.parseInt(build.getText());
if (value > 0 && value < 21) {
submit = true;
repaint();
} else {
submit = false;
repaint();
}
}
}
public void paint(Graphics g) {
super.paint(g);
//add zombie image
Image zombie = getImage(getCodeBase(), "Zombie.jpg");
g.drawImage(zombie, 0, 45, this);
if (submit = false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
int brick_width = 50;
int brick_height = 20;
int spacing = 1;
int x = 0;
while (x < 21) {
drawBrick(g, nextInt(brick_width + spacing), nextInt(brick_height + spacing));
x = x + getWidth() + 50;
x = x - 25 + getWidth() + 50;
x++;
}
}
public void drawBrick(Graphics g, int x, int y) {
g.setColor(new Color(150, 0, 0));
g.fillRect(0, 635, 50, 20);
}
}
Instead of painting on top-level containers such as JApplet directly, you have to use JPanel and paint on it. Just override paintComponent() method of JPanel(). Never forget to call super.paintComponent(g) in overridden method.
sample code:
top = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
...
// your custom painting code goes here
}
};
Note:
submit == false and !submit are the correct way to check it but it still will result in NullPointerException because you have never initialized instance variable submit.
Use primitive boolean instead of Boolean to avoid such exception or initialize it properly.
Boolean submit = false;
Change your if statement to:
if (submit == false)
That'll get your error message to pop up
Should be if (submit == false) rather than if (submit = false).
Two issues:
1) Your submit variable is not initialized, you should initialize it to true/false.
If you dont want to initialize it, you can simple try with
if(submit!=null && !submit)
2)
Secondly, "=" is used for assigning a value where as "==" is used for comparing
Change
if (submit = false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
to
if (submit == false) //add error message
{
g.setColor(Color.WHITE);
g.setFont(new Font("TimesRoman", Font.BOLD, 40));
g.drawString("You must enter a number between 1 & 20!", 400, 100); //add message
}
To solve your error display issue,
submit = false should be submit == false
= is for assignment
== is for comparison
Also, initialize your variable to avoid null pointer exception:
boolean submit = false;

How to paint corners of a JButton with numbers?

I'm making a simple Kakuro application in Java Swing and I have used JButtons as cells. I've done everything from generating the grid (setBackground(Color.BLACK) and setBackground(Color.WHITE)) to filling with unique numbers.
But the problem is, I don't know how to paint the "clues" at the ends of JButtons. What I want is similar to:
Sometimes the numbers may appear on only 3 sides, 2 sides or even 1 side.
I thought of setting background images, but its not possible as the numbers are sums of dynamically generated numbers (the grid is dynamic).
So any idea how to get this kind of JButton? Or if its not possible, what other options do I have?
Thanks a lot in advance (I'm really stuck).
very simple and confortable way is add JLabels to the JButton by using BorderLayout,
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class FourLabelsInButton {
private JFrame frame;
private JButton myButton1;
private JLabel myButton1_Label_N;
private JLabel myButton1_Label_E;
private JLabel myButton1_Label_W;
private JLabel myButton1_Label_S = new JLabel();
public FourLabelsInButton() {
myButton1_Label_N = new JLabel("45");
myButton1_Label_N.setHorizontalAlignment(JLabel.CENTER);
myButton1_Label_N.setForeground(Color.red);
myButton1_Label_E = new JLabel("1");
myButton1_Label_E.setHorizontalAlignment(JLabel.CENTER);
myButton1_Label_E.setForeground(Color.red);
myButton1_Label_W = new JLabel("9");
myButton1_Label_W.setHorizontalAlignment(JLabel.CENTER);
myButton1_Label_W.setForeground(Color.red);
myButton1_Label_S = new JLabel("21");
myButton1_Label_S.setHorizontalAlignment(JLabel.CENTER);
myButton1_Label_S.setForeground(Color.red);
myButton1 = new JButton();
myButton1.setBackground(Color.black);
myButton1.setLayout(new BorderLayout());
myButton1.add(myButton1_Label_N, BorderLayout.NORTH);
myButton1.add(myButton1_Label_E, BorderLayout.EAST);
myButton1.add(myButton1_Label_W, BorderLayout.WEST);
myButton1.add(myButton1_Label_S, BorderLayout.SOUTH);
frame = new JFrame();
frame.add(myButton1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
FourLabelsInButton ggg = new FourLabelsInButton();
}
});
}
}
I thought of setting background images, but its not possible as the numbers are sums of dynamically generated numbers (the grid is dynamic).
The images for the buttons can also be generated dynamically.
You may also use a custom component. For this case the painting can be implemented quite straightforward:
class KakuroComponent extends JComponent {
private final int[] numbers;
public KakuroComponent(int... numbers) {
this.numbers = numbers;
}
#Override
public void paintComponent(Graphics g) {
int w = getWidth();
int h = getWidth();
g.setColor(Color.black);
g.fillRect(0, 0, w, h);
g.setColor(Color.white);
g.drawLine(0, 0, w, h);
g.drawLine(w - 1, 0, 0, h - 1);
if (numbers[0] > 0) // if there is a top number
drawStringCentered(g, String.valueOf(numbers[0]), w / 2, h / 6);
if (numbers[1] > 0) // if there is a left number
drawStringCentered(g, String.valueOf(numbers[1]), w / 6, h / 2);
if (numbers[2] > 0) // if there is a right number
drawStringCentered(g, String.valueOf(numbers[2]), w * 5 / 6, h / 2);
if (numbers[3] > 0) // if there is a bottom number
drawStringCentered(g, String.valueOf(numbers[3]), w / 2, h * 5 / 6);
}
void drawStringCentered(Graphics g, String s, int x, int y) {
Rectangle2D bounds = g.getFontMetrics().getStringBounds(s, g);
g.drawString(s, (int) (x - bounds.getCenterX()), (int) (y - bounds.getCenterY()));
}
}
There shouldn't be any reason you can't simply draw anything you want in the JButton by overwriting the paint() method.
public class KakuroSquare extends JButton
{
/* ... */
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
/* Your paint logic. */
}
}

Placing component on Glass Pane

I have a subclass of JLabel that forms a component of my GUI. I have implemented the ability to drag and drop the component from one container to another, but without any visual effects. I want to have this JLabel follow the cursor during the drag of the item from one container to another. I figured that I could just create a glass pane and draw it on there. However, even after I add the component to the glass pane, set the component visible, and set the glass pane visible, and set the glass pane as opaque, I still so not see the component. I know the component works because I can add it to the content pane and have it show up.
How do I add a component to the glass pane?
Finally figured how to get the simple example working. Thanks, #akf. I was able to adapt this solution to my original problem, allowing me to remove ~60 lines of Java2D code that manually rendered a representation of the JLabel.
package test;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class MainFrame extends JFrame {
/**
* #param args
*/
public static void main(String[] args) {
MainFrame mf = new MainFrame();
mf.setSize(400, 400);
mf.setLocationRelativeTo(null);
mf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
mf.setGlassPane(new JPanel());
JLabel l = new JLabel();
l.setText("Hello");
l.setBorder(new LineBorder(Color.BLACK, 1));
l.setBounds(10, 10, 50, 20);
l.setBackground(Color.RED);
l.setOpaque(true);
l.setPreferredSize(l.getSize());
//mf.add(l);
((JPanel)mf.getGlassPane()).add(l);
mf.getGlassPane().setVisible(true);
mf.setVisible(true);
}
}
The example code below shows how to drag a chess piece around a chess board. It uses JLayeredPane instead of a glass pane, but I'm sure the concepts would be the same. That is:
a) add the glass pane to the root pane
b) make the glass pane visible
c) add the component to the glass pane making sure the bounds are valid
d) use setLocation() to animate the dragging of the component
Edit: added code to fix SSCCE
JLabel l = new JLabel();
l.setText("Hello");
l.setBorder(new LineBorder(Color.BLACK, 1));
// l.setPreferredSize(l.getSize());
// l.setBounds(10, 10, 50, 20);
((JPanel)mf.getGlassPane()).add(l);
mf.setVisible(true);
mf.getGlassPane().setVisible(true);
When using layout managers you never use the setSize() or setBounds() methods. In your case you just set the preferred size to (0, 0) since this is the default size of all components.
It works when you add the label to the frame because the default layout manger for the content pane of the frame is a border layout, therefore the preferred size of the label is ignored and the label is made the size of the frame.
However, by default a JPanel uses a FlowLayout which does respect the preferred size of the component. Since the preferred size is 0, there is nothing to paint.
Also, the glass pane needs to made visible in order for it to be painted.
I suggest you read the Swing tutorial. There are section on how layout managers work and on how glass panes work and each section has working examples.
Edit: Example code added below:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
public ChessBoard()
{
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize( boardSize );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
getContentPane().add(layeredPane);
// Add a chess board to the Layered Pane
chessBoard = new JPanel();
chessBoard.setLayout( new GridLayout(8, 8) );
chessBoard.setPreferredSize( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
// Build the Chess Board squares
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
JPanel square = new JPanel( new BorderLayout() );
square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
chessBoard.add( square );
}
}
// Add a few pieces to the board
ImageIcon duke = new ImageIcon("dukewavered.gif"); // add an image here
JLabel piece = new JLabel( duke );
JPanel panel = (JPanel)chessBoard.getComponent( 0 );
panel.add( piece );
piece = new JLabel( duke );
panel = (JPanel)chessBoard.getComponent( 15 );
panel.add( piece );
}
/*
** Add the selected chess piece to the dragging layer so it can be moved
*/
public void mousePressed(MouseEvent e)
{
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel) return;
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
/*
** Move the chess piece around
*/
public void mouseDragged(MouseEvent me)
{
if (chessPiece == null) return;
// The drag location should be within the bounds of the chess board
int x = me.getX() + xAdjustment;
int xMax = layeredPane.getWidth() - chessPiece.getWidth();
x = Math.min(x, xMax);
x = Math.max(x, 0);
int y = me.getY() + yAdjustment;
int yMax = layeredPane.getHeight() - chessPiece.getHeight();
y = Math.min(y, yMax);
y = Math.max(y, 0);
chessPiece.setLocation(x, y);
}
/*
** Drop the chess piece back onto the chess board
*/
public void mouseReleased(MouseEvent e)
{
layeredPane.setCursor(null);
if (chessPiece == null) return;
// Make sure the chess piece is no longer painted on the layered pane
chessPiece.setVisible(false);
layeredPane.remove(chessPiece);
chessPiece.setVisible(true);
// The drop location should be within the bounds of the chess board
int xMax = layeredPane.getWidth() - chessPiece.getWidth();
int x = Math.min(e.getX(), xMax);
x = Math.max(x, 0);
int yMax = layeredPane.getHeight() - chessPiece.getHeight();
int y = Math.min(e.getY(), yMax);
y = Math.max(y, 0);
Component c = chessBoard.findComponentAt(x, y);
if (c instanceof JLabel)
{
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
parent.validate();
}
else
{
Container parent = (Container)c;
parent.add( chessPiece );
parent.validate();
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args)
{
JFrame frame = new ChessBoard();
frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
frame.setResizable( false );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
Although tangential to the question, the JLayeredPane example cited by #camickr admits the following adaptation, which highlights the effect of mouseReleased() over an existing component.
public ChessBoard() {
...
// Add a few pieces to the board
addPiece(3, 0, "♛");
addPiece(4, 0, "♚");
addPiece(3, 7, "♕");
addPiece(4, 7, "♔");
}
static Font font = new Font("Sans", Font.PLAIN, 72);
private void addPiece(int col, int row, String glyph) {
JLabel piece = new JLabel(glyph, JLabel.CENTER);
piece.setFont(font);
JPanel panel = (JPanel) chessBoard.getComponent(col + row * 8);
panel.add(piece);
}
Besides the pointers to the LayerPane examples already provided, the issue with your original code centers around the setting of the preferred size of your label. You set it before the JLabel has been sized, so your:
l.setPreferredSize(l.getSize());
is ineffectual. If, on the other hand, you make that call after you make your call to setBounds, you will see your desired results. With that in mind, reorder this:
l.setPreferredSize(l.getSize());
l.setBounds(10, 10, 50, 20);
to look like this:
l.setBounds(10, 10, 50, 20);
l.setPreferredSize(l.getSize());
Since I had been following Romain Guy's blogs on Swing for a long time. I have a link that you might be interested in. He released the source - which used a GlassPane for DnD effects.
http://jroller.com/gfx/entry/drag_and_drop_effects_the
I myself never did use a fizzy animation/effect on DnD, so can't comment any further :-|

Categories

Resources