The picture does not show in this Frame? - java

I have a problem with this following piece of code: the photo does not show in my JFrame...
Please help me.
My Java code:
public class ImageRead extends JFrame {
JPanel pan;
Image img1;
public ImageRead() throws IOException {
this.pan = new JPanel();
final Dimension d = new Dimension(500, 500);
this.img1 = ImageIO.read(new File("C:\\Users\\KHALED\\workspace\\"
+ "Universite Base De Donner\\src\\2.jpg"));
this.pan.setPreferredSize(d);
this.pan.add(new JButton("entre"));
this.pan.setBackground(Color.red);
this.getContentPane().add(this.pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(500, 500);
this.setVisible(true);
this.setResizable(false);
}
public void paintComponents(final Graphics g) {
final Graphics2D g2 = (Graphics2D) g;
g.drawImage(this.img1, 0, 0, this.pan);
g2.finalize();
}
public static void main(final String[] args) throws IOException {
new ImageRead();
}
}

There are lots of problems with this, but probably the primary one is that you don't want to override paintComponent of the JFrame, you want to do that on the ContentPane. So create a JPanel derived class, and do that code in there, then call myframe.setContentPane(mypanel);
another issue is you don't want to call finalize() on g2
Chances are you want to use a classpath resource to load that image, rather than using File io.

You should probably use a proper layout and a real ImageIcon. Also, did you mean destroy(); instead of g2.finalize();?

Related

What did super.paintComponent(g) do?

So in the past few days I've tried to implement an easier version of a graph plotter.
One big problem I was confronted with was a bug that occured on repainting.
Basically I've designed my program in one class which is responsible for drawing the whole coordinate system and the given function after clicking a JButton in an other class. The other class contains the JButton which is pressed. After pressing the JButton it calls a function in the coordinate system class which repaints the picture. Both of those classes are extending JPanel.
The bug was that when I was doing the repainting on pressing the button, the button was drawn on the coordinate System and not in its original place, so in other words on the other JPanel even though I didn't change a thing about placements and stuff. Both classes were added to a JFrame which use a GridLayout.
Can anyone tell me why super.paintComponent(g); solved that bug?
Edit: Added Code
Window class
public class main {
public static void main(String[] args) throws SemanticFailureException {
int x = 800;
int y = 600;
JFrame frame = new JFrame();
frame.setLayout(new GridLayout());
frame.setTitle("Function plotter");
frame.setSize(2*x, 2*y);
Surface test = new Surface(x, y, 10);
CommandDraw test1 = new CommandDraw(x/2,y,test);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(test);
frame.add(test1);
frame.setVisible(true);
}
}
Coordinate System class: (changed drawing the coordinate system to a rectangle for simplicity, the bug still occures with only drawing a rectangle)
public class Surface extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
boolean drawFunct;
public Surface(int x1, int y1, int coordLength) {
setSize(x1,y1);
drawFunct = false;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // without this the jbutton occures on the left
// create Graphics object to get more functions
Graphics2D g2 = (Graphics2D) g;
// draw Plotter
drawFunction(g2);
if (drawFunct)
g2.drawLine(0, 0, 80, 80);
}
public void drawFunction(Graphics2D g) {
g.drawRect(40, 40, 30, 30);
}
public void redraw() {
drawFunct = true;
repaint();
}
}
The Class with the JButton:
public class CommandDraw extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton makeDraw;
JTextField inputPoly;
Surface surf;
public CommandDraw(int x, int y, Surface surf) {
this.surf = surf;
setSize(x,y);
setLayout(new FlowLayout());
makeDraw = new JButton("draw Function");
makeDraw.setBackground(Color.LIGHT_GRAY);
makeDraw.setFocusable(false);
makeDraw.addActionListener( new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
surf.redraw();
}
});
add(makeDraw);
inputPoly = new JTextField("Input polynomial");
inputPoly.setHorizontalAlignment(JTextField.CENTER);
add(inputPoly);
}
}
Can anyone tell me why super.paintComponent(g); solved that bug?
Because the call to paintComponent(g) of the superclass (JPanel) will guarantee that panel will be rendered as expected before you do your paint operations. You need to make sure that your JPanel will behave, in the Graphics perspective, like any other JPanel.
A template for a customized JPanel to draw should be something like:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class MyDrawPanel extends JPanel {
#Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
// create a new graphics context based on the original one
Graphics2D g2d = (Graphics2D) g.create();
// draw whatever you want...
g2d.dispose();
}
}
EDIT
You need to call super.paintComponent(g) to tell that the JPanel paintComponent(Graphics) version should be execute before you start doind your customized things. It's something like to ask the JPanel to prepare its paint capabilities to be used. A good starting point to these kind of doubts is the documentation: https://docs.oracle.com/javase/10/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
The dispose() method is being called in the copy of the original Graphics. If you dispose the Graphics that is passed to the method, you may have some issues too. You could use the original Graphics to perform you painting operations, but you shouldn't, so a better practice is to make a copy of the original Graphics and dispose it after using.
Take a look here too: How does paintComponent work?

