I'm learning java by watching tutorials, I'm a beginner. Currently, I'm trying to follow a tutorial (https://www.youtube.com/watch?v=_SqnzvJuKiA) to make the snake game. Around minute 11, the man makes a jpg display on the window. I believe I followed all of his steps but can't seem to make it work. Iv'e attached following image of my code and output
and paste "my" code.In the public void paint line netbeans tells me to "add #override annotation", which he didn't do, and "multiple annotations here [2] click to cycle".
In the output on the top white rectangle should appear the jpg.
Does someone see my mistake?
package snake;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class GP extends JPanel{
private ImageIcon titleImage;
public GP() {
}
public void paint(Graphics g)
{
// Titulo
g.setColor(Color.white);
g.drawRect(24, 10, 851, 55);
titleImage = new ImageIcon("snaketitle.jpg");
//ImageIcon icon = new ImageIcon("androidBook.jpg");
titleImage.paintIcon(this, g, 25, 11);
//Area Juego
g.setColor(Color.WHITE);
g.drawRect(24,74,851,577);
g.setColor(Color.black);
g.fillRect(25,75,850,575);
}
First of all i think your Question is understandable, what you are facing is that the program is not able to locate your image "androidBook.jpg".
By the way,
The ImageIcon class has several Constructors
Try using
ImageIcon(URL location)
//Creates an ImageIcon from the specified URL.
and paste the URL pointing to the location of "androidBook.jpg".
Also you can follow the below link for more insight...
https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html
Related
I'm new to Java and as such I've been doing my own small projects to help my learning. The code below when run will launch a new window where I changed the colour background which is correct, but the text doesn't appear and I don't understand why as there are no error messages.
In trying to fix this I tried it as an applet instead of a frame but that failed to work due to an error with a missing class. My guess is I'm missing something here but I don't know what.
Any help appreciated
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class Fonts extends Frame
{
public void paint(Graphics g)
{
setBackground(Color.blue);
g.setColor(Color.white);
Font quote = new Font("TimesRoman", Font.PLAIN, 30);
g.setFont(quote);
g.drawString("Hello World ", 10, 10);
}
public static void main (String args[])
{
Frame ff = new Fonts();
ff.resize(500,500);
ff.show();
}
}
Background:
I'm tearing my hair here because I've used awt and Swing a couple of times, and I always run into a roadblock during my initial graphics setup and I never seem to have an "Of course, this thing again! I just have to something something"-moment. This time I can't seem to get even basic drawing to work!
Problem description:
I wish to draw a number of simple boxes in a JFrame (via a JPanel, if necessary). To this end I have a Sprite extends JComponent class which currently only draws simple geometric shapes but will eventually do things with BufferedImageor somesuch.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
#SuppressWarnings("serial")
public class Sprite extends JComponent {
private Rectangle rctBounds;
private Color clrFill;
public Sprite(int x, int y, Color color) {
this.rctBounds = new Rectangle(x, y, 100, 100);
this.clrFill = color;
}// Sprite(int,int,Color)
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(clrFill);
g.fillRect(rctBounds.x, rctBounds.y, rctBounds.width, rctBounds.height);
}// paintComponent(Graphics)
}// Sprite.class
The window for these to go in is obtained by a simple Win extends JFrame class á:
import javax.swing.JFrame;
public class Win extends JFrame{
public Win(){
setSize(800,600);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Finally, components are initialized and connected in the main method, in the class run, using JComponent's (I believe) add() method, called on an instance of Win:
import java.awt.Color;
public class run {
public static void main(String[] args) {
Win w = new Win();
Sprite s1 = new Sprite(10,100, Color.BLUE);
Sprite s2 = new Sprite(200,200, Color.RED);
Sprite s3 = new Sprite(300,200, Color.CYAN);
w.add(s1);
w.add(s2);
w.add(s3);
}//main()
}//run.class
Expectations vs. Reality:
Now, I expect a window with three colored boxes, as in this image (all boxes visible). Instead, only the final JComponent to be added shows up, as seen here (only cyan box painted). What's even more infuriating is that attempting to edit the JFrame's layout or adding a JPanel as an intermediary container for the Sprites results in a blank window.
Exasperated Plea:
I'm at a complete loss! Everything I find on here talks about improper use of paintComponent(Graphics) (using paint() instead, forgetting to #Override, not propagating the graphical context properly, etc) or to use a JPanel as an intermediary container. The most infuriating part of all this is that I've built two functioning programs using Swing before (school assignments) and, looking at that code, I cannot for the life of me figure out what I'm doing wrong here! Please help!
I'm making a program for drawing out an Image and it seems I've made a mistake and my program just doesn't want to draw out the image. Can someone please point out the mistake for mi because i really don't see it.
package basic_game_programing;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Practise extends JPanel {
public Image image;
//#####PAINT__FUNCTION#####
public void PaintComponent(Graphics g){
super.paintComponent(g);
ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
image = character.getImage();
g.drawImage(image,20,20,null);
g.fillRect(20, 20, 100, 100);
}
//######MAIN__FUCTION#######
public static void main(String[]args){
Practise panel = new Practise();
//SETTING UP THE FRAME
JFrame frame = new JFrame();
//
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(panel);
//SETTING UP THE PANEL
//
}
}
You're miscapitalizing paintComponent by using PaintComponent instead (note the first "P").
So Change PaintComponent to paintComponent.
Use the #Override annotation above the method to let the compiler tell you when you're making this kind of mistake.
Never read an image into a painting method since this slows down a method that needs to be fast, and makes the image be read in over and over when one read is enough.
The method should be protected not public.
Use ImageIO.read(...) to read in your image, and use resources and relative path within the jar file, rather than use files or ImageIcons.
Don't call setVisible(true) on the JFrame until after adding all components, else some might not show.
Do read the tutorials as most all of this is well explained there.
e.g.,
public class Practise extends JPanel {
private Image image;
public Practice() {
// read in your image here
image = ImageIO.read(.......); // fill in the ...
}
#Override // use override to have the compiler warn you of mistakes
protected void paintComponent(Graphics g){
super.paintComponent(g);
// never read within a painting method
// ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
// image = character.getImage();
g.drawImage(image, 20, 20, this);
g.fillRect(20, 20, 100, 100);
}
}
So I have an image being drawn inside a JPanel which itself is added to a JFrame. However when the Image is first drawn it appears to be very small. I'm not sure if this is a problem with the panel or a problem with the image. It is illustrated below:
![enter image description here][1]
I have drawn a rectangle around the image.
Now the JPanel is supposed to be contained within the JFrame. The JFrame is not supposed to be coloured in as is seen above. The JPanel is meant to be about a quarter of the size of the JFrame and the image is supposed to take up almost all of the JPanel.
Could you please tell me if it's the image which is the problem or the Panel. Sorry if it seems obvious.
Awaiting SSCCE
I have no idea what you are doing based on the few random lines of code you posted. Nowhere in the code do you actually create/read an image.
As far as I know a Mandelbrot Set is actually done by painting code. If so the problem is probably that you did not override the getPreferredSize() (don't use the setSize() method) of you painting panel to return the size of the image you are painting. Read the section from the Swing tutorial on Custom Paining for more information.
Or if you are actually using an existing image then read the section from the Swing tutorial on How to Use Icons for working examples of using images.
Also, components should be added to the frame BEFORE you make the frame visible.
If you need more help then post a proper SSCCE that demonstrates the problem.
BufferedImage image = ImageIO.read(file); //Read image through BufferedReader
labelimage.setIcon(new ImageIcon(image.getScaledInstance(labelimage.getWidth(), labelimage.getHeight(), image.SCALE_SMOOTH))); // This line will automaticallically set Image size equal to size of Jlabel
how about adding a label inside the panel, and draw the image using setIcon(..)
usually I use the following class to make the image fit to the label size (I make my label has static size - not resizable).. you might wanna modify it to suit your need..
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.swing.ImageIcon;
public class CustomImageIcon extends ImageIcon {
private BufferedImage dest;
public CustomImageIcon(String filename) {
super(filename);
}
public CustomImageIcon(Image image) {
super(image);
}
public CustomImageIcon(URL location) {
super(location);
}
#Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
if(c!=null)
dest = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
else dest = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB);
ImageObserver imgObs = getImageObserver();
if(imgObs==null) imgObs = c;
int width;
int height;
if(c!=null)
{
width = c.getWidth();
height = c.getHeight();
}
else
{
width = getIconWidth();
height = getIconHeight();
}
g.drawImage(dest, 0, 0, c);
g.drawImage(
getImage(),
0,
0,
width,
height,
imgObs);
}
}
I am complete Java noob and this question will be very easy. I did try to find answer across whole internet but, nothing was particularly what I need (if you know any tutorial pages for this subject please post link I would be very grateful.)
Basically I am trying to draw string in my DrawPanel. I know I need to call method somewhere in order to do so but I have no idea where. My draw panel has method:
public void drawGuessWord(Graphics g){
WordsList guessWord = new WordsList();
String word = guessWord.pickWord();
g.drawString(word, 20, 20);
}
And I want to call that method so it would draw string inside DrawPanel.
Just in case this is my whole DrawPanel:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class DrawPanel extends JPanel {
DrawPanel(){
Border raisedbevel = BorderFactory.createRaisedBevelBorder();
Border loweredbevel = BorderFactory.createLoweredBevelBorder();
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createLoweredBevelBorder());
this.setPreferredSize(new Dimension(200,200));
}
public void drawGuessWord(Graphics g){
WordsList guessWord = new WordsList();
String word = guessWord.pickWord();
g.drawString(word, 20, 20);
}
}
public void paint(Graphics g)
{
g.drawString(word, 20, 20);
}
In addition to MimiEAM's solution, you might like to take a read of
Performing Custom Painting
2D Graphics