Swing draws an image of lower quality than on the web - java

For Java swing, I'm trying to add an image in a panel and create the image using JLabel.
JLabel imageLabel = new JLabel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
BufferedImage bi = ImageIO.read(imageInputStream);
double scale = 50 / bi.getHeight();
g2.scale(scale, scale);
g2.drawImage(bi, 0, 0, null);
}
}
On web
<img src="imageURL" hieght="50"/>
Why Swing draws the image, its quality is not as good as web. Why?

Related

Java: Canvas.drawline, how to draw a thicker line

Im trying to draw a lightning bolt on my canvas but the line is only one pixel wide. How can I make the line thicker?
canvas.drawLine(lightningBoltArray[i].startXArray[k],lightningBoltArray[i].startYArray[k],lightningBoltArray[i].endXArray[k],lightningBoltArray[i].endYArray[k],paintHalfAlpha);
Is the trick somewhere in the settings of the applied paint?
If you're not doing something that allows you do to this you're probably painting incorrectly. Assuming that this is in a JPanel instance. It will also work for paint(Graphics g) in Canvas but most painting in Java is done using Swing
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// other stuff
float width = 3f;
BasicStroke stroke = new BasicStroke(width);
g2d.setStroke(stroke);
g2d.drawLine(...);
// more stuff
}

How to draw a circle on to panel in java swing

I am trying to draw a circle for a game I am making for a class.
This is my code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// Assume x, y, and diameter are instance variables.
Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 150, 150);
}
However, when I run the code, it is not seen (My frame is 300 by 300)

Java: Change color of an JPanel with fillRect

I want to fill my Panel with an Rectangle to override the image. But with my code nothing happens. You know why? I dont want to use setBackground.
Graphics g = JPanel.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, window.drawPanel.getWidth(), window.drawPanel.getHeight());
If you want to change the panel background with this way. You need to override paintComponent method like this.
JPanel jYourPanel = new JPanel(){
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//lets paint background
g2.setColor(Color.RED);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}

How to resize text in java

I have seen that in photoshop text can be easily resized just by dragging them. How can we do the same thing in Java? Any idea on how to resize text in java?
Added a snapshot of letter "A" resized in photoshop
Please let me know what is wrong with this code?
public class ResizeImage extends JFrame {
public ResizeImage(){
JPanel panel = new JPanel(){
public void paintComponent(Graphics g) {
// In your paint(Graphics g) method
// Create a buffered image for use as text layer
BufferedImage textLayer = new BufferedImage(240, 240,
BufferedImage.TYPE_INT_RGB);
// Get the graphics instance of the buffered image
Graphics2D gBuffImg = textLayer.createGraphics();
// Draw the string
gBuffImg.drawString("Hello World", 10, 10);
// Rescale the string the way you want it
gBuffImg.scale(200, 50);
// Draw the buffered image on the output's graphics object
g.drawImage(textLayer, 0, 0, null);
gBuffImg.dispose();
}
};
add(panel);
}
public static void main(String [] args){
ResizeImage frame = new ResizeImage();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
One way is to use an AffineTransform (this variant also fades the color).
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import javax.imageio.ImageIO;
public class StretchText {
public static void main(String[] args) throws Exception {
// used to stretch the graphics instance sideways
AffineTransform stretch = new AffineTransform();
int w = 640; // image width
int h = 200; // image height
int f = 21; // Font size in px
String s = "The quick brown fox jumps over the lazy dog.";
final BufferedImage bi = new BufferedImage(
w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setFont(new Font("Serif",Font.PLAIN,f));
g.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// paint BG
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
g.setColor(Color.BLACK);
for (int i=0; (i*f)+f<=h; i++) {
g.drawString(s, 0, (i*f)+f);
// stretch
stretch.concatenate(
AffineTransform.getScaleInstance(1.18, 1d));
g.setTransform(stretch);
// fade
Color c = g.getColor();
g.setColor(new Color (
c.getRed(),
c.getGreen(),
c.getBlue(),
(int)(c.getAlpha()*.75)));
}
g.dispose();
ImageIO.write(bi, "png", new File(
new File(System.getProperty("user.home")),
"StretchText.png"));
Runnable r = new Runnable() {
#Override
public void run() {
JLabel gui = new JLabel(new ImageIcon(bi));
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
You can use TextLayout to get the geometry, as shown here. The example scales the image to fill the frame. JInternalFrame might be a good choice to leverage the frame's resizing feature. Alternative, the example cited here shows how to click and drag multiple selections.
u can define type of font
e.g.
Font f = new Font("SansSerif", Font.BOLD, 40)
Unfortunately the java api doesn't have a native free-form scaling/transform method fonts.
You can however rescale a BufferedImage or Graphics object with the scale(x, y) method. So you can try an approach with "layers" instead. I.e. draw objects, such as text, in their own layer (i.e. a BufferedImage) first and then on the actual graphics output.
So draw the text as usual on a BufferedImage and rescale it the way you want. Here is some simple sample code to get you starting.
// In your paint(Graphics g) method
// Create a buffered image for use as text layer
BufferedImage textLayer = new BufferedImage(240, 240,
BufferedImage.TYPE_INT_ARGB);
// Get the graphics instance of the buffered image
Graphics2D gBuffImg = buffImg.createGraphics();
// Draw the string
gBuffImg.drawString("Hello World", 0, 0);
// Rescale the string the way you want it
gBuffImg.scale(240, 120);
// Draw the buffered image on the output's graphics object
g.drawImage(gBuffImg, 0, 0, null);
The actual size of the text layer could be determined with the help of the FontMetrics object but I'll leave that as an exercise for the OP.
This can be done at the Graphics level using Graphics.setTransform(). However I believe it is more obvious to do this at the Font level using the lesser known Font.deriveFont(transform). For example
// create transform
AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(1d, 3d);
// create font using that transform
Font stretchedFont = g.getFont().deriveFont(affineTransform);
// set font as normal
g.setFont(stretchedFont);
// render as normal
g.drawString("Stretched", 23, 45);

Anti-aliasing in paintComponent() method

I want to print some text using the paintComponent(..) method.
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawString("Hello world", 10, 10);
}
But the text is somewhat jaggy.
How could you force text drawing with [anti-aliasing] in this method?
Thank you.
You can set double buffering by:
class MyPanel extends JPanel {
public MyPanel() {
super(true);//set Double buffering for JPanel
}
}
or simply call JComponent#setDoubleBuffered(..).
You can also set RenderingHints for Graphics2D objects like anti-aliasing and text anti-aliasing to improve Swing painting quality by:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
//Set anti-alias!
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Set anti-alias for text
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.setColor(Color.red);
graphics2D.drawString("Hello world", 10, 10);
}

Categories