I was thinking to set an image as background instead of a solid color. Is that possible?
Here are the codes of my MainApp.java
package com.gabriel.guiApp;
import java.awt.Color;
import javax.swing.JFrame;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
import com.gabriel.guiImpl.Car;
public class MainApp extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
MainApp mainApp=new MainApp();
mainApp.setTitle("Draw");
mainApp.setBounds(10,10, 1200, 400);
Draw draw= new Draw();
draw.setBounds(0,0, 80,80);
draw.setBackground(Color.red);
draw.setForeground(Color.yellow);
draw.init();
mainApp.add(draw);
ClassPathResource r= new ClassPathResource("ApplicationContext.xml");
BeanFactory factory=new XmlBeanFactory((org.springframework.core.io.Resource) r);
Car car=(Car) factory.getBean("car");
draw.setCar(car);
mainApp.setVisible(true);
}
}
Please insert your solution to my code. Thank you!
I believe there's no helper method that does this for you. This type of question has been asked and answered before, so i would just share a link to the accepted answer. You could look at other examples on that thread too and then choose the one that best suits you.
https://stackoverflow.com/a/1466278/12796448
Look at this example too
https://www.tutorialspoint.com/how-to-add-background-image-to-jframe-in-java
I believe the best way to achieve what you want is to paint the background of your container with the desired image. Look at the links i shared, they might help.
Related
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.sound.midi.Patch;
import javax.swing.*;
this is were the code start
public class graficCar extends JComponent
{
private ImageIcon image1=null;
private JLabel label1=null;
private ImageIcon image2=null;
private JLabel label2=null;
graficCar(){
setLayout(new FlowLayout());
the problem here
image1=new ImageIcon(getClass().getResource("4596067.png"));
label1=new JLabel(image1);
add(label1);
}
the main
public static void main(String[] args)
{
graficCar g=new graficCar();
g.setDebugGraphicsOptions(JFrame.EXIT_ON_CLOSE);
g.setVisible(true);
}
As you mentioned, your png file is in C:\Users\user\Downloads folder. I bet class is somewhere else.
The problem is getResource() finds file in classpath by default. Here you could find details if you want to go deeper.
One solution is to put .png right next to your .java class but it's bad practice. More convenient way described here. The best practice is to create special folder for your resources in the project and then use relative path to it. How to do this is out of scope although it's described in many sites and in Stackoverflow too. Don't hesistate to use Search :)
I am trying to track all of the supplies stored in a knapsack object and create an interface that shows the supplies update using an observer/observable implementation. For some reason when I run this code, with 2 items in the knapsack, the second item updates and shows expiration date decreasing as time change is triggered. The first one does not change at all, as if it's a static label. Please could someone let me know what I did wrong? Thanks so much for your help in advance! Also, I'm super new to Java programming so please extra information/explanation would be greatly appreciated.
Here is my code:
package view;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import supplies.Supplies;
import model.Adventure;
import model.Knapsack;
public class InventoryView extends JPanel implements Observer{
private Knapsack knapsack;
private Adventure adventure;
private JLabel b;
public InventoryView(Adventure adventure) {
this.adventure=adventure;
this.knapsack=adventure.getSquad().getKnapsack();
for (Supplies supply : knapsack.getSupplies()) {
b=new JLabel(supply.toString());
add(b);
}
knapsack.addObserver(this);
}
#Override
public void update(Observable arg0, Object arg1) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (Supplies supply : knapsack.getSupplies()) {
b.setText(supply.toString());
add(b);
}
}
});
}
}
Swing is lazy when it comes to container updates (add/removes), this allows you to execute a number of add/removes in quick succession without fear that the system will grind to a halt while it attempts to update the entire container hierarchy on each call.
Call revalidate and repaint after you have added all your components. Also, make sure that your JPanel is using a layout manager capable of supporting multiple children.
You might consider using a JList or JTable instead
It's Giving me an error saying that "The method setContentPane(Container) in the type JFrame is not applicable for the arguments (GamePanel)"
Here is my Code:
package main;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args){
JFrame window = new JFrame("Dragon Tales");
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
}
}
I am following a tutorial exactly and his screen shows no errors at all.
Your GamePanel class does not extend any Swing GUI component such as Container or one of its children. Probably it should extend JPanel.
i.e.,
import javax.swing.JPanel;
public class GamePanel extends JPanel {
// .... etc
}
Please don't add the urgent or "help as soon as possible" bit. Yes your question is very important, but it is no more important than anyone else's.
Edit: Mad's link is worth putting in the answer: The Oracle Swing Tutorial.
I have a general question about java. Because I want to create StronaGlowna.java (class) where I have place all buttons, check box and other GUI component which I want to display in main class. The first question is this right way, it's correct ? or maybe is better way to do this thing. My code look this:
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = -4575271483481196192L;
Container pane;
CardLayout layout;
public Main() throws FileNotFoundException, IOException {
layout = new CardLayout();
setLayout(layout);
pane = this.getContentPane();
/*Page: Strona główna */
JPanel newPanel = new JPanel();
pane.add("New", newPanel);
JButton przycisk = new JButton("Przycisk");
newPanel.add(przycisk);
...
In "pane.add("New", newPanel);" I want to display elements from:
package aplikacja.glowna;
import javax.swing.JButton;
import javax.swing.JPanel;
public class StronaGlowna {
public void StronaGlownaDisplay() {
JPanel newPanel = new JPanel();
JButton przycisk2 = new JButton("Przycisk");
newPanel.add(przycisk2);
}
}
Can I import/display all class StronaGlowna in main() something like a include in PHP ? What do You thing about my idea, it's correct or I'm wrong ? Thanks for help and discussion.
It sounds like the way Netbeans handling GUI. You may view the article in http://netbeans.org/kb/docs/java/quickstart-gui.html, it may help you understand how the GUI works since Netbeans can generate code for you. You can always import class and create object to access methods (often public methods) . I think it is not like a include in PHP. PHP include is like to directly include the source code, but jave is not.
First - Never, never, never, code in Main class. Call a method from it and then start your staff in another class. And, of course, don't extend it. And the constructor is neither a good idea. All of these are bad practices. Now, going into your problem, my suggestion is that you make StronaGlowna extend JPanel, and then obtain an instance of it through a public constructor, and use that instance as the parameter for the constructor of JScrollPane. That will make the scrollPane act as a 'screen' inside which you can see the contents of StronaGlowna, which is what I understand you're after.
Can someone convert this into Clojure, I don't know to do the line setMainWindow(argument) like things....
import com.vaadin.Application;
class something {
public void init() {
Window main = new Window("The Main Window");
setMainWindow(main);
addComponent(new WindowOpener("Window Opener", main));
}
}
Update:
package app;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
/**
* The Application's "main" class
*/
#SuppressWarnings("serial")
public class MyVaadinApplication extends Application{
private Window window;
#Override
public void init(){
window = new Window("My Vaadin Application");
setMainWindow(window);
window.addComponent(new Button("Click Me"));
}
}
There is a "/lib/vaadin.jar" which contains all "com.vaadin.*" things.
I think setMainWindow(window); is from the extended class. I am not going to write that method.
Literal translation:
(defn init []
(let [main (Window. "The Main Window")]
(setMainWindow main)
(addComponent (WindowOpener. "Window Opener" main))))
Though it doesn't make much sense without the context.
See http://dev.vaadin.com/wiki/Articles/ClojureScripting. Also I would suggest http://www.odesk.com.