Java 在任意位置放置形状
我开始自学Java,我的能力可能会超出我的掌握范围。不过,这是最好的学习方式,对吗 我试着玩简单的图形。我已经找到了关于如何将形状绘制到JFrame中的各种资源,并使它们能够工作,但据我所知,代码总是需要进入paint方法内部,该方法会自动调用,这意味着我不知道如何传递它的参数 我要做的是编写一些代码,将坐标作为参数,然后在坐标处放置一个简单的形状(让我们使用10x10像素的正方形)。我确信这一定是可能的,但我一生都不知道该怎么做 迄今为止的代码,包括@resueman和@Mikle的帮助:Java 在任意位置放置形状,java,swing,jframe,Java,Swing,Jframe,我开始自学Java,我的能力可能会超出我的掌握范围。不过,这是最好的学习方式,对吗 我试着玩简单的图形。我已经找到了关于如何将形状绘制到JFrame中的各种资源,并使它们能够工作,但据我所知,代码总是需要进入paint方法内部,该方法会自动调用,这意味着我不知道如何传递它的参数 我要做的是编写一些代码,将坐标作为参数,然后在坐标处放置一个简单的形状(让我们使用10x10像素的正方形)。我确信这一定是可能的,但我一生都不知道该怎么做 迄今为止的代码,包括@resueman和@Mikle的帮助: p
public class Robots {
public static void main(String[] args) {
Window window = new Window();
window.Window();
new PlayArea();
Point p = new Point(50,50);
Entity player1 = new Entity();
player1.Entity(p);
}
}
public class Window extends JFrame{
int x;
int y;
public void Window(){
Window window = new Window();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Robots");
window.setSize(800,600);
window.getContentPane().add(new PlayArea());
window.setLocationRelativeTo(null);
window.setBackground(Color.white);
window.setVisible(true);
}
}
public class PlayArea extends JComponent{
// I keep all added and displayed entities here for easier access
public final List<Entity> entities = new ArrayList<Entity> ();
public PlayArea(){
super();
}
public void addEntity(final Entity entity){
//add new entity and repaint area
entities.add(entity);
}
@Override
protected void paintComponent (final Graphics g)
{
super.paintComponent (g);
// Painting entities
final Graphics2D g2d = (Graphics2D) g;
for (final Entity entity : entities)
{
g2d.setPaint ( Color.BLACK );
g2d.fill (getEntityShape (entity));
}
}
protected Shape getEntityShape ( final Entity entity )
{
// Simple entity shape, you can replace it with any other shape
return new Rectangle ( entity.entPos.x - 5, entity.entPos.y - 5, 10, 10 );
}
}
public class Entity extends JComponent{
protected Point entPos = null;
public void Entity(Point p){
entPos = p;
repaint();
}
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
if (entPos != null){
g.fillRect(entPos.x, entPos.y, 10,10);
}
}
}
公共类机器人{
公共静态void main(字符串[]args){
窗口=新窗口();
window.window();
新游乐场();
点p=新点(50,50);
实体player1=新实体();
玩家1.实体(p);
}
}
公共类窗口扩展了JFrame{
int x;
int-y;
公共作废窗口(){
窗口=新窗口();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle(“机器人”);
设置窗口大小(800600);
window.getContentPane().add(新游戏区());
window.setLocationRelativeTo(空);
窗户。背景(颜色。白色);
window.setVisible(true);
}
}
公共类游乐场扩展JComponent{
//我将所有添加和显示的实体保存在此处,以便于访问
公共最终列表实体=新的ArrayList();
公众游乐场(){
超级();
}
公共无效附加值(最终实体){
//添加新实体并重新绘制区域
实体。添加(实体);
}
@凌驾
受保护组件(最终图形g)
{
超级组分(g);
//绘画实体
最终图形2d g2d=(图形2d)g;
for(最终实体:实体)
{
g2d.setPaint(颜色:黑色);
g2d.fill(getEntityShape(entity));
}
}
受保护形状getEntityShape(最终实体)
{
//简单实体形状,可以用任何其他形状替换它
返回新矩形(entity.entPos.x-5,entity.entPos.y-5,10,10);
}
}
公共类实体扩展JComponent{
保护点entPos=null;
公共无效实体(p点){
entPos=p;
重新油漆();
}
@凌驾
公共组件(图形g){
超级组件(g);
如果(entPos!=null){
g、 fillRect(入口位置x,入口位置y,10,10);
}
}
}
我希望能够在实体类中创建一个对象,并将其放置在窗口中的x,y坐标处
我最终会希望能够移动实体,但我会在一开始就知道如何绘制它们的时候解决这个问题
仍然没有在窗户上画任何东西。不过我可能错过了一些非常明显的东西 如果要独立于演示/渲染进行绘制,可以在一个图形上进行绘制,例如。您可以使用
getGraphics()
和createGraphics()
方法获取与buffereImage
关联的Graphics
对象
如果以后您想显示它,您可以使用
JComponent.paintComponent()
中的Graphics.drawImage()
方法来显示它。如果您想独立于演示/渲染进行绘制,您可以在一个示例上进行绘制。您可以使用getGraphics()
和createGraphics()
方法获取与buffereImage
关联的Graphics
对象
如果以后您想显示它,可以使用
JComponent.paintComponent()
中的Graphics.drawImage()
方法来解决您的任务。以下是我想到的前两个选项:
实体
对象
这将是一个容易和快速的实施。
实现实体拖动也不是什么大问题。这里还有几个选项可以选择如何进行绘画。如果您只需要在区域上绘制一些简单的元素,并且不想使代码过于复杂,那么这种方法是很好的实体
,并使用布局管理器放置它们
这很难实现,但由于Swing重新绘制优化,它将使用更少的资源,并且绘制速度更快。如果您的目标是大量的元素或大面积,这种方法会更好。但这对你的案子来说可能是杀伤力太大了公共类SingleComponent扩展JFrame
{
publicsinglesscomponent()抛出HeadlessException
{
超级();
片名(“机器人”);
挫折地面(颜色:白色);
//将自定义组件添加到框架中
getContentPane().add(newEntitiesArea());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(800600);
setLocationRelativeTo(空);
setVisible(真);
}
公共类实体
{
int x;
int-y;
公共实体(最终整数x,最终整数y)
{
这个.x=x;
这个。y=y;
}
}
公共类实体区域扩展JComponent
{
//我将所有添加和显示的实体保存在此处,以便于访问
公共最终列表实体=新的ArrayList();
公共实体区()
{
超级();
public class SingleComponent extends JFrame
{
public SingleComponent () throws HeadlessException
{
super ();
setTitle ( "Robots" );
setBackground ( Color.white );
// Adding our custom component into the frame
getContentPane ().add ( new EntitiesArea () );
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
setSize ( 800, 600 );
setLocationRelativeTo ( null );
setVisible ( true );
}
public class Entity
{
int x;
int y;
public Entity ( final int x, final int y )
{
this.x = x;
this.y = y;
}
}
public class EntitiesArea extends JComponent
{
// I keep all added and displayed entities here for easier access
public final List<Entity> entities = new ArrayList<Entity> ();
public EntitiesArea ()
{
super ();
// Adding mouse adapter that will add/drag entities
final MouseAdapter mouseAdapter = new MouseAdapter ()
{
// Currently dragged entity
private Entity dragged = null;
@Override
public void mousePressed ( final MouseEvent e )
{
// Looking for entity under mouse
Entity underPoint = null;
for ( final Entity entity : entities )
{
if ( getEntityShape ( entity ).contains ( e.getPoint () ) )
{
underPoint = entity;
break;
}
}
// Either dragging existing entity or adding new one
if ( underPoint != null )
{
dragged = underPoint;
}
else
{
addEntity ( new Entity ( e.getX (), e.getY () ) );
}
}
@Override
public void mouseDragged ( final MouseEvent e )
{
if ( dragged != null )
{
// Change entity coordinate and repaint area
dragged.x = e.getX ();
dragged.y = e.getY ();
repaint ();
}
}
@Override
public void mouseReleased ( final MouseEvent e )
{
if ( dragged != null )
{
dragged = null;
}
}
};
addMouseListener ( mouseAdapter );
addMouseMotionListener ( mouseAdapter );
}
public void addEntity ( final Entity entity )
{
// Add new entity and repaint area
entities.add ( entity );
repaint ();
}
@Override
protected void paintComponent ( final Graphics g )
{
super.paintComponent ( g );
// Painting entities
final Graphics2D g2d = ( Graphics2D ) g;
for ( final Entity entity : entities )
{
g2d.setPaint ( Color.BLACK );
g2d.fill ( getEntityShape ( entity ) );
}
}
protected Shape getEntityShape ( final Entity entity )
{
// Simple entity shape, you can replace it with any other shape
return new Rectangle ( entity.x - 20, entity.y - 20, 40, 40 );
}
}
public static void main ( final String[] args )
{
new SingleComponent ();
}
}