简体   繁体   中英

How would I move an object shape from one location to another?

I am trying to move an object in this case a blue circle from one location to another. This would be on a 8*8 grid.

gBoard.setColor(Color.LIGHT_GRAY);
    gBoard.fillRect(0,0,400,400);
    for (int k=000; k<=300; k+=100){
        for (int l=000; l<=300; l+=100){
            gBoard.clearRect(k,l,50,50);
        }
    }
    for (int k=50; k<=350; k+=100){
        for (int l=50; l<=350; l+=100){
            gBoard.clearRect(k,l,50,50);
        }
    }

The code above shows that I have successfully created the 8*8 grid which means I can place the object where it needs to go.

    gBoard.setColor(Color.BLUE);
    int x = 0;
    int y = 0;
    gBoard.fillOval(x,y,50,50);

The code above is showing that I have put the object into the grid, however would this go into a public void method or would this be a separate method as the object will not be at a constant location. The object would be constantly moving. Is public void more appropriate or is it best to use a separate method for the interface?

Suggestions:

  • Create a grid of JLabel added to a JPanel using GridLayout
  • Give each JLabel an empty ImageIcon of appropriate size with the JLabel's constructor that takes an Icon.
  • Also create another ImageIcon that holds a colored disk
  • Move the icon to the appropriate JLabel by calling setIcon(...) on that JLabel
  • Remove an icon from a JLabel by calling setIcon(...) and passing in the empty or blank icon.

eg,

在此处输入图片说明

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class ColoredOvals extends JPanel {
    public static final int CELL_WIDTH = 50;
    public static final int SIDE = 8;
    private JLabel[][] grid = new JLabel[SIDE][SIDE];
    private Icon emptyIcon;
    private Icon colorIcon;

    public ColoredOvals() {
        // so lines appear between cells
        setBackground(Color.BLACK);

        // empty icon is 50x50 in size, and with clear color
        emptyIcon = createIcon(new Color(0, 0, 0, 0));
        // icon with a RED disk 
        colorIcon = createIcon(Color.RED);

        // create a grid layout to hold the JLabels
        // the 1, 1 is for the empty space between cells to show the black line
        setLayout(new GridLayout(SIDE, SIDE, 1, 1)); 

        // line around the entire JPanel (if desired)
        setBorder(BorderFactory.createLineBorder(Color.BLACK));

        // mouse listener that moves the icon to the selected cell:
        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                clearGrid();  // all labels hold blank icon
                JLabel label = (JLabel) e.getSource();
                label.setIcon(colorIcon);  // selected JLabel holds disk
            }
        };

        // iterate through the grid 2D array, creating JLabels and adding
        // blank icon as well as a MouseListener
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                grid[i][j] = new JLabel(emptyIcon); // blank icon
                grid[i][j].setOpaque(true);
                grid[i][j].setBackground(Color.WHITE);
                add(grid[i][j]);
                grid[i][j].addMouseListener(mouseListener);
            }
        }
    }

    public void clearGrid() {
        for (JLabel[] jLabels : grid) {
            for (JLabel jLabel : jLabels) {
                jLabel.setIcon(emptyIcon);
            }
        }
    }

    // code to create blank icon or disk icon of color of choice
    private Icon createIcon(Color color) {
        BufferedImage img = new BufferedImage(CELL_WIDTH, CELL_WIDTH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(color);
        int gap = 2;
        g2.fillOval(gap, gap, CELL_WIDTH - 2 * gap, CELL_WIDTH - 2 * gap);
        g2.dispose();
        return new ImageIcon(img);
    }

    private static void createAndShowGui() {
        ColoredOvals mainPanel = new ColoredOvals();

        JFrame frame = new JFrame("ColoredOvals");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Option 2 -- if you want more free-form motion of the disk, then:

  • Add a MouseListener and MouseMotionListener to a JPanel itself
  • Within this combination listener (use a MouseAdapter for both), change the values held by two int fields, say centerX and centerY, and call repaint();
  • Override the paintComponent method, taking care to call the super's method in your override
  • In the override, paint the disk at the location specified by the fields changed by the mouse listeners.

For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class ColoredOvals2 extends JPanel {
    public static final int CELL_WIDTH = 50;
    public static final int SIDE = 8;
    private static final Color BG = Color.WHITE;
    private static final Color DISK_COLOR = Color.BLUE;
    private int centerX = 0;
    private int centerY = 0;

    public ColoredOvals2() {
        setBackground(BG);
        MouseAdapter myMouse = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                moveDisk(e);
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                moveDisk(e);
            }

            private void moveDisk(MouseEvent e) {
                centerX = e.getX();
                centerY = e.getY();
                repaint();
            }
        };
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        int w = SIDE * CELL_WIDTH;
        int h = w;
        return new Dimension(w, h);
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(DISK_COLOR);
        int x = centerX - CELL_WIDTH / 2;
        int y = centerY - CELL_WIDTH / 2;
        int w = CELL_WIDTH;
        int h = CELL_WIDTH;
        g2.fillOval(x, y, w, h);
    }

    private static void createAndShowGui() {
        ColoredOvals2 mainPanel = new ColoredOvals2();

        JFrame frame = new JFrame("ColoredOvals2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM