How to display a picture on JFrame? - java

I'm doing this exercise where I'm supposed to write a program that simulates a race between two cars.
I've created a JFrame and added two rectangles that are supposed to be the tracks.
But I can't insert the cars. I have googled and tried some solutions but it just doesn not work out.
Here is my code.
public class Race extends JComponent {
private ImageIcon image;
public void paint(Graphics g) {
g.setColor(Color.GRAY);
g.fill3DRect(30, 150, 530, 55,true);
g.setColor(Color.GRAY);
g.fill3DRect(30, 250, 530, 55, true);
g.setColor(Color.BLACK);
g.fill3DRect(90, 130, 12, 189, true);
}
public static void main(String[] a) {
JFrame window = new JFrame();
window.setPreferredSize(new Dimension(600, 400));
window.getContentPane().setBackground(Color.GREEN);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new Race());
window.pack();
window.setVisible(true);
}
}
Where and how can I add two pictures?
Thanks

This is how you would add a image into your JFrame
frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));

Related

Java on Eclipse won't show the JPanel even when I add it to JFrame

These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it.
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Collections;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
frame.setVisible(true);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.setVisible(true);
}
-------------DRAWINGPANEL FILE-------------------
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
public void painting(Graphics pen) {
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200,50,20,20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
Here's what I get after making your code runnable, fixing your JFrame method calls and fixing your drawing JPanel.
Swing applications should always start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
You pack a JFrame. You set the preferred size of your drawing JPanel. This way, you know how big your drawing JPanel is, without worrying about the JFrame decorations.
Here's the complete runnable code.
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawingPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawingPanelExample());
}
#Override
public void run() {
JFrame frame = new JFrame("My Empty Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(350, 300));
}
#Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.drawRect(50, 50, 20, 20);
pen.drawRect(100, 50, 40, 20);
pen.drawOval(200, 50, 20, 20);
pen.drawOval(250, 50, 40, 20);
pen.drawString("Square", 50, 90);
pen.drawString("Rectangle", 100, 90);
pen.drawString("Cirlce", 200, 90);
pen.drawString("Oval", 250, 90);
pen.fillRect(50, 100, 20, 20);
pen.fillRect(100, 100, 40, 20);
pen.fillOval(250, 100, 20, 20);
pen.fillOval(250, 100, 40, 20);
pen.drawLine(50, 150, 300, 150);
pen.drawArc(50, 150, 200, 100, 0, 180);
pen.fillArc(100, 175, 200, 75, 90, 45);
}
}
}
Try changing the method in DrawingPanel from painting to paint, which will get called when run. paint is a method inherited from JPanel.
Edit: As mentioned by NomadMaker, use paintComponent() not paint() here. Read this for more information.
You should override paintComponent like so:
...
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D pen = (Graphics2D) g;
...
}
Also some suggestions:
You can extending JComponent instead of JPanel (It should work both ways)
You can use setSize or setPreferredSize for your panel to fit it with your frame size.
You can only use setVisisble(true); only once after all of the configurations of your frame.
And add it to the center of the frame like so:
...
JFrame frame = new JFrame();
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
DrawingPanel panel = new DrawingPanel();
panel.setPreferredSize(new Dimensions(350, 300));
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
...
On a side note:
Adding a layout manager may not be necessary and you can also replace setPreferredSize with setBounds like so:
panel.setBounds(0, 0, 350, 300);
frame.add(panel);
Where 0s are x and y coordinates respectively.

i cant display buttons on top of a graphic

