Proper way to create an image with paintCompnent method - Java - java

I am working on an assignment and I need to first create a simple GUI that displays a picture/gif on it. However I need to use the paintComponent method, which I am struggling with.
Here is the code I have so far:
import javax.swing.*; import java.awt.Graphics.*;
public class HelloGIF extends JFrame {
image1 =Toolkit.getDefaultToolkit().createImage("C:\\Users\\carlo\\Documents\\Carlo's Documents\\ITEC 2610\\Java Files\\HelloGIF\\BT-Thumbsup.gif");
public HelloGIF(String title) {
super(title);
setBounds(75,75,750,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image1, 200, 150, 50,50,this);
}
public static void main(String[] args){
System.out.println("Initializing...");
HelloGIF myApp = new HelloGIF(String.join("+",args));
myApp.setVisible(true);
}
}
At the moment compiling generates an "identfier expected" on "C:\Users\carlo\Documents\Carlo's Documents\ITEC 2610\Java Files\HelloGIF\BT-Thumbsup.gif", pointing to the I in 'HelloGIF'. what declaration should I add? Am I on the right track or am I totally off?

Related

Java drawLine() not working

I need help with this code. My g.drawLine(0,0,300,300) is not working. It was working until monday, I don't know what is going wrong. I use the g.drawLine(0,0,300,300) in order to test before using the plota_pixel() method. g.drawLine(0,0,300,300) shoud print a line from (0,0) to (300,300) on the Jpanel panel
MainView Class:
package alexandre.VIEW;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainView {
private JFrame janela;
public JPanel panel;
public MainView()
{
janela = new JFrame("Exercicio 15/09");
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
this.ShowView();
}
public void ShowView()
{
janela.pack();
janela.setSize(750,600);
janela.setLayout(null);
janela.add(panel);
panel.setBounds(0,0,710,600);
janela.setVisible(true);
System.out.println("OIdsazxc");
Graphics g = panel.getGraphics();
g.setColor(Color.BLACK);
g.drawLine(0,0,300,300);
}
public void plota_pixel(int x, int y)
{
Graphics g = panel.getGraphics();
g.drawLine(x, y, x, y);
}
}
Starter Class:
package alexandre.CONTROL;
import alexandre.VIEW.MainView;
public class Starter {
public static void main(String[] args) {
MainView view = new MainView();
view.ShowView();
}
}
Using and drawing with the Graphics object from panel.getGraphics() does not work (see below links for the "why"). You will have to override the method "paintComponent" for the the JPanel, where the input parameter is the Graphics object
(Also quick note - standard method naming has the first letter lowercase so ShowView() should be showView())
public MainView()
{
janela = new JFrame("Exercicio 15/09");
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0,0,300,300);
}
};
this.showView();
}
public void showView() {
janela.pack();
janela.setSize(750, 600);
janela.setLayout(null);
janela.add(panel);
panel.setBounds(0, 0, 710, 600);
panel.setVisible(true);
janela.repaint();
}
Check out the following stack overflow question
Drawing an object using getGraphics() without extending JFrame
And this resource (it's also in the linked question)
http://docs.oracle.com/javase/tutorial/uiswing/painting/
You should set the panel visible as the last thing in the ShowViewmethod.
public void ShowView()
{
//your code
janela.setVisible(true);
}

Drawing a rectangle on screen as an "area selection" tool (Java)

I want the user of my program to be able to highlight an area on a JFrame to select a group of items on screen. The code I posted below works, but it is very choppy, which becomes quite an eyesore every single time the JFrame repaints. Here's an image of what I'm talking about: http://i1065.photobucket.com/albums/u400/mfgravesjr/choppy%20draw%20rectangle_zpsspqsqnyf.png
Maybe someone here has suggestions to improve my code?
This is the mouseDragged method in MouseMotionListener:
public void mouseDragged(MouseEvent me)
{
if(groupingTerr&&me.getSource()==background)
{
endPoint = MouseInfo.getPointerInfo().getLocation();
topLeftRect = new Point(Math.min(startPoint.x,endPoint.x),Math.min(startPoint.y,endPoint.y));
bottomRightRect = new Point(Math.max(startPoint.x,endPoint.x),Math.max(startPoint.y,endPoint.y));
for(Point p:map.territoryPoints)
{
if(p.x>topLeftRect.x&&p.x<bottomRightRect.x&&p.y>topLeftRect.y&&p.y<bottomRightRect.y)img.getGraphics().drawImage(selectedIco,p.x-selectedIco.getWidth()/2,p.y-selectedIco.getHeight()/2,null);
else img.getGraphics().drawImage(defaultIco,p.x-defaultIco.getWidth()/2,p.y-defaultIco.getHeight()/2,null);
}
background.repaint();
}
}
This is the private overridden JFrame class
private static class DrawableJFrame extends JFrame
{
#Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(new Color(0,0,0,100));
g.drawRect(topLeftRect.x,topLeftRect.y,bottomRightRect.x-topLeftRect.x,bottomRightRect.y-topLeftRect.y);
g.setColor(new Color(200,10,10,100));
g.fillRect(topLeftRect.x,topLeftRect.y,bottomRightRect.x-topLeftRect.x,bottomRightRect.y-topLeftRect.y);
}
}
The image is original artwork. Please do not use it.

