How to show animated GIFs? - java

First of all I am making a minigame with the Five Nights At Freddy's graphics and jump scares. I already know how to import and draw a picture (png and etc). The only one I don't know how to import, are gifs.
Here Is My Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Minigame extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
GameEvents gameEvents = new GameEvents();
Timer gameTimer = new Timer(1, gameEvents);
int i = 0;
int horizontalposition = 500;
int verticalposition = 500;
BufferedImage Picture;
BufferedImage Picture2;
BufferedImage Picture3;
BufferedImage Picture4;
BufferedImage Picture5;
BufferedImage Picture6;
//Don't forget to declare your variables!
Minigame()
{
gameTimer.start();
this.addKeyListener(gameEvents);
try
{
Picture = ImageIO.read(getClass().getResource("Child.gif"));
Picture2 = ImageIO.read(getClass().getResource("Freddycake_Gif.gif"));
Picture3 = ImageIO.read(getClass().getResource("Purple_man.png"));
Picture4 = ImageIO.read(getClass().getResource("Cake_Child_Idle.png"));
Picture5 = ImageIO.read(getClass().getResource("Cake_Child.gif"));
//The format for this is Picture = ImageIO.read(getClass().getResource("NameOfFile.typeoffile"));
}
catch (IOException e)
{
System.out.println("Pictures failed to load");
}
}
#Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,this.getWidth(), this.getHeight());
///g.drawImage(Picture, horizontalposition, verticalposition, 100, 150, null);
g.drawImage(Picture, 200, 10, 70, 100, null);
g.drawImage(Picture, 200, 100, 70, 100, null);
g.drawImage(Picture, 200, 200, 70, 100, null);
g.drawImage(Picture, 200, 300, 70, 100, null);
g.drawImage(Picture, 200, 400, 70, 100, null);
g.drawImage(Picture, 200, 500, 70, 100, null);
g.drawImage(Picture2, horizontalposition, verticalposition, 100, 150, null);
g.drawImage(Picture3, 1100, 50, 100, 150, null);
g.drawImage(Picture4, 900, 50, 100, 150, null);
//g.drawImage(Picture5, 900, 50, 100, 150, null);
}
public class GameEvents implements ActionListener, KeyListener
{
#Override
public void actionPerformed(ActionEvent arg0)
{
repaint();
}
#Override
public void keyPressed(KeyEvent key) //stuff inside here happens when a key is pressed
{
if(key.getKeyChar()=='d')
{
horizontalposition=horizontalposition+50;
}
if(key.getKeyChar()=='s')
{
verticalposition=verticalposition+50;
}
if(key.getKeyChar()=='w')
{
verticalposition=verticalposition-50;
}
if(key.getKeyChar()=='a')
{
horizontalposition=horizontalposition-50;
}
if(horizontalposition<0)
{
horizontalposition=0;
}
System.out.println(key.getKeyChar());
System.out.println('d');
}
#Override
public void keyReleased(KeyEvent arg0) {
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
public static void main(String[] args)
{
JFrame f = new JFrame("Java Graphics Example Project");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Minigame p = new Minigame();
f.setSize(1500,700);
f.add(p);
f.setVisible(true);
p.requestFocusInWindow();
}
}
Any yes, I already know how to do g.drawImage();

Look up this answer response: Show animated GIF
If you don't feel like it, I'll just put the code of the person who replied here. However, they explain it there and really go in detail.
THIS IS THE CODE FROM STACKER WHO EXPLAINED IT ON THE LINK I GAVE YOU
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("<URL to your Animated GIF>");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

Related

GIF ImageIcon keeps flickering in Java Swing

I have to do a little Game in Java Swing for school. I created some gifs for it and wanted to put them in and I did, but the problem is that the gifs flicker sometimes. I really can't find anything on the Internet and honestly I don't even know where to start to look exactly.
My code:
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Chicken extends JFrame {
private int canvasWidth = 560;
private int canvasHeight = 480;
private Color sideGreen = new Color(14, 37, 14);
private Color gapGreen = new Color(56, 148, 56);
private Color edgeGreen = new Color(49, 129, 49);
private Image chicken;
private Image cookie;
private DrawCanvas canvas;
public Chicken() {
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
try {
addImages();
} catch (Exception ex) {
ex.printStackTrace();
}
Container cp = getContentPane();
cp.add(canvas);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setTitle("Chicken");
pack();
setVisible(true);
}
private class DrawCanvas extends JPanel {
#Override
public void update(Graphics g) {
paintComponent(g);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
setDoubleBuffered(true);
setBackground(Color.BLACK);
g2.setColor(sideGreen);
g2.fillRect(0, 0, 80, canvasHeight);
g2.fillRect(canvasWidth - 80, 0, 80, 480);
g2.setColor(gapGreen);
g2.fillRect(80, 0, 400, 100);
g2.fillRect(80, canvasHeight - 100, 400, 100);
g2.setColor(edgeGreen);
g2.fillRect(80, 80, 400, 20);
g2.fillRect(80, canvasHeight - 100, 400, 20);
g2.drawImage(cookie, 85, 5, this);
g2.drawImage(chicken, 150, 120, this);
}
}
private void addImages() throws Exception {
ImageIcon iconCookie = new ImageIcon(new URL("https://i.stack.imgur.com/1PcWC.gif"));
Image cookieimage = iconCookie.getImage();
Image newimg = cookieimage.getScaledInstance(70, 70, Image.SCALE_DEFAULT);
iconCookie = new ImageIcon(newimg);
ImageIcon iconChicken = new ImageIcon(new URL("https://i.stack.imgur.com/qJA7G.gif"));
Image chickenimage = iconChicken.getImage();
Image newimgc = chickenimage.getScaledInstance(250, 250, Image.SCALE_DEFAULT);
iconChicken = new ImageIcon(newimgc);
chicken = iconChicken.getImage();
cookie = iconCookie.getImage();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Chicken());
}
}
Is there something wrong in the code? Or is there another way to do it and I've just done it wrong? The gifs should be fine, I've created them myself and they look fine when I view them from my PC.
Chicken
Cookie
Edit
Found out that the chicken isn't flickering when I remove the cookie, but I don't know why.
Remove setDoubleBuffered(true); from paintComponent. Probably temptative code. You could call it in the constructor, but by default double buffering is on. Move setBackground(Color.BLACK); to the constructor.
Then remove the update method.
You normally do not need to call super.paintComponent();. Comment it out, and try to see a change.
You might do setContentPane(canvas).
Should there still be a problem, then it is due to the inner workings of gif handling.

