store images and display in different classes - java

I'm trying to get mutiple images in 'GetImage' class, and disply them in the main class.
Can anybody show me an example how to do it?? I tried bunch of other samples but they didn't work since I have two classes.
Here is one that I tried.
main clss:
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage c = new GetImage();
c.paint(g);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
seperate class:
import java.awt.*;
import hsa.Console;
import java.awt.event.*;
public class GetImage extends Frame
{
Image image;
String imageName = "ImageFileName.jpg";
public void paint (Graphics g)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
g.drawImage (image, 30, 30, this); // location of the image
g.drawString (imageName, 100, 50); // location of the name
}
}

I'm not very familiar with the hsa package, but some quick googling says it's an educational package from some company that has since gone out of business, correct me if I'm wrong. So personally I'd try to avoid using any of their stuff if you can.
If you have to use this for school or something then you probably want to stick entirely with their package instead of mix and matching hsa with awt. Something like this might accomplish what you want, but again I'm not familiar with the hsa package.
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage gI = new GetImage(c,25,80,12);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
import java.awt.*;
import hsa.ConsoleCanvasGraphics;
import java.awt.event.*;
public class GetImage extends ConsoleCanvasGraphics
{
Image image, image2;
String imageName = "ImageFileName.jpg", image2Name = "Image2FileName.jpg";
public GetImage(ConsoleParent parent, int rows, int columns, int fontSize)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
image2 = tool.getImage (image2Name);
super(parent,rows,columns,fontSize);
drawImage(image,30,30,this);
drawImage(image2,30,60,this);
drawString(imageName,100,50,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
drawString(image2Name,100,80,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
}
}
Again, I'd try to avoid hsa myself, but if you're set on using it and need to have two separate classes in your program then the above should be a rough outline of something that might work.

Related

Proper way to create an image with paintCompnent method - 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?

How to write in frame using JFrame?

I'm a beginner in Java, and I'm using book to learn, now I'm in the first GUI topics, and I've got a problem. I don't know why but I can't see text that I'm trying to put in the frame, this is the code:
import java.awt.*;
import javax.swing.*;
public class Ramka {
public static void main(String[] args)
{
JFrame ramka = new JFrame();
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setTitle("Frame");
ramka.setVisible(true);
Toolkit zestaw = Toolkit.getDefaultToolkit();
Dimension rozmiar = zestaw.getScreenSize();
int szerokosc = rozmiar.width;
int wysokosc = rozmiar.height;
ramka.setSize(szerokosc, wysokosc);
}
}
class Ramkatekst extends JComponent{
Toolkit zestaw = Toolkit.getDefaultToolkit();
Dimension rozmiar = zestaw.getScreenSize();
int szerokosc = rozmiar.width/2;
int wysokosc = rozmiar.height/2;
public void paintComponent(Graphics g)
{
g.drawString(" Sample text",szerokosc, wysokosc );
}
}
As #pshemo points out, Ramkatekst is never used. You need to create an instance of it and add it to your JFrame "ramka". So simply add this line at the end of your main method:
ramka.add(new Ramkatekst());

No main class found? Netbeans

I was working on my class project. I tried to draw a face emoji using the graphics thing, but I typed in the class and it says FaceComponent is not found in the FaceComponent project. I was confused about how to name the class and main method.Also, if I want to add a frame, I would have to introduce another class and main method. How do I do that in one program?
package facecomponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
/**
*
* #author HelenPeng
*/
public class FaceComponent extends JComponent {
public static void main(Graphics g) {
Graphics2D g2= (Graphics2D)g;
Ellipse2D.Double interestingEmoji = new Ellipse2D.Double(0,0,100,100);
g2.draw(interestingEmoji);
Ellipse2D .Double eye1 = new Ellipse2D.Double(25,25,10,10);
Ellipse2D .Double eye2 = new Ellipse2D.Double(50,25,10,10);
g2.draw(eye2);
g2.draw(eye1);
Line2D.Double mouth = new Line2D.Double(35,75,75,75) ;
g2.draw(mouth);
}
}
In Java args contains the supplied command-line arguments as an array of String objects.
change
public static void main(Graphics g)
In the Java programming language, every application must contain a
main method whose signature is:
public static void main(String[] args) {
When your java program run as application JVM will first try to find main method will one of the below syntax, so try to change your syntax to one of the belo version.
public static void main(String[] args) { }//Prefered style
public static void main(String args[]) { }
public static void main(String... args) { }//which additionally allows callers to use varargs syntax
Here your definition of main method is wrong. One solution is to have a main class and a drawing class. Change your FaceComponent class to below:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
/**
*
* #author HelenPeng
*/
public class FaceComponent extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2= (Graphics2D)g;
Ellipse2D.Double interestingEmoji = new Ellipse2D.Double(0,0,100,100);
g2.draw(interestingEmoji);
Ellipse2D .Double eye1 = new Ellipse2D.Double(25,25,10,10);
Ellipse2D .Double eye2 = new Ellipse2D.Double(50,25,10,10);
g2.draw(eye2);
g2.draw(eye1);
Line2D.Double mouth = new Line2D.Double(35,75,75,75) ;
g2.draw(mouth);
}
}
Then create a new class called FaceComponentTest and paste the code below in it.
import javax.swing.JFrame;
public class FaceComponentTest
{
public static void main( String[] args )
{
FaceComponent faceComponent = new FaceComponent();
JFrame faceComponentFrame = new JFrame( "My Face" );
faceComponentFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
faceComponentFrame.add( faceComponent );
faceComponentFrame.setSize( 400, 400 );
faceComponentFrame.setVisible( true );
}
This will display the face. Good luck.
public class FirstClass
{
public static void main(String[] args)
{
System.out.println("Java");
}
}
In above coding portion everything is good. But you may still get,, "no main class found"!! (It is because, I have deleted the built in class and created a new class named "FirstClass".)
For not getting this error at first 'debug' the class and then 'run'

Questions about JavaCV example

HI guys I am experimenting with the use of JavaCV as I want to get to know how it works in order to include it's functionality in a project I have in mind. I have downloaded and set up OpenCV just like what the instructions said and I also have downloaded form bytedeco the JavaCV 1.0 jars that I need to include in my project.
I have started with an example program I found online that basically grabs and save images from a webcam. The code I have written is the following:
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.VideoInputFrameGrabber;
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.*;
public class GrabberShow implements Runnable{
IplImage image;
CanvasFrame canvas = new CanvasFrame("Web Cam");
public GrabberShow(){
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public void run(){
FrameGrabber grabber = new VideoInputFrameGrabber(0);
int i = 0;
try{
grabber.start();
IplImage img;
while(true){
img = grabber.grab();
if(img != null){
cvFlip(img, img, 1);
cvSaveImage((i++) + "-aa.img", img);
canvas.showImage(img);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
GrabberShow gs = new GrabberShow();
Thread th = new Thread(gs);
th.start();
}
}
This is a very straightforward and easy example. The problem I am experiencing can be found on the following lines:
img = grabber.grab();
and
canvas.showImage(img);
The problem I face is a Type Mismatch "Cannot convert from Frame to opencv_core.LplImage".
I have tried searching for this online but I was unable to locate a good answer about this. What I did found was the same example only. Does anyone have any ideas about this?
Need to stress out that this the first time I use openCV with Java. I have used it in the past to make and object tracking program but this was done with native openCV and using Python.
The code you have looks like it would probably work for javacv version 0.10. For 1.0 theFrameGrabber's returnFrame objects that then need to be converted to IplImage objects using a OpenCVFrameConverter.ToIplImage.
import org.bytedeco.javacpp.opencv_core.IplImage;
import static org.bytedeco.javacpp.opencv_core.cvFlip;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.VideoInputFrameGrabber;
...
public class GrabberShow implements Runnable {
CanvasFrame canvas = new CanvasFrame("Web Cam");
public GrabberShow() {
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public void run() {
FrameGrabber grabber = new VideoInputFrameGrabber(0);
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
int i = 0;
try {
grabber.start();
IplImage img;
while (true) {
Frame frame = grabber.grab();
img = converter.convert(frame);
if (img != null) {
cvFlip(img, img, 1);
cvSaveImage((i++) + "-aa.img", img);
canvas.showImage(frame);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GrabberShow gs = new GrabberShow();
Thread th = new Thread(gs);
th.start();
}
}

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