drawing with multiple classes in java "applet not initialized"

I am trying to draw to a single panel from multiple sources in java. However when I try this test code I get applet not initialized. Keeping in mind I am fairly new to this. How would I get rid of this error and or draw to a panel from multiple sources.
import java.awt.Graphics;
import javax.swing.JPanel;
class Surface extends JPanel {
public void init() {
}
public void paintComponent(Graphics g){
super.paintComponent(g);
DrawRect d = new DrawRect(this);
d.draw( g );
}
}
class DrawRect {
Surface surface;
Graphics g;
public DrawRect(Surface surface)
{
g = surface.getGraphics();
}
public void draw( Graphics g )
{
g.fillRect(20,20,100,50); // (now this will work).
}
}

Keyevent getID causing error

I am trying to make a game where the character is a 75x75 object, and he moves around the screen. However, when I run my code, I get the error:
java.lang.IllegalArgumentException: Invalid display mode
at sun.awt.Win32GraphicsDevice.setDisplayMode(Unknown Source)
at sylvyrfysh.Screen.setFullScreen(Screen.java:17)
at sylvyrfysh.ImageDrawer.run(ImageDrawer.java:26)
at sylvyrfysh.ImageDrawer.main(ImageDrawer.java:17)
at sylvyrfysh.Main.main(Main.java:7)
I am not sure what is causing this, as I made another project with the same DisplayMode arguments, and it worked fine.
package sylvyrfysh;
import game.infos.Information;
import java.awt.*;
import javax.swing.*;
public class ImageDrawer extends JFrame{
/**
*
*/
private static final long serialVersionUID = -4278324581016693552L;
public static void main() throws InterruptedException{
DisplayMode dm=new DisplayMode(Information.sX,Information.sY,16,DisplayMode.REFRESH_RATE_UNKNOWN);
ImageDrawer i=new ImageDrawer();
i.run(dm);
}
private void loader(){
bg=new ImageIcon("src/sylvyrfysh/maze_icon.png").getImage();
chara=new ImageIcon("src/sylvyrfysh/char.png").getImage();
}
private void run(DisplayMode dm){
System.out.println("HI");
loader();
s=new Screen();
s.setFullScreen(dm,this);//error here
repaint();
while(EHandler.run){
if(rp){
repaint();
rp=false;
}
}
}
public void paint(Graphics g){
g.drawImage(bg,0,0,null);
g.drawImage(bg,360,0,null);
g.drawImage(bg,720,0,null);
g.drawImage(bg,0,480,null);
g.drawImage(bg,360,480,null);
g.drawImage(bg,720,480,null);
g.drawImage(chara,imgX,imgX,null);
}
private Image bg,chara;
Screen s;
public static int imgX=0;
public static int imgY=525;
public static Boolean rp=false;
}
Any help would be appreciated.
The ability to change graphics device's
display mode is platform- and configuration-dependent and may not always be available. GraphicsDevice.isDisplayChangeSupported() should be used to check prior to change the display mode on graphics device.
Some other important suggestion are made here nicely.

Troubles with a piechart applet

Alright the deal is I am trying to use the drawPie method to create my pie chart in an applet. After attempting google searches I find multiple tutorials that explain part of the process but not all of it. While trying to knit together partial information I am not getting the results I want.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JComponent;
public class JLW_PieApplet extends JApplet {
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = new PieValue(35, Color.red);
slices[1] = new PieValue(33, Color.green);
slices[2] = new PieValue(20, Color.pink);
slices[3] = new PieValue(12, Color.blue);
}
public void paint(Graphics g) {
drawPie((Graphics2D)g, getBounds(), slices);
}
}
}
Ther isn't such method in Swing called drawPie. Without the contents of this method, we have no idea of how to help you
Try having a read through 2D Graphics and have a look at Ellipse2D in particular
The other problem I can see is you don't call super.paint(g) in your paint method. This is VERY, VERY, VERY important
You have a PieData component within your applet but you never add it, so you need to add it in init and bring in drawPie from your link above:
public class JLW_PieApplet extends JApplet {
public void init() {
add(new PieData());
}
class PieData extends JComponent {
PieValue[] slices = new PieValue[4];
PieData() {
slices[0] = ...
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawPie((Graphics2D) g, getBounds(), slices);
}
public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
...

Categories