I am not able to dynamically repaint() inside the Jframe.
public static BufferedImage createBufferedImage(BufferedImage image)
{
ColorModel cm = image.getColorModel();
boolean premultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = image.copyData(image.getRaster());
return new BufferedImage(cm, raster, premultiplied, null);
}
public static void main(String[] args) {
BufferedImage img = new BufferedImage(old_width_i, old_height_i, BufferedImage.TYPE_INT_RGB);
img=createBufferedImage(img_white_screen);
JFrame frame=new JFrame();
JLabel label = new JLabel(new ImageIcon(img));
frame.getContentPane().add(label, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
img.flush();
img=createBufferedImage(img_black_screen);
// frame.removeAll();
// frame.revalidate();
// label.removeAll();
// label = new JLabel(new ImageIcon(img));
// frame.getContentPane().add(label, BorderLayout.WEST);
frame.repaint();
}
It basically, creates a screen with the first assignment to the "img" (i.e. img_white_screen) variable and does not change to the second assignment i.e. img_black_screen
don't to use Thread.sleep(1000);, this code line lock Event Dispatch Thread,
no idea for why reason you need to pause code execution for one second, use Swing Timer instead
for showing the Image in Java to use
XxxImage as Icon /IconImage to JLabel
paintComponent() for JComponent or JPanel
don't paint XxxImage or Icon / ImageIcon directly to the JFrame, use JPanel / JComponent or JLabel
Related
I did exactly according to the excellent guide but it does not work, I want to click a button that will change the background of the program. I would love to make the picture change
Code Guide https://stackhowto.com/how-to-set-background-image-in-java-swing/
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Display an image in the background");
final ImageIcon icon = new ImageIcon("background.png");
JTextArea text = new JTextArea()
{
Image img = icon.getImage();
// instance initializer
{setOpaque(false);}
public void paintComponent(Graphics graphics)
{
graphics.drawImage(img, 0, 0, this);
super.paintComponent(graphics);
}
};
JScrollPane pane = new JScrollPane(text);
Container content = frame.getContentPane();
content.add(pane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(3);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
I need to put a menu in a game, so I've created a frame called menuFrame and a panel called menuPanel. I've been able to get a button and a label with text to appear on this panel, but I can't get an image to display.
Here is the bulk of my code:
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel);
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
} catch (IOException e) {
e.printStackTrace();
}
I've been messing with it for a very long time, and the image simply wont appear. I've tried using just ImageIcon and no BufferedImage and that didn't work. I put the image in the same package as the class.
You could do something like this:
ImageIcon imgIcon = new ImageIcon("background2.png");
JLabel menuLabel = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(imgIcon);
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel); //This might not work, try the name of the JFrame instance
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
this.revalidate() //Use the name of the JFrame if this is not a JFrame or if this.add(menuPanel); doesnt work
} catch (IOException e) {
e.printStackTrace();
}
I build a game in which I want to add a buffered image on existing background image icon, My code is for this game is:
public class Hunter extends JFrame {
JPanel panel = new JPanel();
JLabel label = new JLabel(new ImageIcon("F:\\workspace\\HunterGame\\src\\huntergame\\background.jpg"));
BufferedImage image;
Graphics g;
Hunter(){
setSize(961, 694);
setResizable(false);
setTitle("Hunter");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
panel.add(label);
add(panel);
addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent me) {
}
#Override
public void mousePressed(MouseEvent me) {
int x = me.getX();
int y = me.getY();
if((x>11&&x<69)&&(y>26&&y<69)){
dispose();
new menu();
}
}
#Override
public void mouseReleased(MouseEvent me) {
}
#Override
public void mouseEntered(MouseEvent me) {
}
#Override
public void mouseExited(MouseEvent me) {
}
});
try {
image = ImageIO.read(new File("F:\\workspace\\HunterGame\\src\\huntergame\\hunter.png"));
} catch (IOException ex) {
System.out.println("Exception Occurs");
}
JLabel label1 = new JLabel(new ImageIcon(image));
label.add(label1);
revalidate();
}
}
I also tried Graphics.drawImage() method but this didn't work.
My background image is on JLabel and label is on frame and for second image label1 is on label but I also tried this on JPanel and no result showing.
Output Screen show only background image.
Please help me out in this.
Regards.
If you want to add a second label to the background label then you need to set the layout manager of the label.
The basic code would be something like:
JLabel background = new JLabel(....);
background.setLayout( new FlowLayout() );
JLabel foreground = new JLabel(...);
background.add( foreground );
frame.add(background, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
public class Main{
public static void main(String []args){
JLabel c=new JLabel();
c.setIcon(new ImageIcon("picture.png"));
JFrame frame = new JFrame();
frame.setBackground(Color.WHITE);
frame.setUndecorated(true);
frame.getContentPane().add(c);
frame.pack();
BufferedImage bi = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bi.createGraphics();
c.print(graphics);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
graphics.dispose();
frame.dispose();
}
}
Hi all! I am simply trying to print an image without any frame onto the screen. This code should, I think, print the image to the screen; wait two seconds and then dispose of it. What am I doing wrong?
B.T.W I get no errors whatsoever, the program just stays alive for 2 seconds then dies.
Your image is in your JLabel. Why should it be printed on your screen if the frame where JLabel is is not showing?
You are already setting the frame undecorated. Setting visible on the frame, will work.
You don't need the Graphics part at the end and also you forgot to call setVisible(true);
public class Main{
public static void main(String []args){
JLabel c=new JLabel();
c.setIcon(new ImageIcon("picture.png"));
JFrame frame = new JFrame();
frame.setBackground(Color.WHITE);
frame.setUndecorated(true);
frame.getContentPane().add(c);
frame.pack();
frame.setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
frame.dispose();
}
}
So for fun I've been working on developing this simple stock-chart GUI, which grabs charts from YahooFinance and displays them into a tabbed-Jpanel. I've been able to get the tabs to populate with user-defined stocks and all. However, I've developed some buttons that allow one to query different chart aspects (boll bands, moving averages, etc.) and would like the be able to "redraw" the panel with this update chart.
Problem: I'm not sure how to access the individual panels that I create via the method below. I need to be able to select a panel (say panel1 created when i=1 below) and have it update in an ActionListener. I'm really just wondering how Java defines these panels in the loop so I can access them later and redraw the label! Cheers.
public static void urlstock(String options,final String[] s, final JFrame f,final
JTabbedPane tpane) throws IOException{
for(int i=0;i<s.length;i++){
String path = "http://chart.finance.yahoo.com/z?s="+s[i]+options;
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel=new JPanel();
tpane.addTab(s[i],null, panel);
panel.add(label);
}
So I've tried this which cues on a button press, but it doesn't work because it doesn't recognize panel as variable for a reason that is beyond my understanding:
public void actionPerformed(ActionEvent e)
{ //Execute when button is pressed
System.out.println("MAButton");
tpane.getComponentAt(1);
tpane.remove(panel);
//Container.remove();
String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=7m&z=l&q=l&a=ss,sfs";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel=new JPanel();
tpane.setComponentAt(1,panel);
panel.add(label);
}
Grab an index of the tab in question using it's name (s[i]) JTabbedPane#indexOfTab(String)
Get a reference to the component at the retrieved index (JTabbedPane#getComponentAt(int))
Remove the contents of the retrieved component Container#removeAll()
Add the new label....
UPDATED with Example
public class TestTabPane {
public static void main(String[] args) {
new TestTabPane();
}
public TestTabPane() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JTabbedPane tabPane = new JTabbedPane();
JPanel panel = new JPanel();
JButton updateButton = new JButton("Update me");
panel.add(updateButton);
updateButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int indexOfTab = tabPane.indexOfTab("Testing");
JPanel panel = (JPanel) tabPane.getComponentAt(indexOfTab);
panel.removeAll();
panel.add(new JLabel("I've begin updated"));
panel.repaint();
}
});
tabPane.addTab("Testing", panel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(tabPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}