Jbutton issues within my code

i have been working on a flashing beacon on java using Jpanel, paint component and timers, however i am having trouble trying to get the buttons within the code to function. when the code is run, the flash button is supposed to prompt the beacon to start blinking/flashing whereas the steady button keeps it on the same colour. the alternating colours for the beacon are orange and grey.As well as this i cant seem to get rid of a button that keeps appearing in the top left of the window when the code is run. so far, this is what i have
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
/**
* Created by Enoch on 26/03/2015.
*/
class BelishaBeacon extends JPanel {
Color startLight, stopLight, color;
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
Ellipse2D firstOval = new Ellipse2D.Double(130, 70, 50, 50);
g2.draw(firstOval);
g2.fill(firstOval);
g2.setColor(Color.BLACK);
Rectangle rect1 = new Rectangle(150, 119, 10, 35);
g2.draw(rect1);
g2.fill(rect1);
g2.setColor(Color.WHITE);
Rectangle rect2 = new Rectangle(150, 150, 10, 35);
g2.draw(rect2);
g2.fill(rect2);
g2.setColor(Color.BLACK);
Rectangle rect3 = new Rectangle(150, 180, 10, 35);
g2.draw(rect3);
g2.fill(rect3);
g2.setColor(Color.WHITE);
Rectangle rect4 = new Rectangle(150, 210, 10, 35);
g2.draw(rect4);
g2.fill(rect4);
g2.setColor(Color.BLACK);
Rectangle rect5 = new Rectangle(150, 240, 10, 35);
g2.draw(rect5);
g2.fill(rect5);
g2.setColor(Color.WHITE);
Rectangle rect6 = new Rectangle(150, 270, 10, 35);
g2.draw(rect6);
g2.fill(rect6);
g2.setColor(Color.BLACK);
Rectangle rect7 = new Rectangle(150, 300, 10, 35);
g2.draw(rect7);
g2.fill(rect7);
g2.setColor(Color.WHITE);
Rectangle rect8 = new Rectangle(150, 330, 10, 35);
g2.draw(rect8);
g2.fill(rect8);
g2.setColor(Color.BLACK);
Rectangle rect9 = new Rectangle(150, 360, 10, 35);
g2.draw(rect9);
g2.fill(rect9);
g2.setColor(Color.WHITE);
Rectangle rect10 = new Rectangle(150, 390, 10, 35);
g2.draw(rect10);
g2.fill(rect10);
}
public BelishaBeacon() {
startLight = Color.ORANGE;
stopLight = Color.LIGHT_GRAY;
color = startLight;
new Blinker(this);
setBackground(Color.white);
}
public void blink()
{
color = (color == startLight ? stopLight : startLight);
repaint();
}
}
//
class Blinker
{
BelishaBeacon blinkPanel;
public Blinker(BelishaBeacon bp)
{
blinkPanel = bp;
new Timer(500, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
blinkPanel.blink();
}
}).start();
}
}
//
class BelishaBeaconViewer extends JFrame {
JButton jbtFlash = new JButton("Flash");
JButton jbtSteady = new JButton("Steady");
JPanel bPanel = new JPanel();
BelishaBeacon bBPanel = new BelishaBeacon();
public BelishaBeaconViewer() {
bPanel.add(jbtFlash);
this.add(bPanel, BorderLayout.SOUTH);
bPanel.add(jbtSteady);
this.add(bBPanel, BorderLayout.CENTER);
jbtFlash.addActionListener(new FlashListener());
jbtSteady.addActionListener(new SteadyListener());
}
class FlashListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
class SteadyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public static void main(String[] args) {
JFrame bBFrame = new BelishaBeaconViewer();
bBFrame.setTitle("Belisha Beacon");
bBFrame.setSize(300, 300);
bBFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
bBFrame.setVisible(true);
}
}
i cant seem to get rid of a button that keeps appearing in the top left of the window
//super.paintComponents(g); // typo
super.paintComponent(g); // should be
Don't start the Timer automatically.
The FlashListener should start the Timer, no need for the repaint
The SteadyListner should stop the Timer, no need for the repaint.

