Java User Interface: add simple chart - java

I am new to Java User Interface ..
I have my main window with a TabbedPane ...
I want to use a library of graphics to display ... I have chosen this and i looked this example ...
ChartExample
but, how can I add the example chart on my TabbedPane?

The chart API you have chosen generates charts using Google's API. This means you'll have to display the image from the URL of chart.toURLString(); in your app. This could be as simple as putting a JLabel on your tabbed pane and setting its caption to <img src="(url)"/>, but I'm not entirely sure that handles HTTP downloads.
JLabel label = new JLabel("<img src='" + chart.toURLString() + "'/>");
tabbedPane.add(label);
If it doesn't, you can use javax.ImageIO.read(URL) to load the chart image into a BufferedImage and then create a new ImageIcon to put that onto the label:
URL url = new URL(chart.toURLString());
BufferedImage bimg = ImageIO.read(url);
Icon icon = new ImageIcon(bimg);
JLabel label = new JLabel(icon);
tabbedPane.add(label);
Your application will need a live Internet connection for the chart image to show up. If you need the images to work when you have no Internet access, you'll have to use a different charting library such as JFreeChart.

Related

take screen shot of a specific area in sikuli for eclipse gui

I am trying to take screen shot of my screen while running the test execution.
I use the below code using sikuli java:
Screen screen = new Screen();
ImageIO.write(screen.capture().getImage(), "png", new File(file-location+"1.png"));
Another way i am using is java awt:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle screenRect = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image5 = robot.createScreenCapture(screenRect);
ImageIO.write((RenderedImage) image5, "png", new File(AppConstant.IMAGE_DIR+"1.png"));
But the problem is, it is taking screen shot of my whole screen with Ubuntu menu bar. What I want is to take only the screen shot of my eclipse window, a specific region.
You can do this using the Sikuli App Class. For eg. If the app is on focus while running the test, you can use the below method:
Region appRegion = App.focusedWindow();
Screen screen = new Screen();
ImageIO.write(screen.capture(appRegion).getImage(), "png", new File(file-location+"1.png"));
This way only the app window is captured as per your requirement and since you have a region now, you can manipulate it to increase/decrease the size captured.
One way of doing that is through using the App class. So if you start your Eclipse process like this:
// You might need to provide full path here
App app = App.open("eclipse.exe");
Then you can use the capture method with app region as argument. The app region can be obtained through window() method. So:
App app = App.open("eclipse.exe");
ImageIO.write(screen.capture(app.window()).getImage(), "png", new File("1.png"));

Using UrlImage causes bad quality scaled image

I am trying to create an app using codename one that uses a RESTful web service to download details to be displayed including an image url.
I think what I need is ScaleImageButton, the purpose of which is to have a list of items to be clicked to display more information (so several ScaleImageButtons).
The code I am currently using to create the button is as follows:
java.util.List<String> splitImage = tokenize(url, '/');
EncodedImage placeholder = FontImage.createMaterial(FontImage.MATERIAL_SAVE, btnStyle.getUnselectedStyle()).toEncodedImage();
URLImage image = URLImage.createToStorage(placeholder, splitImage.get(splitImage.size() - 1), url, URLImage.RESIZE_SCALE_TO_FILL);
ScaleImageButton btn = new ScaleImageButton(image);
btn.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
The background image of this button is of very poor quality (a few millimetres per pixel) even though the image displays in the larger browser window.
How can I have the image fill the background but also keep its quality?
Thanks in advance.
What you are doing is downloading an image which is the size of your placeholder and scaling it up by applying it to a ScaleImageButton, instead of downloading an image of the right size.
Be sure that your placeholder image is of the right size before using it to download the actual image.
For instance:
//half the size of device width image
EncodedImage placeholder = FontImage.createMaterial(FontImage.MATERIAL_SAVE, btnStyle.getUnselectedStyle(), CN.getDisplayWidth() / 2).toEncodedImage();

OSMdroid - Replace empty grid background when no tile available (offline)

