相关疑难解决方法(0)

在Swing中移动背景图像

你好我是编程的新手,我们有一个项目.我们创建了一个简单的赛车游戏,背景动人,但我坚持我的代码,我不知道该怎么做.游戏开始时我需要一个移动的背景请有人帮助我.我在求T_T

在此输入图像描述

这是我的代码:

public class Game extends JFrame implements KeyListener, ActionListener {

    Random s = new Random();
    int x = 0, y = 50, a = 0, b = 250, f = 900, a1 = 0, b2 = 350, a2 = 0, b3 = 150;
    int Move;
    int value;
    JLabel spriteLabel1 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel2 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel3 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel4 = new JLabel(new ImageIcon("ss.gif"));
    JLabel background = new JLabel(new ImageIcon("geren.png"));
    Timer T1 …
Run Code Online (Sandbox Code Playgroud)

java swing paint keylistener imageicon

3
推荐指数
1
解决办法
2771
查看次数

任何java包绘制简单的几何形状?

有没有人有任何建议任何lib /包绘制简单的几何,如三角形,方形?它可以保存为png格式.

java image draw

2
推荐指数
1
解决办法
2135
查看次数

Component.setBounds 调用 Component.repaint?

所以我正在制作一个游戏,我有EnemyAI以及 ,player并且它们都扩展了JPanel。世界有一个null布局,所以我用来setBounds();“移动”(实际上我只是移动世界图像)entities(playerAI) 并正确放置它们。但是当我添加(看起来像是,我测试了可能的最小数字)5 时,它repaint()完全自行调用。这会导致玩家视觉上在原地行走。添加的实体越多,间隔就越快(即 5 个实体的调用repaint()比 500 个实体的调用慢得多)。

注意:window下面的类中只是一个JFrame.

主要类别:

public class Game(){
public static boolean fighting = false;
public static void startGame(){
    WorldPanel game = new WorldPanel();
    game.setPreferredSize(new Dimension(window.getWidth(), window.getHeight()));
    PlayerPane player = new PlayerPane(32,32, "Player 1");
    game.addKeyListener(new KeyListener(){

        public void keyPressed(KeyEvent arg0) {
            if(fighting == false){
                move(player, game, arg0.isShiftDown(), arg0.getKeyCode());
            } else {
                System.out.println("Fighting …
Run Code Online (Sandbox Code Playgroud)

java swing multithreading

2
推荐指数
1
解决办法
451
查看次数

如何将BufferedImage绘制到JPanel

我试图使用某种绘制方法将精灵图像绘制到我的JPanel子类名为AnimationPanel.我创建了一个Spritesheet类,它可以生成包含工作表中所有精灵的BufferedImage [].在我的AnimationPanel类中,它实现了Runnable,我从AnimationPanel构造函数中实例化的spritesheet中获取BufferedImage [].我希望能够循环遍历此数组并将每个精灵显示到屏幕上.我该怎么做?这是我的AnimationPanel和Spritesheet类.

AnimationPanel

package com.kahl.animation;

import javax.swing.JPanel;

public class AnimationPanel extends JPanel implements Runnable {

//Instance Variables
private Spritesheet sheet;
private int currentFrame;
private Thread animationThread;
private BufferedImage image;

public AnimationPanel(Spritesheet aSheet) {
    super();
    sheet = aSheet;
    setPreferredSize(new Dimension(128,128));
    setFocusable(true);
    requestFocus();

}

public void run() {
    BufferedImage[] frames = sheet.getAllSprites();
    currentFrame = 0;
    while (true) {
        frames[currentFrame].draw(); //some implementation still necessary here
        currentFrame++;
        if (currentFrame >= frames.length) {
            currentFrame = 0;
        }
    }
}

public void addNotify() {
    super.addNotify(); …
Run Code Online (Sandbox Code Playgroud)

java swing

1
推荐指数
1
解决办法
6646
查看次数

无法使用GridBagLayout将JLabel替换为其他JLabel

我有一个程序,通过GridBagLayout布局管理器显示一个4x4的正方形网格.显示包含square.gif的16个JLabel.当点击其中一个矩形时,应该用包含图像的JLabel(例如帽子)替换它.因此,图像取代了单击的矩形.

但是,目前发生的情况是,单击的矩形有时会被替换.其他时候,矩形消失但图像不会取代它.其他时候,图像显示在之前单击过的矩形中,但仅在单击其他矩形后才显示.我在下面放置了最相关的代码.

public void displayGrid() {


    c.gridx = 0;
    c.gridy = 0;

    try {
        squareImage = ImageIO.read(this.getClass().getResource("stimulus(0).gif"));  //line 37

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JLabel squareLabel = new JLabel(new ImageIcon(squareImage));

    for(int i = 0; i < 16; i++){
        c.gridx = i % 4;
        c.gridy = i / 4;
        squareLabel = new JLabel(new ImageIcon(squareImage));
        squareLabels[i] = squareLabel;
        panel.add(squareLabels[i], c);
        squareLabels[i].addMouseListener(this);

        System.out.println(c.gridx + "" + c.gridy);

    }

    panel.validate();

}

public void mousePressed(MouseEvent e) {

    for(int …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel layout-manager gridbaglayout

1
推荐指数
1
解决办法
76
查看次数

为什么当我将图像设置到光标时,它不起作用?

我是Java的新手,我在尝试将图像设置到光标时遇到问题.我正在使用a BufferedImage,Graphics.drawImage但它只是绘制图像的颜色而不是完整的png图像.

这是我的代码:

/*The images List*/
iconsBet.add(ImageIO.read(getClass().getResource("/resources/ChipType"+ String.valueOf(maxChipBet+1) +".png")));
/*The images List*/

BufferedImage output = new BufferedImage(iconsBet.get(0).getWidth(), iconsBet.get(0).getHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics graphicsCursorIcon = output.getGraphics();

int count = 0;
for(BufferedImage icon : iconsBet)
{                
   graphicsCursorIcon.drawImage(icon, 0, count*10, null);
   count++;
}

graphicsCursorIcon.dispose();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor c = toolkit.createCustomCursor(output , new Point(mainPanel.getX(), mainPanel.getY()), "img");
mainPanel.setCursor(c);
Run Code Online (Sandbox Code Playgroud)

图像:这是我正在使用的图像组中的一个图像

该程序只绘制一个红色圆圈而不是png图像.

我已经尝试过使用所有BufferedImage类型,但是没有用.你能帮帮我吗?我需要做些什么来使它工作?

java swing image mouse-pointer

1
推荐指数
1
解决办法
261
查看次数

使用JLabels更新图像的正确方法

我正在创建一个GUI,并且是一个相当新的摇摆和awt.我正在尝试创建一个gui,在启动时,将背景设置为图像,然后使用方法创建各种幻灯片.我已经尝试过了,而且我没有附加到代码中,所以我能够同时修改和/或全新的概念.

编辑(2013年9月15日):我在幻灯片中遇到问题,似乎无法让它发挥作用.

这是我目前的代码.

public class MainFrame extends JFrame{

JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
Boolean busy;
double width;
double height;

public MainFrame() throws IOException {
    initMainframe();
}



public void initMainframe() throws IOException { 

//misc setup code, loads a default jpg as background

    setTitle("Pemin's Aura");
    busy = true;
    String backgroundDir = "resources/frame/background.jpg";

    backimg = ImageIO.read(new File(backgroundDir));
    backgroundL = new JLabel(new ImageIcon(backimg));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    refreshframe();
    setVisible(true);
    busy = false;
}
public void adjSize() { // the attempted start of a …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel jframe javax.imageio

0
推荐指数
2
解决办法
688
查看次数

如何使用drawString和drawImage为游戏实现Java swing GUI启动屏幕?

我不确定如何修复程序中的错误以及如何突出显示用户正在悬停的选项.我希望它突出显示每个位置的代码,即位置1将突出显示(作为不同的颜色)以开始游戏等.上/下会改变位置,我会用上,下,左,右改变位置.这就是我到目前为止所拥有的.目前它的漏洞和编译时我的窗口出现了:

在此输入图像描述

哪个适用于主游戏并且改变了这个标题牌,我做错了什么以及如何修复它?

TitleBoard类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;

//sound + file opening
import java.io.*;
import javax.sound.sampled.*;
public class TitleBoard extends JPanel implements ActionListener{

    private ArrayList<String> OptionList;
    private Image background;
    private ImageIcon bgImageIcon;
    private String cheatString;
    private int position;
    private Timer timer;

    public TitleBoard(){

    setFocusable(true);
    addKeyListener(new TAdapter());
    bgImageIcon = new ImageIcon("");
    background = bgImageIcon.getImage();
    String[] options = {"Start Game","Options","Quit"};
    OptionList = new ArrayList<String>();
    optionSetup(options);
    position = 1;
    timer = new Timer(8, this);
    timer.start();
    /*
      1 mod …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing awt

0
推荐指数
1
解决办法
5052
查看次数

如何快速显示常规网格?

我想显示一个400个相同大小JPanel的网格.通常的方法似乎是创建和布局所有面板,然后实际显示它们.然而,在我的应用程序中,大多数面板实际上都是隐藏的(想想"扫雷",但面板要复杂得多),所以我希望能够显示一个"空"网格,然后将面板添加到因为我需要它们.我考虑过两种方法:

  1. 使用布局管理器进行分配,并根据需要在适当的绝对坐标处添加面板.

  2. 使用布局管理器,但开始用虚拟组件填充表格,并在我去的时候用复杂的组件替换它们.

然而,使用这些方法中的任何一种,我似乎需要提前知道面板尺寸,但事实并非如此.我可以通过构建一个示例面板并测量它来解决这个问题,但这看起来相当丑陋,并且重复了一堆代码.还有其他方法可以做到这一点吗?

java swing

0
推荐指数
1
解决办法
142
查看次数

JTabbedPane:选项卡位置设置为LEFT但图标未对齐

我有一个JTabbedPane标签位置设置为LEFT.问题是每个标签中的图标不是彼此垂直对齐的.

考虑这张图:

在此输入图像描述

正如你所看到的"Editar Protocolo"(第二个选项卡)的图标不完全符合"Distribuir Protocolo"(第一个选项卡)的图标排列,这也与其他标签发生.我希望所有图标都垂直对齐到左边.

这是我用来设置标签组件的代码:

...
jtabbedPane.setTabComponentAt(1, configurarJtabbedPane("Editar Protocolo", iconEditarProtocolo));
...

public JLabel configurarJtabbedPane(String title, ImageIcon icon) {
    JLabel l = new JLabel(title);
    l.setIcon(icon);
    l.setIconTextGap(5);
    l.setHorizontalTextPosition(SwingConstants.RIGHT);
    return l;
}
Run Code Online (Sandbox Code Playgroud)

代码从标签左侧的 Q&A:JTabbedPane:图标中提取.

java icons swing alignment jtabbedpane

0
推荐指数
1
解决办法
5999
查看次数

旋转球失去锋利和颜色

 package testIDE;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Utils.MyJFrame;

public class ExampleClass {

    public static void main(String[] args) {
        JFrame ballRotate = new BallRotate();
    }
}

class BallRotate extends MyJFrame {

    ArrayList<Integer> degree = new ArrayList<Integer>();
    BufferedImage backGroundImage = getBufferedImage("testIDE/buttonDefaultImage.jpg");
    JLabel backGroundLabel = new JLabel(new ImageIcon(backGroundImage));
    BufferedImage footballImage = getBufferedImage("testIDE/Tennis_Ball.png");
    int x = 0;

    public BallRotate() {

        footballImage=getScaledImage(250, 250, footballImage);
        BufferedImage rotatedImage = footballImage;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); …
Run Code Online (Sandbox Code Playgroud)

java swing image java-2d

-1
推荐指数
1
解决办法
586
查看次数