Swing - drawString shifting animation fails

I'm trying to make a little animation on Java swing, which should move the drawString's string a little to left(to center), however, whenever I try to do it, the program just slowly opens and just jumps to the place where It should land eventually, so seemingly, there no animation occurs.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class IntroPanel extends JPanel {
private int x = 300;
private JButton exitButton, startButton;
private JPanel buttonsPanel;
public IntroPanel()
{
setPreferredSize( new Dimension( 300, 150));
setLayout( new BorderLayout());
buttonsPanel = new JPanel();
exitButton = new JButton( "Exit" );
startButton = new JButton( "Start" );
exitButton.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
startButton.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
/**
* TODO GOES TO MAIN PANEL
*/
}
});
setBackground( new Color( 250, 250, 250) );
startButton.setPreferredSize( new Dimension(70, 40) );
exitButton.setPreferredSize( new Dimension(70, 40) );
Border padding = BorderFactory.createEmptyBorder(0, 0, 25, 0);
setBorder(padding);
buttonsPanel.setBackground( new Color( 250, 250, 250));
buttonsPanel.add(startButton);
buttonsPanel.add(exitButton);
add(buttonsPanel, BorderLayout.SOUTH);
animate();
}
public void animate()
{
for( int i = 1; i < 211; i++ )
{
x--;
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException ex) { }
}
}
public void paintComponent( Graphics page )
{
Graphics2D gra = (Graphics2D) page;
gra.setFont( new Font( "Philosopher-BoldItalic", Font.ITALIC | Font.BOLD, 50 ) );
Color start = new Color( 50, 50, 50 );
Color end = new Color( 250, 250, 250 );
GradientPaint gradient =
new GradientPaint( 120, 100, start, 270, 270, end);
gra.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gra.setPaint( gradient );
gra.drawString("Geometrica", x, 100);
}
}
Main method:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame( "Geometrica" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 250);
frame.setBackground( new Color(250, 250, 250) );
frame.setLocationRelativeTo(null);
frame.getContentPane().add( new IntroPanel() );
frame.setVisible(true);
}
}
Thank you!
Call the animate method after the frame is made visible from outside the object.
Also you need to call super.paintComponent(page); inside your animate method like it says in the Javadoc then you'll get the desired result.
Main:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame( "Geometrica" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 250);
frame.setBackground( new Color(250, 250, 250) );
frame.setLocationRelativeTo(null);
IntroPanel p=new IntroPanel();
frame.getContentPane().add(p);
frame.setVisible(true);
p.animate();
}
}
IntroPanel:
public class IntroPanel extends JPanel {
private int x = 300;
private JButton exitButton, startButton;
private JPanel buttonsPanel;
public IntroPanel()
{
setPreferredSize( new Dimension( 300, 150));
setLayout( new BorderLayout());
buttonsPanel = new JPanel();
exitButton = new JButton( "Exit" );
startButton = new JButton( "Start" );
exitButton.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
startButton.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
/**
* TODO GOES TO MAIN PANEL
*/
}
});
setBackground( new Color( 250, 250, 250) );
startButton.setPreferredSize( new Dimension(70, 40) );
exitButton.setPreferredSize( new Dimension(70, 40) );
Border padding = BorderFactory.createEmptyBorder(0, 0, 25, 0);
setBorder(padding);
buttonsPanel.setBackground( new Color( 250, 250, 250));
buttonsPanel.add(startButton);
buttonsPanel.add(exitButton);
add(buttonsPanel, BorderLayout.SOUTH);
}
public void animate()
{
super.paintComponent(page);
for( int i = 1; i < 211; i++ )
{
x--;
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException ex) { }
}
}
public void paintComponent( Graphics page )
{
Graphics2D gra = (Graphics2D) page;
gra.setFont( new Font( "Philosopher-BoldItalic", Font.ITALIC | Font.BOLD, 50 ) );
Color start = new Color( 50, 50, 50 );
Color end = new Color( 250, 250, 250 );
GradientPaint gradient =
new GradientPaint( 120, 100, start, 270, 270, end);
gra.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gra.setPaint( gradient );
gra.drawString("Geometrica", x, 100);
}
}

