How to fix the look of rendered string in Java - java

I am drawing a string in a JPanel with a pixel font. Some parts of the letters are drawn with extra pixels so it looks wider. How can I remove those extra pixels? Here's what it looks like.
public void paintComponent(Graphics g) {
Graphics2D gtd = (Graphics2D) g;
gtd.setFont(new Font("EXEPixelPerfect", Font.PLAIN, 50));
gtd.drawString("continue", 100, 100);
gtd.setFont(new Font("EXEPixelPerfect", Font.PLAIN, 51));
gtd.drawString("continue", 100, 150);
gtd.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
gtd.setFont(new Font("EXEPixelPerfect", Font.PLAIN, 50));
gtd.drawString("continue", 100, 200);
}
The first text is what I have originally. As you can see in the picture, some parts are wider.
In the second text, I increased the font size by one. I want it to look like that, but it's still not fixed. A part of 'e' looks wider.
The third text has the same font format with the first text but it has anti-alias. It fixed the wide lines but the problem is, it looks blurry.

Related

drawLine() method not drawing anything

So I have this class inside a class which is an implementation of JPanel.
private static class Line extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
System.out.println("Pozvan paintComponent()");
g.setColor(Color.YELLOW);
g.drawLine(20, 20, 100, 20);
super.paintComponent(g);
}
}
This is a snippet of code which creates a single instance of Line:
Line line = new Line();
line.setOpaque(true);
add(line);
I really don't know what I am doing wrong here. When I draw a rectangle, everything is nicely drawn.
when I set the height to remotely big number it works.
The default size of a Swing component is (0, 0). Since the size is 0, there is nothing to paint.
g.drawLine(20, 20, 100, 20);
Using the above information this means your component needs a size of (120, 40). That is, width = 20 + 100 and height = 20 + 20, in order for the component to be painted.
I added line.setBounds(20, 20, 80, 50);
Only part of your line will be painted, since you set the width to 80, not 120.
Read the section from the Swing tutorial on Custom Painting for more information and examples.

Java 2D, Translation with the same amount on X and Y axis leads to different results

I'm just playing around with Java 2D for a weekend-project. What I try to accomplish is simply to load and draw an image at position 50,50 away from the origin. Now when I try to do exactly that, with
g2d.drawImage(imageLeft, 50, 50, 200, 200, null);
then I get the following:
Frankly, this confuses me a little bit. Why does the x-part of the translation get scaled differently than the y-part? What is going on here? Must be an obvious beginner's mistake, but I cannot figure it out the reason for this behavior for the life of me ;-)
The complete code I use to draw the image is:
final IsogenWindow window = new IsogenWindow();
final BufferedImage imageLeft = loadImage(new File(getClass().getResource("/texture/tile1.png").toURI()));
final Graphics2D g2d = Graphics2D)window.getGraphics();
window.setSize(1000, 1000);
window.setVisible(true);
g2d.drawImage(imageLeft, 50, 50, 200, 200, null);
EDIT 1:
To try out DontRelaX' suggestion, I changed my code so a panel is added to the JFrame and its Graphic2D object is used for drawing:
getContentPane().setLayout(null);
this.renderPanel = new JPanel();
getContentPane().add(renderPanel);
final Graphics2D g2d = (Graphics2D)renderPanel.getGraphics();
...
g2d.drawImage(imageLeft, 50, 50, 200, 200, null);
Unfortunately, the result is still the same. But I think DontRelaX is on the right track. Any further suggestions?
EDIT 2: Basically DontRelaX' answer was correct. I indeed used not the renderPanel's Graphics2D but instead still the JFrame's Graphics2D object.
Cheers,
nanoquack
Pixels count from left top corner of window, not gray place. Add panel to your window and draw on it.

JFrame text displaying wrong

I am creating an app that uses an undecorated border and wanted to add a shadow to the my JFrame. I got the shadow working but in the process the text got all screwed up.
Due to the size of the program I can not post all of my code but the problem does go away when I remove this line.
setBackground(new Color(0, 0, 0, 0));
So what could cause the text to display blurry and incorrectly? It is bolder and some of the letters seem to be taller.
And I cannot post a picture since I do not have a level 10 reputation.
Here is more of my code:
int extendBy=30;
setMaximumSize(new Dimension(width + extendBy, height + extendBy));
setMinimumSize(new Dimension(width + extendBy, height + extendBy));
setPreferredSize(new Dimension(width + extendBy, height + extendBy));
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0)); // all hell breaks loose here
setContentPane(new ShadowPane());
getContentPane().setBackground(Color.BLACK);
setLocationRelativeTo(null);
setLayout(null); // I know setting null this is bad practice
edit: acquired 10 reputation so here is a pic (look at W or A or k):
Try to override the paintComponent method for this JTable.
How to do it: Overriding paintComponent
For Your case i would use anti-aliasing to get rid of those unwanted effects.
jTable1 = new javax.swing.JTable(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON );
}
};

Order of rectangles to display and can they be changed?

I working on a project in which I have to simulate a memory manager and show some memory snapshots. I have created a draw class via examples I have found here in which I override the paintComponet(). Everything draws fine.
I would like to be able to draw a rectangle to represent a memory partition and then overlay another rectangle over top to represent an incoming job (ie Job1 is in this partition3). What seems to occur is that I add the partition first (which will always be the case) and then when I add the job it will sit behind the partition block. Is there a way other than drawing the Job first to shift these after the job is created?
Here is the paint override
#Override public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// set up rendering to allow anti-aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// create the rectangle to represent the memory partition block
// x = address position h = amount of memory (y & w are predefined for the display block)
Rectangle2D rect = new Rectangle2D.Double(x, y, w, h); // create the rectangle
g2d.setPaint(partColor); // set it's color
g2d.fill(rect); // fill it in
// create the transparency for the text
Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
g2d.setComposite(comp);
// draw the text with color, type and size and center the text in the block created above
g2d.setPaint(Color.black);
g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
g2d.drawString(text, (int)((w / 2) - (text.length()/2)), (int)h/2);
}
The call to draw is in my window class (this will place the partition in front of the job) but I need to order to be reversed without changing the order of the calls.
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
snapPanel.add(part1);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
Is there some reason you can't do this?
// Draw both Text and Block with transparency
DrawPartition part1 = new DrawPartition(Color.blue, 0, 0, 110, 100, "part1");
part1.setBounds(5, 5, 110, 100);
DrawJob job1 = new DrawJob(Color.green, 0, 0, 110, 100, "Job 1");
job1.setBounds(5, 15, 110, 100);
snapPanel.add(job1);
snapPanel.add(part1);
A more general answer would be to add a z component to each of your rectangles. Then you can loop through your rectangles in the paintComponent method, drawing them in z order.

What is the correct syntax for changing fonts and drawing strings in Java?

Can someone check my syntax here? I am passing "Times New Roman","Arial","Verdana" to fontName and using 8,12,15 etc. for fontSize. It never changes the font here. I am doing this to write some text over an image.
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.drawImage(photo, 0, 0, null);
g2d.setColor(Color.white);
Font font = new Font(fontName, Font.PLAIN, fontSize);
g2d.setFont(font);
g2d.drawString(text,x,y);
I finally found out that none of fonts from my list were there on the system so I had to use getAllFonts() method and pass only those fonts from the list .
You should be doing this
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(photo, 0, 0, null);
g2d.setPaint(Color.red);
//example : g2d.setFont(new Font("Serif", Font.BOLD, 15));
g2d.setFont(new Font(fontName, Font.BOLD, size));
String s = "Hello, world!";
// assuming x & y is set using graphic's font metrics
g2d.drawString(s, x, y);
g2d.dispose();
Excerpt from sun documentation
getGraphics
public Graphics getGraphics() This
method returns a Graphics2D, but is
here for backwards compatibility.
createGraphics is more convenient,
since it is declared to return a
Graphics2D.
This does not really mean that you should not use getGraphics API. Just that the above code worked for me :)

Categories

Resources