Painting mutliple objects in 1 JFrame

I'm trying to make my program object oriented and I'm attempting to split it into several classes. The short version is that I want to paint different objects in 1 JFrame. So I created a class per object that I wanted to paint, defined the object in my method and then add them to my frame. The problem is that only the last component is painted in the frame. I tried adding the objects to a JPanel first but that doesn't seem to work.
trees1
public class trees1 extends JComponent{
public final ImageIcon pokemontree;
public trees1(){
ImageIcon poke = new ImageIcon("pokemontree.png");
Image image = poke.getImage(); // transform it
pokemontree = new ImageIcon(newimg); // transform it back
}
public void paintComponent (Graphics g){
Graphics2D g2 = (Graphics2D) g;
pokemontree.paintIcon(this,g2 , 100,200);
}
}
testing
// main program
public class testing {
public static void main(String[] args){
JFrame win = new JFrame();
win.setSize(600,400);
win.setTitle("Test");
win.setResizable(false);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
trees1 exo = new trees1();
Playerwalking p1 = new Playerwalking(1,2);
win.add(exo);
win.add(p1);
win.setVisible(true);
}
JFrame content pane default uses BorderLayout
The default content pane will have a BorderLayout.
For BorderLayout, the "add()" method always set to "CENTER" if you don't specify the location (NORTH/EAST/SOUTH/WEST/CENTER etc) of the components you adding.
You need to specify your "getPreferredSize()" in your component, also understand how to use the BorderLayout (or other layout manager properly).
public class Test1 extends JFrame {
public static void main(String[] args) throws Exception {
JFrame win = new JFrame();
win.setSize(600,400);
win.setTitle("Test");
win.setResizable(false);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new trees1(), BorderLayout.CENTER);
win.add(new trees1(), BorderLayout.SOUTH);
win.add(new trees1(), BorderLayout.EAST);
win.pack();
win.setVisible(true);
}
}
class trees1 extends JComponent {
public final ImageIcon pokemontree;
public trees1(){
ImageIcon poke = new ImageIcon("c:\\temp\\2.png");
Image image = poke.getImage(); // transform it
pokemontree = new ImageIcon(image); // transform it back
}
#Override
public Dimension getPreferredSize() {
return new Dimension(pokemontree.getIconWidth(), pokemontree.getIconHeight());
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
pokemontree.paintIcon(this,g2 , 0,0);
}
}

Java noob - drawing text

