Initial paint in window coordinates, subsequent in panel coordinates. How to keep it in all in panel?Graphics Context misaligned on first paintHow do I declare and initialize an array in Java?JTable Won't Show On JPanelGetting problem in accessing web camHow to directly initialize a HashMap (in a literal way)?problem in setting scrollpane for canvasJPanel added but not displayed “in time”Drawing an image in JScrollPane within scaleAccessing class variables from ActionListener methodHow does double buffering in applets work?How to use getWidth and getHeight outside of the buildGUI method?

apt-file regex: find multiple packages at once using or

Calculate Landau's function

Can UV radiation be safe for the skin?

German equivalent to "going down the rabbit hole"

I failed to respond to a potential advisor

A vector is defined to have a magnitude and *a* direction, but the zero vector has no *single* direction. So, how is the zero vector a vector?

How does the search space affect the speed of an ILP solver?

ELI5 what is SMTChecker?

Heavy Box Stacking

What's the origin of the concept of alternate dimensions/realities?

Heuristic argument for the Riemann Hypothesis

Tikz: Draw simplified BLE-Stack

Does the Freedom of Movement spell prevent petrification by the Flesh to Stone spell?

Can a level 20 Berserker barbarian use the Frenzy feature all day with one use?

Is the following statement true: two real numbers a and b are equal iff for every ε > 0, |a − b| < ε.

Should a TA point out a professor's mistake while attending their lecture?

A truncated alternating sum of product of binomial terms

When you have to wait for a short time

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Can you use Apple Care+ without any checks (bringing just MacBook)?

'Horseshoes' for Deer?

Resources to learn about firearms?

Why don't 3D printer heads use ceramic inner walls?

Sum and average calculator



Initial paint in window coordinates, subsequent in panel coordinates. How to keep it in all in panel?


Graphics Context misaligned on first paintHow do I declare and initialize an array in Java?JTable Won't Show On JPanelGetting problem in accessing web camHow to directly initialize a HashMap (in a literal way)?problem in setting scrollpane for canvasJPanel added but not displayed “in time”Drawing an image in JScrollPane within scaleAccessing class variables from ActionListener methodHow does double buffering in applets work?How to use getWidth and getHeight outside of the buildGUI method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















In the following program I'm putting some images in a panel and performing transformations on them relative to that panel. However, when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel. in addition to being in the wrong place, all images are stacked on top of each other. How do I avoid this? All subsequent repaints are correct but not the initial setting.



I've already tried repainting the panels in the main method after setting the window visible.



I'm attempting to jerry-rig a solution by creating a boolean flag that will change when the ActionListener is triggered. This will allow me to change the behaviour of the paintComponent() method when initially run and I can just use the window coordinate system, but this is incredibly inelegant and I suspect there is a better solution.



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.*;
public class GraphicsCreation extends JFrame
static int frame;
static JPanel images;
static final BufferedImage[] IMAGE = Image.getImage(Image.imageArr1), Image.getImage(Image.imageArr2), Image.getImage(Image.imageArr3);
boolean built = false;
public GraphicsCreation()
final int MAXFRAMES = 5;
JPanel navBar = new JPanel();
navBar.setLayout(new BorderLayout());
JButton next = new JButton("NEXT >>");
JLabel label = new JLabel(" ", SwingConstants.CENTER);
frame = 1;
ActionListener listener = new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
frame++;
for (Component image : images.getComponents())
image.repaint();


;
next.addActionListener(listener);
images = new JPanel();
images.setLayout(new GridLayout(0, 2));
JPanel imagePanel1 = new ImagePanel(0);
JPanel imagePanel2 = new ImagePanel(1);
images.add(imagePanel1);
images.add(imagePanel2);
images.validate();
this.setLayout(new BorderLayout());
this.add(next, BorderLayout.NORTH);
this.add(images, BorderLayout.CENTER);


public static void main(String args[])
GraphicsCreation gc = new GraphicsCreation();
gc.setMinimumSize(new Dimension(400, 300));
gc.setLocationRelativeTo(null);
gc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gc.setResizable(false);
gc.setVisible(true);


class ImagePanel extends JPanel

AffineTransform currentTransform;
int img;

public ImagePanel(int img)
super();
this.img = img;


