好的,我对Java很新,并且在分配给我的项目上遇到了问题。我无法在整个JFrame中移动我的CTetrimino对象 这是我的CTetrimino课程。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
public class CTetriMino {
public CTetriMino(int type, int x, int y, int w, int h, Color c)
{
CMino m = new CMino();
d = m.getDiameter();
Type=type;
X=x;
Y=y;
Width=w;
Height=h;
FillColor=c;
}
public CTetriMino(CTetriMino src) { // copy constructor
Type=src.Type;
X=src.X;
Y=src.Y;
Width=src.Width;
Height=src.Height;
FillColor=src.FillColor;
}
public int getX() { return X; }
public void setX(int x) { X=x; }
public int getY() { return Y; }
public void setY(int y) { Y=y; }
public void draw(Graphics g) {
g.setColor(FillColor);
switch (Type) {
case 0:
//g.fillOval(X, Y, Width, Height);
//break;
g.fillOval(X, Y, d, d);
g.fillOval(X+d,Y, d, d);
g.fillOval(X+d,Y-d, d, d);
g.fillOval(X+d+d,Y-d, d, d);
break;
case 1:
g.fillRect(X, Y, Width, Height);
//g.fillOval(X, Y, d, d);
//g.fillOval(X+d,Y, d, d);
//g.fillOval(X+d,Y+d, d, d);
//g.fillOval(X+d+d,Y+d, d, d);
break;
case 2:
g.fillOval(X, Y, d, d);
g.fillOval(X+d,Y, d, d);
g.fillOval(X,Y-d, d, d);
g.fillOval(X+d,Y-d, d, d);
break;
case 3:
g.fillOval(X, Y, d, d);
g.fillOval(X,Y+d, d, d);
g.fillOval(X,Y+d+d, d, d);
g.fillOval(X,Y-d, d, d);
break;
case 4:
g.fillOval(X, Y, d, d);
g.fillOval(X,Y+d, d, d);
g.fillOval(X,Y-d, d, d);
g.fillOval(X+d,Y-d, d, d);
break;
case 5:
g.fillOval(X, Y, d, d);
g.fillOval(X,Y+d, d, d);
g.fillOval(X,Y-d, d, d);
g.fillOval(X-d,Y-d, d, d);
break;
case 6:
g.fillOval(X, Y, d, d);
g.fillOval(X+d,Y, d, d);
g.fillOval(X,Y-d, d, d);
g.fillOval(X-d,Y, d, d);
break;
}
}
public boolean containPoint(int x, int y) {
switch (Type) {
case 0:
{
double a=Width/2.0;
double b=Height/2.0;
double xc=X+a;
double yc=Y+b;
System.out.println(a);
System.out.println(b);
System.out.println(xc);
System.out.println(yc);
System.out.println(((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0));
return ((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0);
}
case 1:
return (x>=X && y>=Y && x<X+Width && y<Y+Height);
}
return false;
}
private int Type;
private int X;
private int Y;
private int Width;
private int Height;
private Color FillColor;
private int d;
}
这是我在TestNewTetris类中添加到Jframe的Panel类。
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import java.util.*;
public class Panel extends JPanel
implements MouseListener, MouseMotionListener
{
private ArrayList<CTetriMino> originals;
private ArrayList<CTetriMino> duplicates;
private CTetriMino BlockToBeMoved;
private int m_nOffsetX; // difference between cursor and top-left corner
private int m_nOffsetY;
// double buffering
private Image backBuffer;
private Graphics gBackBuffer;
boolean isInitialized;
// init and register mouse event handler
public Panel()
{
isInitialized=false;
// handle mouse and mouse motion events
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
// Set up the initial state after the panel is created
void init()
{
// Initial state
duplicates = new ArrayList<CTetriMino>();
originals = new ArrayList<CTetriMino>();
Color[] colors = {Color.red, Color.green, Color.blue, Color.magenta, Color.cyan, Color.yellow, Color.orange};
int count=colors.length;
int dx=10;
int dy=30;
int gap=20;
int length=(getSize().height-2*dy-(count-1)*gap)/count;
for (int i=0; i<count; i++) {
//originals.add(new CTetriMino((i<count/2)?0:1, dx, dy+i*(length+gap), length, length, colors[i]));
System.out.println(length);
originals.add(new CTetriMino(i, dx, dy, length, length, colors[i]));
dx = dx+110;
}
BlockToBeMoved=null; // no shape selected
// create the back buffer
backBuffer = createImage(getSize().width, getSize().height);
gBackBuffer = backBuffer.getGraphics();
}
// State Presentation
public void paintComponent( Graphics g )
{
// super.paintComponent( g ); // clears drawing area
if (!isInitialized) {
isInitialized=true;
init();
}
// State Presentation, using double buffers
// First, clear the back buffer
gBackBuffer.setColor(Color.white);
gBackBuffer.clearRect(0, 0, getSize().width, getSize().height);
// draw the originals to back buffer
for (int i=0; i<originals.size(); i++) {
originals.get(i).draw(gBackBuffer);
}
// draw the duplicates to back buffer
for (int i=0; i<duplicates.size(); i++) {
duplicates.get(i).draw(gBackBuffer);
}
// copy from back buffer to front
g.drawImage(backBuffer, 0, 0, null);
g.fillRect(0, 100, 800, 4);
g.fillRect(0, 400, 800, 4);
} // end method paintComponent
// MouseListener event handlers
public void mouseClicked( MouseEvent e )
{
if (e.isMetaDown()) { // right button
for (int i=duplicates.size()-1; i>=0; i--) {
if (duplicates.get(i).containPoint(e.getX(), e.getY())) {
duplicates.remove(i);
repaint();
break;
}
}
}
}
public void mousePressed( MouseEvent e )
{
if (e.isMetaDown()) return; // ignore right button
// First, check the originals, from top down (i.e. back to front)
for (int i=duplicates.size()-1; i>=0; i--) {
CTetriMino p=duplicates.get(i);
if (p.containPoint(e.getX(), e.getY())) {
duplicates.remove(i);
duplicates.add(p); // move to the end, i.e. the top
BlockToBeMoved=p;
m_nOffsetX=e.getX()-BlockToBeMoved.getX();
m_nOffsetY=e.getY()-BlockToBeMoved.getY();
repaint();
return;
}
}
// Second, check the orginals
for (int i=originals.size()-1; i>=0; i--) {
CTetriMino p=originals.get(i);
System.out.println(p.containPoint(e.getX(), e.getY()));
if (p.containPoint(e.getX(), e.getY())) {
CTetriMino p2=new CTetriMino(p); // make a copy of p
duplicates.add(p2); // add to the end
BlockToBeMoved=p2; // p2 is selected, to be moved
m_nOffsetX=e.getX()-BlockToBeMoved.getX();
m_nOffsetY=e.getY()-BlockToBeMoved.getY();
repaint();
return;
}
}
}
public void mouseReleased( MouseEvent e )
{
BlockToBeMoved=null; // no shape selected
}
public void mouseEntered( MouseEvent e )
{
}
public void mouseExited( MouseEvent e )
{
}
public void mouseMoved( MouseEvent e )
{
}
public void mouseDragged( MouseEvent e )
{
if (e.isMetaDown()) return; // ignore right button
System.out.println(BlockToBeMoved);
if (BlockToBeMoved!=null) {
BlockToBeMoved.setX(e.getX()-m_nOffsetX);
BlockToBeMoved.setY(e.getY()-m_nOffsetY);
repaint();
}
} // end method mouseDragged
}
我会在运行时显示我的程序所执行的操作,但我的声誉还不够高。
如果我更改此代码,则底部Jframe是编译的:
case 0:
g.fillOval(X, Y, d, d);
g.fillOval(X+d,Y, d, d);
g.fillOval(X+d,Y-d, d, d);
g.fillOval(X+d+d,Y-d, d, d);
break;
到此:
case 0:
g.fillOval(X, Y, Width, Height);
break;
在两种变体中,我都可以拖动绿色方块。在第二个变体中,我可以拖动红色圆圈。但是我不能移动我的红色tetrimino。我只是想知道为什么我不能移动我的红色tetrimino。最终我希望能够移动所有的tetriminos并将它们拖到第二条黑线下以删除它们。我也想通过按下righr点击来旋转tetriminos。任何代码或建议表示赞赏。如果您需要更多信息,我会尽快提供。 感谢。
答案 0 :(得分:3)
如果我理解正确,解决方案应如下所示。 请创建如下所示的图像包。您需要两个事件来拖动标签鼠标,按下标签鼠标来执行此操作。添加图标标签应该是您的图像。
关键导入
import java.awt.Point;
<强>事件强>
private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {
initialClick = evt.getPoint();
}
private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {
int thisX = jLabel1.getLocation().x;
int thisY = jLabel1.getLocation().y;
// Determine how much the mouse moved since the initial click
int xMoved = (thisX + evt.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + evt.getY()) - (thisY + initialClick.y);
// Move picture to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
jLabel1.setLocation(X, Y);
jLabel1.repaint();
}