How to restart?

I have this code and I want the user to be able to restart the game by pressing R, but I do not know how to do this, I've tried to call the main method again, which obviously didn't work, but I don't know any other way to restart it, this is the main class code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JPanel implements KeyListener {
long startT = System.currentTimeMillis();
long elapsedTimeMillis;
float elapsedT;
private Player player;
private Stage stage;
private boolean isGameOver = false;
private EnemyManager manager;
public Main() {
setSize(800,600);
setPreferredSize(new Dimension(800,600));
setFocusable(true);
addKeyListener(this);
stage = new Stage();
player = new Player(this, 200, 500);
manager = new EnemyManager(this, 10);
}
#Override
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
if(!isGameOver){
player.draw(g);
manager.draw(g);
stage.draw(g);
long elapsedTimeMillis = System.currentTimeMillis() - startT;
elapsedT = elapsedTimeMillis/1000F;
g.setColor(Color.white);
g.setFont(new Font("Century Gothic", Font.BOLD, 24));
g.drawString("You're alive for: " + elapsedT, 400, 50);
}else {
g.setFont(new Font("Century Gothic", Font.BOLD, 55));
g.setColor(Color.RED);
g.drawString("Game Over" , 250, 150);
g.setColor(Color.ORANGE);
g.setFont(new Font("Century Gothic", Font.BOLD, 24));
g.drawString("You lasted for: ", 300, 250);
g.drawString("Press to restart", 300, 300);
g.setColor(Color.white);
g.drawString("" + elapsedT, 480, 250);
g.drawString("R", 364, 300);
}
g.dispose();
repaint();
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_W){
}
if (code == KeyEvent.VK_A){
player.setdirectionX(-1);
}
if (code == KeyEvent.VK_S){
}
if (code == KeyEvent.VK_D){
player.setdirectionX(1);
}
if (code == KeyEvent.VK_R){
}
}
public void setGameOver(boolean flag){
isGameOver = flag;
}
#Override
public void keyReleased(KeyEvent e){
player.setdirectionX(0);
player.setdirectionY(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
public Stage getStage(){
return stage;
}
public EnemyManager getEnemyManager(){
return manager;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Dodge the bloody rectangles");
frame.add(new Main());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800,600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
public void restart() {
stage = new Stage();
player = new Player(this, 200, 500);
manager = new EnemyManager(this, 10);
isGameOver = false;
}
...
#Override
public void keyPressed(KeyEvent e) {
...
if (code == KeyEvent.VK_R){
restart();
}
}

Java GUI drawing an image on mouse event

I'm making a program where hovering over a grid will place an image into the cell hovered over. Currently I have it working so that only a color is filled in, but I have no idea how to make it so an image is drawn in instead. Here's my program:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Test
{
Image img=Toolkit.getDefaultToolkit().getImage("img.gif");
public static void main(String[]args)
{
new Test();
}
public Test()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PixelGrid());
frame.setSize(new Dimension(364,357));
frame.setVisible(true);
}
public class PixelGrid extends JPanel
{
private List<Shape>grid,square;
public PixelGrid()
{
grid=new ArrayList<>();
square=new ArrayList<>();
addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
for(Shape shape:grid)
{
if(shape.contains(e.getPoint()))
square.add(shape);
}
repaint();
}
}
);
for(int row=0;row<5;row++)
{
for(int col=0;col<5;col++)
grid.add(new Rectangle(col*25+112,row*25+50,25,25));
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(112,50,237,50);
g.drawLine(112,75,237,75);
g.drawLine(112,100,237,100);
g.drawLine(112,125,237,125);
g.drawLine(112,150,237,150);
g.drawLine(112,175,237,175);
g.drawLine(112,50,112,175);
g.drawLine(137,50,137,175);
g.drawLine(162,50,162,175);
g.drawLine(187,50,187,175);
g.drawLine(212,50,212,175);
g.drawLine(237,50,237,175);
Graphics2D g2=(Graphics2D)g;
for(Shape cell:square)
g2.fill(cell);
}
}
}
I want to make the image "img" become the "pixel" that is filled in. However, I'm confused on how to do this as I don't think I can use for-each loops and Graphics2D. If anyone can help, thanks so much!
Define ImageHolder class which has 2 fields shape and image.
class ImageHolder {
Shape shape;
Image img;
public void paint(Graphics2D g2) {
if (img!=null) {
g2.drawImage(img);
}
else {
g2.fill(shape);
}
}
}
Your grid should be List. On init all the ImageHolders have squares and null images. On click image is assigned to the clicked holder.
In the paintComponent() you just call the holder's paint() method
take a look at this one.
all you need to do is repalce your g2.fill(cell); with g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
read drawimage api here
for (Shape cell : square) {
//g2.fill(cell);
g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
System.out.println(cell.getBounds().x);
}
however use Dimention list or 2D list instead Shape in this case
complete code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Test {
Image img = Toolkit.getDefaultToolkit().getImage("img.gif");
public static void main(String[] args) {
new Test();
}
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PixelGrid());
frame.setSize(new Dimension(364, 357));
frame.setVisible(true);
}
public class PixelGrid extends JPanel {
int x=0;
int y=0;
private List<Shape> grid, square;
public PixelGrid() {
grid = new ArrayList<>();
square = new ArrayList<>();
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
for (Shape shape : grid) {
if (shape.contains(e.getPoint())) {
square.add(shape);
}
}
repaint();
}
}
);
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
grid.add(new Rectangle(col * 25 + 112, row * 25 + 50, 25, 25));
}
}
}
public void paintComponent(Graphics g) {
System.out.println(x);
g.drawLine(112, 50, 237, 50);
g.drawLine(112, 75, 237, 75);
g.drawLine(112, 100, 237, 100);
g.drawLine(112, 125, 237, 125);
g.drawLine(112, 150, 237, 150);
g.drawLine(112, 175, 237, 175);
g.drawLine(112, 50, 112, 175);
g.drawLine(137, 50, 137, 175);
g.drawLine(162, 50, 162, 175);
g.drawLine(187, 50, 187, 175);
g.drawLine(212, 50, 212, 175);
g.drawLine(237, 50, 237, 175);
Graphics2D g2 = (Graphics2D) g;
//g2.drawImage(img,x,y,null);
for (Shape cell : square) {
//g2.fill(cell);
g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
System.out.println(cell.getBounds().x);
}
}
}
}
output>>

Categories