@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
//applyWindowToViewportTransformation(g2, -75, 75, -75, 75, true);
double width = getWidth();
double height = getHeight();
switch (frame)
case 1:
currentTransform = new AffineTransform();
g2.setTransform(currentTransform);
break;
case 2:
g2.setTransform(currentTransform);
g2.translate((-5), 7);
break;
case 3:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(-45), width / 2, height / 2);
break;
case 4:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(90), width / 2, height / 2);
break;
case 5:
g2.setTransform(currentTransform);
g2.translate((width - width * 2) / 2, (height - height * 0.5) / 2);
g2.scale(2, 0.5);
break;

currentTransform = g2.getTransform();
g2.translate(width / 2 - (IMAGE[img].getWidth() / 2), height / 2 - (IMAGE[img].getHeight() / 2));
System.out.print(g2.getTransform().getTranslateX() + " ");
System.out.println(g2.getTransform().getTranslateY());
g2.drawImage(IMAGE[img], 0, 0, this);






How the program looks when initially run:



enter image description here



How the program SHOULD look when run:



enter image description here



The correct look can be reach by hitting the next button 5 times.



Here's the Image class if you want it, but it should only be relevant in creating a buffered image to work with.



import java.awt.Color;
import java.awt.image.BufferedImage;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Dylan Veraart
*/
public class Image
private static int bg = Color.WHITE.getRGB();
private static int fg1 = Color.MAGENTA.getRGB();
static String name1 = "Rectangle";
static int[][] imageArr1 =
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg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,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg
;
public static BufferedImage getImage(int[][] arr)
BufferedImage image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 25; x++)
for (int y = 0; y < 25; y++)
image.setRGB(x, y, arr[y][x]);
// End for y.
// End for x.
return image;











share|improve this question


























  • This "might" be related to this question

    – MadProgrammer
    Mar 28 at 0:40











  • If it is related, I don't have the slightest clue how.

    – Dylan Veraart
    Mar 28 at 1:02











  • As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

    – MadProgrammer
    Mar 28 at 1:05











  • I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

    – Dylan Veraart
    Mar 28 at 1:35











  • @DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

    – c0der
    Mar 28 at 6:40


















1















In the following program I'm putting some images in a panel and performing transformations on them relative to that panel. However, when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel. in addition to being in the wrong place, all images are stacked on top of each other. How do I avoid this? All subsequent repaints are correct but not the initial setting.



I've already tried repainting the panels in the main method after setting the window visible.



I'm attempting to jerry-rig a solution by creating a boolean flag that will change when the ActionListener is triggered. This will allow me to change the behaviour of the paintComponent() method when initially run and I can just use the window coordinate system, but this is incredibly inelegant and I suspect there is a better solution.



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.*;
public class GraphicsCreation extends JFrame
static int frame;
static JPanel images;
static final BufferedImage[] IMAGE = Image.getImage(Image.imageArr1), Image.getImage(Image.imageArr2), Image.getImage(Image.imageArr3);
boolean built = false;
public GraphicsCreation()
final int MAXFRAMES = 5;
JPanel navBar = new JPanel();
navBar.setLayout(new BorderLayout());
JButton next = new JButton("NEXT >>");
JLabel label = new JLabel(" ", SwingConstants.CENTER);
frame = 1;
ActionListener listener = new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
frame++;
for (Component image : images.getComponents())
image.repaint();


;
next.addActionListener(listener);
images = new JPanel();
images.setLayout(new GridLayout(0, 2));
JPanel imagePanel1 = new ImagePanel(0);
JPanel imagePanel2 = new ImagePanel(1);
images.add(imagePanel1);
images.add(imagePanel2);
images.validate();
this.setLayout(new BorderLayout());
this.add(next, BorderLayout.NORTH);
this.add(images, BorderLayout.CENTER);


public static void main(String args[])
GraphicsCreation gc = new GraphicsCreation();
gc.setMinimumSize(new Dimension(400, 300));
gc.setLocationRelativeTo(null);
gc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gc.setResizable(false);
gc.setVisible(true);


class ImagePanel extends JPanel

AffineTransform currentTransform;
int img;

public ImagePanel(int img)
super();
this.img = img;