I took two open source games to learn how to overlay text onto one of the games from the other. however, whenever i try to transition one piece of code from the next all i get is exceptions. to make sure im drawing a string correct im using :
import java.awt.*;
public class charge{
Image buffer;
Graphics bufferg;
public void draw2(Graphics g2d){
g2d.setFont(new Font("Helvetica", Font.PLAIN, 13));
g2d.drawString("Most relationships seem so transitory", 20, 30);
}
}
can anyone tell me how im using graphics wrong?
Using the graphics class correctly is very tricky, especially if you're new to java. It's likely that the error your getting is a null pointer, because your g2d arg is probably null. One (preferred) way to draw text in java is to subclass the JPanel class and override the paint(Graphics) method so whenever the panel is drawn, it's drawn with text. Here's an example:
public class Test1 extends JFrame {
public Test1(){
TextPanel t = new TextPanel("Here's some text!");
getContentPane().add(t, BorderLayout.CENTER);
setMinimumSize(new Dimension(500, 500));
//Various JFrame initialization stuff.
//repaint() makes sure the text is immediately visible.
repaint();
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class TextPanel extends JPanel{
private String text;
public TextPanel(String t){
text = t;
}
//Called by swing whenever this or a parent component calls repaint()
#Override
public void paint(Graphics g){
g.setFont(new Font("Helvetica", Font.PLAIN, 13));
g.drawString(text, 20, 30);
}
}
public static void main(String[] args){
new Test1();
}
}
If you want to use the text for overlays then just make sure the background of the TextPanel is transparent and place it wherever you want to on the window.

Can't print any string using drawString() in JFrame

I'm trying to find what is wrong with this short code. I can't print the String TEXT in my JFrame using drawString() method. Please Help . Only a plain white screen will appear if you run the program .
Code:
import javax.swing.*;
import java.awt.*;
public class sample extends JFrame
{
private JPanel panel;
public sample()
{
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
panel =new JPanel();
Container mainP= getContentPane();
mainP.add(panel);
panel.setBounds(0,0,500,500);
panel.setBackground(Color.WHITE);
}
public void paintComponent(Graphics g)
{
Graphics2D eg = (Graphics2D)g;
eg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
eg.setColor(Color.BLACK);
eg.drawString("TEXT", 40, 120);
}
public static void main(String args[])
{
new sample();
}
}
JFrame has no paintComponent method. So you aren't override anything, and not painting will be done.
On that note JPanel does have a paintComponent method, and you should be painting on a JComponent or JPanel, which do have the method. You don't want to paint on top-level containers like JFrame. (if you really need to know though, the correct method to override is paint for JFrame).
That being said, you should also call super.paintComponent inside the paintComponent method so you don't break the paint chain and leave paint artifacts.
Side Notes
As good practice, make use of the #Override annotation, so you know you are correctly overriding a method. You would've seen that paintComponent doesn't override one of JFrames methods.
setVisible(true) after add your components.
panel.setBounds(0,0,500,500); will do absolutely nothing, since the JFrame has a default BorderLayout
Follow Java naming convention and use capital letters for class names.
Run Swing apps from the Event Dispatch Thread. See more at Initial Threads
FINAL
import javax.swing.*;
import java.awt.*;
public class Sample extends JFrame {
private JPanel panel;
public Sample() {
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D eg = (Graphics2D) g;
eg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
eg.setColor(Color.BLACK);
eg.drawString("TEXT", 40, 120);
}
};
Container mainP = getContentPane();
mainP.add(panel);
panel.setBackground(Color.WHITE);
setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Sample();
}
});
}
}

Java: Image won't show until I resize the window

I am programming in java and I'm getting into GUIs and Graphics. In my program I paint an image onto a JPanel and add the JPanel to the main window. The Problem I'm having is when I run the program the image doesn't show until I manually resize the window. Here is the relevant code:
Where the image is drawn:
public class painting extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.drawImage(Toolkit.getDefaultToolkit().getImage("image.png"), 0, 0, null);
}
}
Where JPanel is added to JFrame (c is GridBagConstraints):
public class GUI extends JFrame{
public GUI(){
painting Pnt = new painting();
c.gridx = 1; c.gridy = 0;
c.ipadx = 540; c.ipady = 395;
add(Pnt, c);
}
}
Where The Window is set up:
public class MainC{
public static void main (String args[]){
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");
}
}
Thanks,
Bennett
EDIT: I noiced that it sometimes displays the image correctly but then if I close the program and try again and it doesn't work until I resize it.
EDIT2: Here are the files GUI class, MainC class
Toolkit.getImage() works asynchronously. Either use ImageIO.read() or add a MediaTracker.
In GUI constructor you should call super() before anything else.
Also move:
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Title");
in GUI constructor.
Your problem must be somewhere else in your code. I copied and pasted your three classes into my IDE. The only thing I changed was in GUI.java I added setLayout(new GridBagLayout()); and GridBagConstraints c = new GridBagConstraints(); and I changed the location of the image to one of my own. The window works as expected with the image displayed right away.
I assume you have additional code not displayed here, such as the initialization of c that I added. Check your other code to make sure you are not redrawing over your image initially. Also, if you post the other code I could help you identify the problem
does
Toolkit.getDefaultToolkit().getImage("image.png")
brings you the picture?
I tried it with:
public class Gui extends JFrame{
public Gui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setBounds(100, 100, 300, 300);
JPanel pnl = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
try {
g.drawImage(new Robot().createScreenCapture(new Rectangle(90, 90)), 0, 0, null);
} catch (AWTException e) {
e.printStackTrace();
}
}
};
getContentPane().add(pnl, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Gui().setVisible(true);
}
});
}
and it works.

Categories

Resources