I'm using OSMdroid library in my project to display map tiles (it's a shape with lines and blank background) from my company local sever in my app. The thing is when I use the app in offline mode and I browse the map shows an empty grey grid background for the tiles that aren't in the cache, and I want to change that empty grey grid for an blank background image/tile.
The only workaround that I've found to achieve this is the following:
Add a tile overlay and set setLoadingBackgroundColor and setLoadingLineColor to Color.WHITE, and then set the TileSource from the local server from OnlineTileSourceBase. I know this is not quite performant, so is there a better way to do this? Thanks in advance!
final ITileSource tileSource = new OnlineTileSourceBase(...)
{
#Override
public String getTileURLString(MapTile aTile) {
return getBaseUrl() + aTile.getX() + "+" + aTile.getY() + "+" + aTile.getZoomLevel();
}
};
tileProvider = new MapTileProviderBasic(context, tileSource);
tilesOverlay = new TilesOverlay(tileProvider , context);
tilesOverlay.setLoadingBackgroundColor(Color.WHITE);
tilesOverlay.setLoadingLineColor(Color.WHITE);
this.map.getOverlays().add(tilesOverlay);
this.map.setTileSource(tileProvider.getTileSource());
map.invalidate();
Your code is an example on adding a secondary tile overlay. That's useful for when you need another raster graphics overlay on top of the map imagery.
You can also change the loading lines and background for the existing and default tiles overlay. This should get you going
mMapView.getOverlayManager().getTilesOverlay().setLoadingBackgroundColor(Color.TRANSPARENT);
mMapView.getOverlayManager().getTilesOverlay().setLoadingLineColor(Color.TRANSPARENT);

how to draw java chat Bubble speech?

hello for every one pls help me ,
i am trying to
make Java Desktop app for chatting
,i want to add chat representation in my chat application's to make UI style messages bubble speech Conversation so i want to draw by Java Code like this enter image description here pls help me the code or how to draw by java code like this ,
this my first post in https://stackoverflow.com/
Thank you
Judging from your question and your time here I take it that you are not too experienced with java (I mean no offence).
Instead you can do as I did, which worked perfectly fine for such a situation:
I created a JLabel, and assigned it an ImageIcon, In the code example below you will see me creating two labels which where to be used as buttons, one up and one down arrow for a list:
private void buildPair() {
//Make an ImageIcon Array (optional)
ImageIcon image[] = new ImageIcon[2];
//You create a new ImageIcon with the picture from your recouses, then you the the image and scale it, in this case its scaled to 20 by 20 pixel
Image originalDown = new ImageIcon(ExtendedList.class.getResource("/Elements/Down Arrow Button.png")).getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
Image originalUp = new ImageIcon(ExtendedList.class.getResource("/Elements/Up Arrow Button.png")).getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
//Then I add the scaled images to a new ImageIcon and then add it to my ImageIgon arrat
image[0] = new ImageIcon(originalDown);
image[1] = new ImageIcon(originalUp);
//Using an external Jlabel array i assing it with new Jlabel with this ImageIcon
buttonPair[0] = new JLabel(image[0]);
//I then add a custom event class, but you can make your own method or leave this out if its not needed
buttonPair[0].addMouseListener(events);
//And lastly i add this to my form
this.add(buttonPair[0]);
//and repeat for as many times as you need
buttonPair[1] = new JLabel(image[1]);
buttonPair[1].addMouseListener(events);
this.add(buttonPair[1]);
}
Do not forget to import:
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

How to show a captcha in a java swing application

I need to add a captcha validator in a java swing application. I have been searching some libraries (JCaptcha and SimpleCatcha) but they are for web development.
Is there any library to use captcha on swing? and if it's not, is there a web page or repository with some captcha caracters to implement my own captcha?
I really appreciate your time and your help.
Thanks in advance.
JCaptcha can return a BufferedImage. From there it is not much difficult to get the image visible using a JLabel:
BufferedImage captcha = // Get the captcha
// See also com.octo.captcha.service.image.AbstractManageableImageCaptchaService.getImageChallengeForID(String)
JLabel label = new JLabel(new ImageIcon(captcha));
// ... add that label to a visible container of your Swing application
In version 1.0, you can use this: http://jcaptcha.sourceforge.net/apidocs/1.0/com/octo/captcha/service/image/AbstractManageableImageCaptchaService.html
In 2.0-alpha1, there is this: http://jcaptcha.sourceforge.net/apidocs/2.0-alpha1/com/octo/captcha/service/image/AbstractManageableImageCaptchaService.html#getImageChallengeForID(java.lang.String)
You can also check the overloaded version of those methods with an extra Locale argument.
In each case, there is a default implementing class DefaultManageableImageCaptchaService.
BufferedImage captcha = // Get the captcha
// See also
com.octo.captcha.service.image.AbstractManageableImageCaptchaService.getImageChallengeForID(String)
JLabel label = new JLabel(new ImageIcon(captcha));
// ... add that label to a visible container of your Swing application

Categories

Resources