@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
//applyWindowToViewportTransformation(g2, -75, 75, -75, 75, true);
double width = getWidth();
double height = getHeight();
switch (frame)
case 1:
currentTransform = new AffineTransform();
g2.setTransform(currentTransform);
break;
case 2:
g2.setTransform(currentTransform);
g2.translate((-5), 7);
break;
case 3:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(-45), width / 2, height / 2);
break;
case 4:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(90), width / 2, height / 2);
break;
case 5:
g2.setTransform(currentTransform);
g2.translate((width - width * 2) / 2, (height - height * 0.5) / 2);
g2.scale(2, 0.5);
break;

currentTransform = g2.getTransform();
g2.translate(width / 2 - (IMAGE[img].getWidth() / 2), height / 2 - (IMAGE[img].getHeight() / 2));
System.out.print(g2.getTransform().getTranslateX() + " ");
System.out.println(g2.getTransform().getTranslateY());
g2.drawImage(IMAGE[img], 0, 0, this);






How the program looks when initially run:



enter image description here



How the program SHOULD look when run:



enter image description here



The correct look can be reach by hitting the next button 5 times.



Here's the Image class if you want it, but it should only be relevant in creating a buffered image to work with.



import java.awt.Color;
import java.awt.image.BufferedImage;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Dylan Veraart
*/
public class Image
private static int bg = Color.WHITE.getRGB();
private static int fg1 = Color.MAGENTA.getRGB();
static String name1 = "Rectangle";
static int[][] imageArr1 =
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,b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3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,fg3,fg3,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,fg3,fg3,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg
;
public static BufferedImage getImage(int[][] arr)
BufferedImage image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 25; x++)
for (int y = 0; y < 25; y++)
image.setRGB(x, y, arr[y][x]);
// End for y.
// End for x.
return image;











share|improve this question


























  • This "might" be related to this question

    – MadProgrammer
    Mar 28 at 0:40











  • If it is related, I don't have the slightest clue how.

    – Dylan Veraart
    Mar 28 at 1:02











  • As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

    – MadProgrammer
    Mar 28 at 1:05











  • I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

    – Dylan Veraart
    Mar 28 at 1:35











  • @DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

    – c0der
    Mar 28 at 6:40














1












1








1








In the following program I'm putting some images in a panel and performing transformations on them relative to that panel. However, when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel. in addition to being in the wrong place, all images are stacked on top of each other. How do I avoid this? All subsequent repaints are correct but not the initial setting.



I've already tried repainting the panels in the main method after setting the window visible.



I'm attempting to jerry-rig a solution by creating a boolean flag that will change when the ActionListener is triggered. This will allow me to change the behaviour of the paintComponent() method when initially run and I can just use the window coordinate system, but this is incredibly inelegant and I suspect there is a better solution.



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.*;
public class GraphicsCreation extends JFrame
static int frame;
static JPanel images;
static final BufferedImage[] IMAGE = Image.getImage(Image.imageArr1), Image.getImage(Image.imageArr2), Image.getImage(Image.imageArr3);
boolean built = false;
public GraphicsCreation()
final int MAXFRAMES = 5;
JPanel navBar = new JPanel();
navBar.setLayout(new BorderLayout());
JButton next = new JButton("NEXT >>");
JLabel label = new JLabel(" ", SwingConstants.CENTER);
frame = 1;
ActionListener listener = new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
frame++;
for (Component image : images.getComponents())
image.repaint();


;
next.addActionListener(listener);
images = new JPanel();
images.setLayout(new GridLayout(0, 2));
JPanel imagePanel1 = new ImagePanel(0);
JPanel imagePanel2 = new ImagePanel(1);
images.add(imagePanel1);
images.add(imagePanel2);
images.validate();
this.setLayout(new BorderLayout());
this.add(next, BorderLayout.NORTH);
this.add(images, BorderLayout.CENTER);


public static void main(String args[])
GraphicsCreation gc = new GraphicsCreation();
gc.setMinimumSize(new Dimension(400, 300));
gc.setLocationRelativeTo(null);
gc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gc.setResizable(false);
gc.setVisible(true);


class ImagePanel extends JPanel

AffineTransform currentTransform;
int img;

