第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Java Swing 中的背景圖像上繪制動(dòng)畫(huà)

如何在 Java Swing 中的背景圖像上繪制動(dòng)畫(huà)

夢(mèng)里花落0921 2023-04-19 16:28:31
這是我的代碼,我使用了兩張圖片,一張用于背景圖片,一張用于彈球動(dòng)畫(huà),但是使用這段代碼,球會(huì)在它的飛行軌跡中擦掉背景圖片,所以我需要一些幫助,讓我可以在transparent bufferedimage 然后將其作為背景圖像的遮罩。提前致謝。import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer;public class MainTest {    private final int TABLE_WIDTH = 300;    private final int TABLE_HEIGHT = 400;    private final int BALL_SIZE = 16;    private JFrame f = new JFrame("Pinball game");    Random rand = new Random();    private int ySpeed = 5;    private double xyRate = rand.nextDouble() - 0.5;    private int xSpeed = (int) (ySpeed * xyRate * 2);    private int ballX = rand.nextInt(200) + 20;    private int ballY = rand.nextInt(10) + 20;    private BackgroundPanel background = new BackgroundPanel();    private Foreground foreground = new Foreground();    private int preX = -1;    private int preY = -1;    BufferedImage image = new BufferedImage(TABLE_WIDTH, TABLE_HEIGHT, BufferedImage.TYPE_INT_ARGB);    Image back = new ImageIcon("res/cat1.jpg").getImage();    Graphics g = image.getGraphics();    Timer timer;    public void init() {//      Graphics2D g = image.createGraphics();//      image = g.getDeviceConfiguration().createCompatibleImage(30, 30, Transparency.TRANSLUCENT);        background.setBackground(new ImageIcon("res/cat1.jpg"));        background.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));        foreground.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));        background.add(foreground);        f.add(background);        ActionListener taskPerformer = evt -> {            if (ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE) {                xSpeed = -xSpeed;            }            else if (ballY <= 0 || (ballY >= TABLE_HEIGHT - BALL_SIZE)) {                ySpeed = -ySpeed;            }
查看完整描述

2 回答

?
萬(wàn)千封印

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊

繪制背景圖像后繪制球。

在你的情況下,在方法“processBackground”之后畫(huà)球。


查看完整回答
反對(duì) 回復(fù) 2023-04-19
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

我解決了這個(gè)問(wèn)題,并在此處發(fā)布了代碼

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.util.Random;


import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;


public class MainTest {


? ? private final int TABLE_WIDTH = 300;

? ? private final int TABLE_HEIGHT = 400;

? ? private final int BALL_SIZE = 16;

? ? private JFrame f = new JFrame("Pinball game");

? ? Random rand = new Random();

? ? private int ySpeed = 5;

? ? private double xyRate = rand.nextDouble() - 0.5;

? ? private int xSpeed = (int) (ySpeed * xyRate * 2);

? ? private int ballX = rand.nextInt(200) + 20;

? ? private int ballY = rand.nextInt(10) + 20;


? ? private BackgroundPanel background = new BackgroundPanel();? ?

? ? Timer timer;


? ? public void init() {


? ? ? ? background.setBackground(new ImageIcon("res/cat1.jpg"));

? ? ? ? background.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));


? ? ? ? f.add(background);


? ? ? ? ActionListener taskPerformer = evt -> {

? ? ? ? ? ? if (ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE) {

? ? ? ? ? ? ? ? xSpeed = -xSpeed;

? ? ? ? ? ? } else if (ballY <= 0 || (ballY >= TABLE_HEIGHT - BALL_SIZE)) {

? ? ? ? ? ? ? ? ySpeed = -ySpeed;

? ? ? ? ? ? }

? ? ? ? ? ? ballY += ySpeed;

? ? ? ? ? ? ballX += xSpeed;

? ? ? ? ? ? background.repaint();

? ? ? ? };

? ? ? ? timer = new Timer(10, taskPerformer);

? ? ? ? timer.start();

? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

? ? ? ? f.pack();

? ? ? ? f.setVisible(true);

? ? }


? ? public static void main(String[] args) {

? ? ? ? new MainTest().init();

? ? }


? ? public class BackgroundPanel extends JPanel {


? ? ? ? private static final long serialVersionUID = 6702278957072713279L;

? ? ? ? private Icon wallpaper;


? ? ? ? public BackgroundPanel() {

? ? ? ? }


? ? ? ? protected void paintComponent(Graphics g) {

? ? ? ? ? ? if (null != wallpaper) {

? ? ? ? ? ? ? ? processBackground(g);

? ? ? ? ? ? ? ? drawball(g);

? ? ? ? ? ? }

? ? ? ? ? ? System.out.println("f:paintComponent(Graphics g)");

? ? ? ? }


? ? ? ? public void setBackground(Icon wallpaper) {

? ? ? ? ? ? this.wallpaper = wallpaper;

? ? ? ? ? ? this.repaint();

? ? ? ? }


? ? ? ? private void processBackground(Graphics g) {

? ? ? ? ? ? ImageIcon icon = (ImageIcon) wallpaper;

? ? ? ? ? ? Image image = icon.getImage();

? ? ? ? ? ? int cw = getWidth();

? ? ? ? ? ? int ch = getHeight();

? ? ? ? ? ? int iw = image.getWidth(this);

? ? ? ? ? ? int ih = image.getHeight(this);

? ? ? ? ? ? int x = 0;

? ? ? ? ? ? int y = 0;

? ? ? ? ? ? while (y <= ch) {

? ? ? ? ? ? ? ? g.drawImage(image, x, y, this);

? ? ? ? ? ? ? ? x += iw;

? ? ? ? ? ? ? ? if (x >= cw) {

? ? ? ? ? ? ? ? ? ? x = 0;

? ? ? ? ? ? ? ? ? ? y += ih;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? private void drawball(Graphics g){

? ? ? ? ? ? ?g.setColor(Color.BLUE);

? ? ? ? ? ? ?g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);

? ? ? ? }

? ? }

}




查看完整回答
反對(duì) 回復(fù) 2023-04-19
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)