awt

Draw rectangle border thickness

只谈情不闲聊 提交于 2020-01-01 07:54:08
问题 Is it possible to do draw a rectangle with a given border thickness in an easy way? 回答1: If you are drawing on a Graphics2D object, you can use the setStroke() method: Graphics2D g2; double thickness = 2; Stroke oldStroke = g2.getStroke(); g2.setStroke(new BasicStroke(thickness)); g2.drawRect(x, y, width, height); g2.setStroke(oldStroke); If this is being done on a Swing component and you are being passed a Graphics object, you can downcast it to a Graphics2D . Graphics2D g2 = (Graphics2D) g;

Need a way to scale a font to fit a rectangle

社会主义新天地 提交于 2020-01-01 04:18:09
问题 I just wrote some code to scale a font to fit within (the length of) a rectangle. It starts at 18 width and iterates down until it fits. This seems horribly inefficient, but I can't find a non-looping way to do it. This line is for labels in a game grid that scales, so I can't see a work-around solution (wrapping, cutting off and extending past the rectangle are all unacceptable). It's actually pretty quick, I'm doing this for hundreds of rectangles and it's fast enough to just slow it down a

The Need To Restore Graphics Original State When Overwritten paint or paintComponent

断了今生、忘了曾经 提交于 2019-12-31 22:52:36
问题 I realize most of the Java code to overwritten paint or paintComponent, most of them doesn't restore the old state of graphics object, after they had change the state of graphics object. For example, setStroke, setRenderingHint... I was wondering whether it is a good practice that we restore back the old state of graphics object, before returning from the method. For example public void paintComponent(Graphics g) { super.paintComponet(g); Stroke oldStroke = g.getStroke(); g.setStroke

Making a JScrollPane automatically scroll all the way down

六月ゝ 毕业季﹏ 提交于 2019-12-31 19:20:08
问题 I am trying to implement a JScrollPane with a JTextArea. The JTextArea is being appended to, and I want the JScrollPane to keep scrolling down as more text is added. How can this be achieved? 回答1: For (what I think is) a simpler answer check out: Text Area Scrolling. Prior to JDK5, you would have to manually change the caret's position after each append. You can now give this behaviour as a default like this : JTextArea textArea = new JTextArea(); DefaultCaret caret = (DefaultCaret)textArea

Can anyone help me identify the Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException on Line 65?

空扰寡人 提交于 2019-12-31 07:15:13
问题 I keep getting a Run Time error that says I am having an Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException it is saying a [line 65] but to me it just looks like a basic if statement. I can give some background. this frame is call from a sequence of frames that is then once the state is selected sent to a new frame that contains states A-C cities. This current frame will work only when Alabama is not the state selected. (I have not created the other frames for the other

LibGDX - Conditionally use java or android classes

别来无恙 提交于 2019-12-31 07:02:21
问题 I'm using bezier curves in my libgdx project. I was testing the desktop version using java.awt.geom with GeneralPath but when I went to test on android, it raised an error saying that I can't import java.awt . Android have corresponding classes for GeneralPath , Point2D etc so my question is how can I use those classes in their respective environments? 回答1: Android does not have an AWT implementation, so references to those classes won't work on Android. (On the desktop you're getting those

Drawing Canvas on JFrame

血红的双手。 提交于 2019-12-31 06:18:50
问题 I'm trying to draw simple shapes with Canvas, in this class I've set the painting public class Game extends Canvas{ //FIELDS public int WIDTH = 1024; public int HEIGHT = WIDTH / 16 * 9; //METHODS public void start(){ Dimension size = new Dimension (WIDTH, HEIGHT); setPreferredSize(size); paint(null); } public void paint(Graphics g){ g.setColor(Color.GREEN); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.BLACK); g.fillOval(100, 100, 30, 30); } } And in this the Window public class MainW {

Overriding the paint() method

ぐ巨炮叔叔 提交于 2019-12-31 05:29:06
问题 I've got a beginner question here that I was hoping someone with some Java experience could help me with. Currently taking an intro to OOP course focused on Java. My instructor is currently covering awt and swing, specifically the need to override the paint method so that graphics are redrawn when a window is resized, etc. I like to do as much outside reading as possible, and my concern is that the examples that my professor gives involve things that I've read are not best practices. To get

Using CardLayout for multiple JPanels and nothing displays

心已入冬 提交于 2019-12-31 05:11:32
问题 I'm making a simple (and bogus) computer power consumption calculator. I'm using a card layout to put multiple panels in but when I run it, there's just a small window not displaying anything. Here's my long code, I put it all in one class. package my.Project; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.text.DecimalFormat; import javax.swing.*; public class MainProject extends JFrame { CardLayout

Switching Apps on Mac with AWT Robot only sometimes works

匆匆过客 提交于 2019-12-31 04:22:28
问题 I'm trying to use Robot in order to switch apps, and then enter some text. To do this (on my mac), I'm pressing Meta, Tab, and then releasing Tab, Meta in this order: Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_META); robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_META); This works, but only occasionally (about every 5 or six presses). I've tried calling Thread.wait() inbetween press and release, but this has no effect. Neither does