public ImagePanel(int img)
super();
this.img = img;


@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
//applyWindowToViewportTransformation(g2, -75, 75, -75, 75, true);
double width = getWidth();
double height = getHeight();
switch (frame)
case 1:
currentTransform = new AffineTransform();
g2.setTransform(currentTransform);
break;
case 2:
g2.setTransform(currentTransform);
g2.translate((-5), 7);
break;
case 3:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(-45), width / 2, height / 2);
break;
case 4:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(90), width / 2, height / 2);
break;
case 5:
g2.setTransform(currentTransform);
g2.translate((width - width * 2) / 2, (height - height * 0.5) / 2);
g2.scale(2, 0.5);
break;

currentTransform = g2.getTransform();
g2.translate(width / 2 - (IMAGE[img].getWidth() / 2), height / 2 - (IMAGE[img].getHeight() / 2));
System.out.print(g2.getTransform().getTranslateX() + " ");
System.out.println(g2.getTransform().getTranslateY());
g2.drawImage(IMAGE[img], 0, 0, this);






How the program looks when initially run:



enter image description here



How the program SHOULD look when run:



enter image description here



The correct look can be reach by hitting the next button 5 times.



Here's the Image class if you want it, but it should only be relevant in creating a buffered image to work with.



import java.awt.Color;
import java.awt.image.BufferedImage;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Dylan Veraart
*/
public class Image
private static int bg = Color.WHITE.getRGB();
private static int fg1 = Color.MAGENTA.getRGB();
static String name1 = "Rectangle";
static int[][] imageArr1 =
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,fg1,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,fg1,fg1,fg1,f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g
;
public static BufferedImage getImage(int[][] arr)
BufferedImage image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 25; x++)
for (int y = 0; y < 25; y++)
image.setRGB(x, y, arr[y][x]);
// End for y.
// End for x.
return image;











share|improve this question
















In the following program I'm putting some images in a panel and performing transformations on them relative to that panel. However, when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel. in addition to being in the wrong place, all images are stacked on top of each other. How do I avoid this? All subsequent repaints are correct but not the initial setting.



I've already tried repainting the panels in the main method after setting the window visible.



I'm attempting to jerry-rig a solution by creating a boolean flag that will change when the ActionListener is triggered. This will allow me to change the behaviour of the paintComponent() method when initially run and I can just use the window coordinate system, but this is incredibly inelegant and I suspect there is a better solution.



import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.*;
public class GraphicsCreation extends JFrame
static int frame;
static JPanel images;
static final BufferedImage[] IMAGE = Image.getImage(Image.imageArr1), Image.getImage(Image.imageArr2), Image.getImage(Image.imageArr3);
boolean built = false;
public GraphicsCreation()
final int MAXFRAMES = 5;
JPanel navBar = new JPanel();
navBar.setLayout(new BorderLayout());
JButton next = new JButton("NEXT >>");
JLabel label = new JLabel(" ", SwingConstants.CENTER);
frame = 1;
ActionListener listener = new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
frame++;
for (Component image : images.getComponents())
image.repaint();


;
next.addActionListener(listener);
images = new JPanel();
images.setLayout(new GridLayout(0, 2));
JPanel imagePanel1 = new ImagePanel(0);
JPanel imagePanel2 = new ImagePanel(1);
images.add(imagePanel1);
images.add(imagePanel2);
images.validate();
this.setLayout(new BorderLayout());
this.add(next, BorderLayout.NORTH);
this.add(images, BorderLayout.CENTER);


public static void main(String args[])
GraphicsCreation gc = new GraphicsCreation();
gc.setMinimumSize(new Dimension(400, 300));
gc.setLocationRelativeTo(null);
gc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gc.setResizable(false);
gc.setVisible(true);


class ImagePanel extends JPanel

AffineTransform currentTransform;
int img;

public ImagePanel(int img)
super();
this.img = img;