My question is how to display buttons on top of a circle which created with Graphics. My code is :
public class Grafik extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Grafik frame = new Grafik();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Grafik() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 784, 419);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Click");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fire();
}
});
btnNewButton.setBounds(64, 73, 32, 32);
contentPane.add(btnNewButton);
}
public void fire()
{
JPanel panel = new JPanel(){
#Override
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(20, 20, 400, 400);
g.fillOval(20,20,400,400);
g.setColor(Color.white);
g.drawOval(60, 60, 300, 300);
g.fillOval(60,60,300,300);
}
};
panel.setBounds(10, 27, 383, 233);
setContentPane(panel);
JButton btn = new JButton();
btn.setIcon(new ImageIcon("C:\\Users\\nuria\\Desktop\\icon1.png"));
btn.setBounds(75, 76, 32, 32);
panel.add(btn);
}
}
This code is a little part of my project. I have a lot of panels in my project and my fire method isn't called firstly. I mean the fire method is called by sixth panel. I have to draw two circles with buttons. These buttons should be between two circles. But i cant display buttons directly. When i hover the buttons, they show. I want to show buttons instantly when the sixth panel opens.
I have two buttons above. When the first button is clicked, another panel should open and show circles(which created with Graphics) and another button(with a icon) between them.
JPanel panel = new JPanel(){
#Override
public void paint(Graphics g)
{
Don't override paint(). Custom painting is done by overriding paintComponent() and don't forget to invoke super.paintComponent() as the first statement.
The problem with your current code is that by overriding paint you have changed the default painting logic of the panel and the child components are never painted.
Take a look at the section from the Swing tutorial on Custom Painting for more information and examples, especially the section on A Closer Look at the Paint Mechanism.

simple GUI program

I just made this program that I add a button on my panel, but I am not able to move on to use actionListener to make the button work. Their should be pictures appered on the panel and when the button clicked, the picture should change to another picture. Please help me, thanks! Here is my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawPanelThree extends JPanel
{
private JButton button;
public DrawPanelThree()
{
button = new JButton();
setLayout(new BorderLayout());
add(button, BorderLayout.SOUTH);
button.setText("Start");
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(90, 40, 100, 50);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
g.fillRect(260, 10, 10, 10);
g.fillRect(10, 120, 10, 10);
g.fillRect(260, 120, 10, 10);
g.setColor(new Color(255, 215, 0));
g.fillOval(120, 45, 40, 40);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Rectangle");
frame.setSize(new Dimension(300, 200));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPanelThree panel = new DrawPanelThree();
frame.add(panel);
panel.setBackground(Color.CYAN);
frame.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
}
You have to add the ActionListener to the button, like so:
button.addActionListener(new MyCoolActionListener());
You can also define the ActionListener when you declare it, but that's the general idea. You want to add the ActionListener soon after you declare the JButton, in your constructor.
Hope this helps!

Weird shadowing while changing JLabel text

I am working on a little menu program with clickable buttons and an image that changes based on button clicks. If I click a button I get a shadow of the button at the bottom where I change the JLabel text. I cannot figure it out for the life of me. Any advice would be greatly appreciated. Visuals below...thanks
public class SampleGUI
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " +
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Robert's VICI Prototype");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel
{
// Fields
Image imageDisplayed;
JLabel status;
// Methods
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(null);
JLabel title = new JLabel("CS380 TEAM 5 VICI Prototype");
title.setFont(new Font("Tahoma", Font.BOLD, 20));
title.setBounds(425, 10, 400, 40);
add(title);
status = new JLabel("Please click an option above.");
status.setFont(new Font("Tahoma", Font.BOLD, 14));
status.setBounds(425, 740, 400, 40);
add(status);
JButton choice1 = new JButton("Search Class");
choice1.setBounds(50, 50, 150, 40);
add(choice1);
JButton choice2 = new JButton("Add Class");
choice2.setBounds(225, 50, 150, 40);
add(choice2);
JButton choice3 = new JButton("Drop Class");
choice3.setBounds(400, 50, 150, 40);
add(choice3);
JButton choice4 = new JButton("Verify Reg Hold");
choice4.setBounds(575, 50, 150, 40);
add(choice4);
JButton choice5 = new JButton("Verify Reg Date");
choice5.setBounds(750, 50, 150, 40);
add(choice5);
JButton choice6 = new JButton("Schedule Advisor");
choice6.setBounds(925, 50, 150, 40);
add(choice6);
choice6.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Schedule Advisor button pressed.");
status.setText("Choose a date.");
imageDisplayed = new ImageIcon("C:\\Temp\\sa01.jpg").getImage();
}
});
JButton exit = new JButton("EXIT");
exit.setBounds(940, 750, 150, 40);
add(exit);
exit.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
imageDisplayed = new ImageIcon("C:\\Temp\\main.jpg").getImage();
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(1100, 800);
}
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
}
You've broken the paint chain...
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The first thing you should be calling is super.paintComponent(g)
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The Graphics context is shared resource, meaning that the Graphics context you get has also been used to paint the other components that have also been painted. One of the jobs of paintComponent is to clear the Graphics context ready for the component to be painted (fill the background)
See Painting in AWT and Swing and Performing Custom Painting for more details
You should, also, avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Why is it frowned upon to use a null layout in SWING? for more details...
Before you go all defensive over getting your screen laid out just nicely, you shouldn't be doing it this way. You controls should be laid out on separate containers (using appropriate layout managers) and you "drawing surface" should be it's own component. See Laying Out Components Within a Container for more details...
If you're still not convicned, take a look at Why setting setPreferredSize() on JFrame is bad? and java expandable JDialog for examples of differences between Mac and Windows

How to make 3D rounded corner JLabel in Java?

I know there is a way to extend a JLabel to paint 3D borders and a way to paint round borders, but how do you get both? Here is my code
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 25, 25);
g.fill3DRect(10, 10, 30, 30, true);
super.paintComponent(g);
Use LineBorder with rounded corners or a variant of the TextBubbleBorder.
You refer this code to create Round Corner JLabel:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class RoundedLineBorder extends JPanel {
public RoundedLineBorder() {
super(true);
setLayout(new BorderLayout());
JLabel label = new JLabel("Rounded Corners");
label.setHorizontalAlignment(JLabel.CENTER);
LineBorder line = new LineBorder(Color.blue, 2, true);
label.setBorder(line);
add(label, BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Rounded Line Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setContentPane(new RoundedLineBorder());
frame.setVisible(true);
}
}

Categories