awt

Java: ImageIcon vs. Image difference

一个人想着一个人 提交于 2019-12-05 00:36:58
Could anyone explain to me in noob way what the difference is betweeen ImageIcon and Image classes/objects in Java? Thanks tenorsax - support Monica Their nature and application is different. Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source. Edit: Think of an Image as something that could be rendered and an ImageIcon as something that will be rendered as an Icon when its paintIcon() method is called. Edit: The links above will take you to the JDK 6 api. These links will take you to the

How to output java full screen properly

末鹿安然 提交于 2019-12-05 00:16:47
Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again History: I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum

Swing Custom Border

风格不统一 提交于 2019-12-04 23:47:15
问题 Is there any way to achieve custom javax.swing.border.Border as shown in image? 回答1: You simply need to extend AbstractBorder and override its paintBorder() method, here is one related example : import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.awt.geom.Line2D.Double; import javax.swing.*; import javax.swing.border.AbstractBorder; public class CustomMarginBorder { private JPanel contentPane; private CustomBorder customBorder; private void displayGUI() {

How to rotate text with Graphics2D in Java?

99封情书 提交于 2019-12-04 22:51:48
I want to rotate text on a JPanel using Graphics2D.. My code is this: double paso=d.width/numeroBarras; double alto=datos[i].valor; Font fBarras=new Font("Serif", Font.PLAIN, 15); g2.setFont(fBarras); Rectangle2D barra=new Rectangle2D.Double(x,d.height-alto,paso,alto); //g2.fill(barra); x+=paso; g2.draw(barra); g2.rotate(-Math.PI/2); g2.setColor(Color.BLACK); g2.drawString(datos[i].titulo,(float)alto,(float)paso) This method must draw a rectangle and a text over the rectangle, but when i run this method all the graphic is rotated and i just want rotate the text .. Thanks :) The method

How to get real string height in Java?

…衆ロ難τιáo~ 提交于 2019-12-04 22:49:45
I'm using FontMetrics.getHeight() to get the height of the string, but it gives me a wrong value, cutting off the descenders of string characters. Is there a better function I can use? The getStringBounds() method below is based on the GlyphVector for the current Graphics2D font, which works very well for one line string of text: public class StringBoundsPanel extends JPanel { public StringBoundsPanel() { setBackground(Color.white); setPreferredSize(new Dimension(400, 247)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2

Readding panel to layout after editing panel?

霸气de小男生 提交于 2019-12-04 21:44:19
What I'm trying to do is dynamicly edit a panel and readd it to the (Border)layout. The panel contains textfields and I want the user to be able to add or remove textfields to the panel. What I tried is the following: remove the panel from the layout, add another textfield to the panel, readd the panel to the layout. However this doesn't work ( nothing happened; only the panel was removed but not readded with the new textfield in it , so the area was just empty ). Even when I removed the panel from the layout and then added another component to the layout at that position (BorderLayout.EAST)

How to add imagepanel in jpanel inside Jframe?

﹥>﹥吖頭↗ 提交于 2019-12-04 20:22:10
I'm trying to add an imagepanel which extends JPanel to another JPanel. Which is not working well for me. The paint function of image panel don't get invoked inside Jpanel but works fine in JFrame. Any ideas or help will be appreciated. import javax.swing.*; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; class ImagePanel extends JPanel { int g_h=10,g_w=10; int width=50,height=50; int cornerradius; Image castle; Dimension size; protected int x1,y1; Color c1=new Color(255, 0, 0); Rectangle rec; boolean b=false; boolean imboo=false; boolean roundb=

Swing: How do I run a job from AWT thread, but after a window was layed out?

别来无恙 提交于 2019-12-04 19:24:38
My complete GUI runs inside the AWT thread, because I start the main window using SwingUtilities.invokeAndWait(...) . Now I have a JDialog which has just to display a JLabel , which indicates that a certain job is in progress, and close that dialog after the job was finished. The problem is: the label is not displayed. That job seems to be started before JDialog was fully layed-out. When I just let the dialog open without waiting for a job and closing, the label is displayed. The last thing the dialog does in its ctor is setVisible(true) . Things such as revalidate() , repaint() , ... don't

Scale tiny low-resolution app to larger screen size

好久不见. 提交于 2019-12-04 19:15:56
I am creating a retro arcade game in Java. The screen resolution for the game is 304 x 256, which I want to keep to preserve the retro characteristics of the game (visuals, animations, font blockiness, etc.). But when I render this on a large desktop display, it is too small, as one would expect. I'd like to be able to scale the window up say by a constant factor, without having to code the various paint(Graphics) methods to be knowledgeable about the fact that there's a scale-up. That is, I'd like the rendering code believe that the screen is 304 x 256. I also don't want to have to change my

java.awt.Rectangle. intersection()

房东的猫 提交于 2019-12-04 18:50:29
I was developing an task when I decided to use java.awt.Rectangle to calculate the intersection between two rectangles. I realised that the output is different from what I expected. I'm not sure if I understood how this method works or not. For the values in the example here java.awt.Rectangle[x=0,y=10,width=5,height=8] java.awt.Rectangle[x=3,y=15,width=17,height=14] I expect the intersection to be java.awt.Rectangle[x=3,y=10,width=2,height=8] but the program prints java.awt.Rectangle[x=3,y=15,width=2,height=3] instead! here is my code: public void printIntersection(){ Rectangle r1 = new