@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
//applyWindowToViewportTransformation(g2, -75, 75, -75, 75, true);
double width = getWidth();
double height = getHeight();
switch (frame)
case 1:
currentTransform = new AffineTransform();
g2.setTransform(currentTransform);
break;
case 2:
g2.setTransform(currentTransform);
g2.translate((-5), 7);
break;
case 3:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(-45), width / 2, height / 2);
break;
case 4:
g2.setTransform(currentTransform);
g2.rotate(Math.toRadians(90), width / 2, height / 2);
break;
case 5:
g2.setTransform(currentTransform);
g2.translate((width - width * 2) / 2, (height - height * 0.5) / 2);
g2.scale(2, 0.5);
break;

currentTransform = g2.getTransform();
g2.translate(width / 2 - (IMAGE[img].getWidth() / 2), height / 2 - (IMAGE[img].getHeight() / 2));
System.out.print(g2.getTransform().getTranslateX() + " ");
System.out.println(g2.getTransform().getTranslateY());
g2.drawImage(IMAGE[img], 0, 0, this);






How the program looks when initially run:



enter image description here



How the program SHOULD look when run:



enter image description here



The correct look can be reach by hitting the next button 5 times.



Here's the Image class if you want it, but it should only be relevant in creating a buffered image to work with.



import java.awt.Color;
import java.awt.image.BufferedImage;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Dylan Veraart
*/
public class Image
private static int bg = Color.WHITE.getRGB();
private static int fg1 = Color.MAGENTA.getRGB();
static String name1 = "Rectangle";
static int[][] imageArr1 =
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,b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,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,fg3,fg3,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,fg3,fg3,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,fg3,fg3,fg3,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,
bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg
;
public static BufferedImage getImage(int[][] arr)
BufferedImage image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 25; x++)
for (int y = 0; y < 25; y++)
image.setRGB(x, y, arr[y][x]);
// End for y.
// End for x.
return image;








java swing graphics2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 6:22









c0der

10.9k5 gold badges20 silver badges48 bronze badges




10.9k5 gold badges20 silver badges48 bronze badges










asked Mar 27 at 23:19









Dylan VeraartDylan Veraart

62 bronze badges




62 bronze badges















  • This "might" be related to this question

    – MadProgrammer
    Mar 28 at 0:40











  • If it is related, I don't have the slightest clue how.

    – Dylan Veraart
    Mar 28 at 1:02











  • As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

    – MadProgrammer
    Mar 28 at 1:05











  • I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

    – Dylan Veraart
    Mar 28 at 1:35











  • @DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

    – c0der
    Mar 28 at 6:40


















  • This "might" be related to this question

    – MadProgrammer
    Mar 28 at 0:40











  • If it is related, I don't have the slightest clue how.

    – Dylan Veraart
    Mar 28 at 1:02











  • As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

    – MadProgrammer
    Mar 28 at 1:05











  • I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

    – Dylan Veraart
    Mar 28 at 1:35











  • @DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

    – c0der
    Mar 28 at 6:40

















This "might" be related to this question

– MadProgrammer
Mar 28 at 0:40





This "might" be related to this question

– MadProgrammer
Mar 28 at 0:40













If it is related, I don't have the slightest clue how.

– Dylan Veraart
Mar 28 at 1:02





If it is related, I don't have the slightest clue how.

– Dylan Veraart
Mar 28 at 1:02













As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

– MadProgrammer
Mar 28 at 1:05





As you stated the "when the gui is first drawn it performs the initial paint operation relative to the coordinates of the window, rather than the panel" which is a similar issue I had, where the "origin" was not correct when using AffineTransform

– MadProgrammer
Mar 28 at 1:05













I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

– Dylan Veraart
Mar 28 at 1:35





I think it is indeed related. I sort of fixed the problem by initializing frame at 0 which skips the switch statement in paintComponent() during initialization. When the program runs, the first frame repeats twice but every frame is correct. I'm not really sure where the hell to go from there but...at least we've further diagnosed the problem here

– Dylan Veraart
Mar 28 at 1:35













@DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

– c0der
Mar 28 at 6:40






@DylanVeraart I did not dive into the solution linked by @MadProgrammer but I see that it uses currentTransform = g2.getTransform(); instead of currentTransform = new AffineTransform();

– c0der
Mar 28 at 6:40













0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387936%2finitial-paint-in-window-coordinates-subsequent-in-panel-coordinates-how-to-kee%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387936%2finitial-paint-in-window-coordinates-subsequent-in-panel-coordinates-how-to-kee%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript