paintcomponent

How to draw in JPanel? (Swing/graphics Java)

会有一股神秘感。 提交于 2019-11-26 05:53:48
问题 I\'m working on a project in which I am trying to make a paint program. So far I\'ve used Netbeans to create a GUI and set up the program. As of right now I am able to call all the coordinated necessary to draw inside it but I am very confused with how to actually paint inside it. Towards the end of my code I have a failed attempt at drawing inside the panel. Can anyone explain/show how to use graphics in a example like this? All examples I have found make a class and extend it with JPanel

Drawing Multiple JComponents to a Frame

狂风中的少年 提交于 2019-11-26 05:50:48
问题 I am trying to draw multiple car objects onto the same window but it appears that they are overwriting each other. Here is my overridden paintComponent method in the Car class public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(wheelColor); g2.fill(leftWheel); g2.fill(rightWheel); g2.setColor(bodyColor); g2.fill(body); g2.fill(cab); } And in my Viewer Class: JFrame f = new JFrame(); initializeFrame(f); Car x = new Car(100, 100); Car y = new Car(300, 300); f

How to draw an image over another image?

半世苍凉 提交于 2019-11-26 04:27:49
问题 I have a Java project that\'s about traffic network simulation in a random city, I\'ve managed to figure out a way to implement this project, so I divided each intersection into a section which is basically an extended JPanel class (named Carrefour)...everything works well until I got stuck with how to draw vehicles and make them pass through roads. So my problem is how to draw an image (vehicle image) over an another image (road)? 回答1: If this is Swing, then draw the background image in a

Stretch a JLabel text

久未见 提交于 2019-11-26 02:15:06
问题 Is there a way to make a JLabel\'s text stretch to 100% height? I need the text to update when the component\'s size changes as well. I saw some solution that could work; It involved calculating and setting the font size so it appears the right height. I would have also have to add listeners for when the height changed to make it respond and I do not know exactly where I should do that. I am hoping for a better solution with layout managers, but couldn\'t find anything. Any ideas? 回答1: In the

Draw a circle with a radius and points around the edge

久未见 提交于 2019-11-26 02:09:27
问题 I\'m really stuck on how to go about programming this. How to draw a circle in Java with a radius and points around the edge? I need to draw a circle within a JFrame with a radius and points around the circumference. i can mathematically calculate how to find the coordinates of the point around the edge but i cant seem to be able to program the circle. I am currently using a Ellipse2D method but that doesn\'t seem to work and doesn\'t return a radius, as under my understanding, it doesn\'t

Add an Background image to a Panel

不羁岁月 提交于 2019-11-26 02:03:06
问题 I have a JPanel , and i want to add an image as its background. How can i do that ? frame = new JFrame(\"Some frame\"); panel1 = new JPanel(); panel1.setBorder(new EmptyBorder(5, 5, 5, 5)); // NEED TO ADD AN IMAGE TO THIS PANEL panel1.setLayout(cardlayout); frame.getContentPane().add(panel1); frame.setLocationByPlatform(true); frame.setVisible(true); I need to add an image to the panel and how can i do it ? UPDATE 1 panel1 = new JPanel() { private static final long serialVersionUID = 1L;

Difference between paint, paintComponent and paintComponents in Swing

有些话、适合烂在心里 提交于 2019-11-26 01:47:29
问题 What is the actual difference between paint() , paintComponent() and paintComponents() in Java Swing? I tried to understand what explained in Oracle docs but I am not clear. 回答1: AWT, override paint() . Swing top-level container (e.g.s are JApplet , JFrame , JWindow , JDialog ..), override paint() . But there are a number of good reasons not to paint in a TLC. A subject for a separate question, perhaps. The rest of Swing, override paintComponent() . Neither override nor explicitly call

Threads with Key Bindings

こ雲淡風輕ζ 提交于 2019-11-25 23:44:57
问题 I\'m new to Java graphics and threads, and I\'m trying to make a game (specifically, Pong). The idea is that two people can play on the same keyboard (i.e. there are two paddles that are controlled through different keys). Currently, both players can\'t move their paddle at the same time. Is there a solution to this? Are separate threads the answer? If possible, I\'d like the paddles to be able to move (at least seemingly) at the same time. Update: It seems like using a Set<Integer> to store

Difference between paint, paintComponent and paintComponents in Swing

痞子三分冷 提交于 2019-11-25 22:13:08
What is the actual difference between paint() , paintComponent() and paintComponents() in Java Swing? I tried to understand what explained in Oracle docs but I am not clear. AWT, override paint() . Swing top-level container (e.g.s are JApplet , JFrame , JWindow , JDialog ..), override paint() . But there are a number of good reasons not to paint in a TLC. A subject for a separate question, perhaps. The rest of Swing, override paintComponent() . Neither override nor explicitly call paintComponents() , leave it to the API to call it when needed. Be sure to also use @Override notation whenever

How does paintComponent work?

情到浓时终转凉″ 提交于 2019-11-25 19:35:58
This might be a very noob question. I'm just starting to learn Java I don't understand the operation of paintComponent method. I know if I want to draw something, I must override the paintComponent method. public void paintComponent(Graphics g) { ... } But when is it called? I never see anything like "object.paintComponent(g)" but still it is drawn when the program is running. And what is the Graphics parameter? Where is it from? Parameter must be supplied when the method is called. But as I said before, it seems like this method is never be explicitly called. So who provides this parameter?