string doesn't appear in JFrame - java

I've been trying to draw a string in a JFrame, but for some reason it wont show , here is the code with the drawings command:
g.setColor(Color.WHITE);
g.fillRect(0, 0, 900, 500);
g.drawImage(backToMain,0,0,this);
g.setColor(Color.BLACK);
g.drawString("name", 300, 0);
but for some reason the screen I get is white with the Image in the left corner but missing the String.
I thought I might have a problem with the paint() command but I have tried several other things but it seams that the string just refuses to appear.

Related

How to make 2 intersecting rectangles go transparent

My goal is to fill the screen with a black rect with an alpha of 128 so the screen looks dark, then make it so I can render rects to the screen and the place they are rendered to turns fully transparent so you can see right through that rect. I have made the screen get filled partially black but I cannot make it go transparent when I draw a rect on top of that. I have never used AlphaComposites but I assumed that I'm gonna have to use one of those to make this work. Anyone know how I could get this done?
private Color darknessColor = new Color(0,0,0,128), flashlightColor = new Color(255,255,255,128);
public void render(Graphics g) {
// Draws the darkness part of the screen.
Graphics2D g2 = (Graphics2D) g;
g2.setColor(darknessColor);
g2.fillRect(0, 0, handler.getWidth(), handler.getHeight());
g2.setColor(flashlightColor);
g2.setComposite(AlphaComposite.DstOut);
g2.fillRect(200, 200, 300, 200);
g2.dispose();
}
There is no way to "undraw" a rectangle which has already been drawn; if you've already painted over the scene behind the rectangle, then it's gone. The solution is to only draw the black shape where you want it; this means it is not simply a rectangle, but if the part you want to "cut out" is a rectangle, then you could achieve the effect you want by drawing four black rectangles, like so:
Hopefully it's straightforward how to compute the coordinates that these four rectangles should have.

Java Graphics don't draw unless it's in a loop

When I create a Canvas and a JFrame, then put the canvas onto the JFrame.
With this code
final Graphics g = Display.getBufferStrategy().getDrawGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 480, 360);
g.dispose();
Display.getBufferStrategy().show();
However when I put the code in
The drawing only occurs whenever its's in a loop.
This isn't a huge problem since it would be in a loop anyways, but it's annoying having to have it in a loop to have it draw/keep the image on the frame
Please someone help me.
Thanks.

How to make JPanel use JTabbedPane UI without tabs using Aqua/system LAF?

First off, I have tried other solutions. Unfortunately they rely on you using a cross platform LAF.
A panel in OS X's System preferences:
looks identical to a JTabbedPane:
but without the tabs. I am trying to make something that feels native, but I can't find any other components with this UI, and I can't figure out how to hack a JTabbedPane so it displays normally just without tabs. Any ideas?
I can't figure out how to hack a JTabbedPane so it displays normally just without tabs.
I assume you are trying to display multiple panels in the same space. If so, then you can use a CardLayout. You can swap panels by specifying the name of the panel to display in the CardLayout.
Check out the section from the Swing tutorial on How to Use CardLayout for more information and examples.
So I ended up just working on my paintComponent method, and I think this makes it look pretty accurate:
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(212, 212, 212));
g2.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
g2.setColor(new Color(223, 223, 223));
g2.fillRoundRect(0, 1, this.getWidth(), this.getHeight()-1, 10, 10);
g2.setColor(new Color(227, 227, 227));
g2.fillRoundRect(1, 1, this.getWidth()-2, this.getHeight()-2, 10, 10);
}
I think it could be optimized a bit by changing fill() to draw() but this gets the job done. Unfortunately it won't look great on other operating systems, so I will have to handle those separately.
Edit: matched colors using Photoshop.

Line created by drawline method, disappears when resizing form

i have a jlable.and i'm going to draw a graph on it.but i realized that a line crated by draw line method ,disappear when resizing frame.
this is my code.i want to know how to avoid from disappearing when resize.i want to stay line even resize jframe.
void graph(JComponent jcom,int thick,int height,int xpos,int ypos,Color col){
Graphics2D gfx=(Graphics2D) jcom.getGraphics();
gfx.setStroke(new BasicStroke(thick));
gfx.setPaint(col);
gfx.drawLine(xpos, ypos, xpos, ypos-height);
}
button click code
graph(jLabel1, 10, 100, 200, 200, Color.GREEN);
You can create your own class which extends from JLabel and has an extra method to decide if it must paint the line or not.
In the overridden paintComponent() method of this new class, draw your line after the super.paintComponent() call.
apply the logic from suggestion 1 in the parent component of you JLabel. (not sure if this will work in all situations)

paintComponent() leaving behind previously drawn images after repaint()

I have a JPanel which draws .png images. each image has 2 copies to signify whether or not it has been selected. One image is normal and one has a colored border to signify the selection has been made. (You can think of these images as simple shapes for the sake of argument)
Which version of the image is drawn is determined via MouseListeners. If the user selects image1, then image1 will display its highlighted version.
In general this is working, however upon repaint() the highlighted version of the image is left behind. Since the highlighted version is slightly larger than the normal version, you can see it sticking out behind the newly drawn normal image (overlapping).
I thought repaint() was supposed to completely dispose of the current drawings and start from scratch, this doesn't seem to be the case here. Below is the general idea, blackClicked is toggled inside a MouseListener.
So my question is how do I make repaint() get rid of its currently drawn images and start from scratch.
Also, if there is a simpler way to achieve all of this please let me know, making 2 copies of images is tedious work.
#Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if(blackClicked)
g2.drawImage(blackSelected,0, 0, null);
else
g2.drawImage(black,0, 0, null);
g2.drawImage(green,0, 0, null);
}
how do I make repaint() get rid of its currently drawn images and start from scratch.
You mean, how to make repaint fill the component with it's background color before painting? How about adding
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
in top of paintComponent()?

Categories