그래서 내가 여기에 두 개의 클래스가 표시되지 않습니다.그리기 펜 자바 스윙을 사용하여 스트로크 ... 스트로크
이(이 클래스는 JComponent의 같은 특정 이미지를 처리하는 것입니다 때 내가 이미지를하는 대신 펜 스트로크를 그리려는 "튀 겼다."그래서 그 위에 펜 스트로크를 그릴 시도, 사각형과 이미지를 교체합니다.)
public class PhotoComponent extends JComponent {
private Image pic;
private boolean flipped;
private int contentAreaWidth;
private int contentAreaHeight;
p
@Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
System.out.println("Draw: " + draw + ", Pic: " + pic);
if (draw && pic != null) {
super.paintComponent(g);
System.out.println("width using this: " + this.getWidth() + ", actual width of JPanel: " + contentAreaWidth);
System.out.println("height using this: " + this.getHeight() + ", actual height of JPanel: " + contentAreaHeight);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null))/2;
int y = (contentAreaHeight - pic.getHeight(null))/2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x,y,pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (drawingMode) {
g2.setPaint(Color.RED);
if (drawOval) {
penStrokes.put(ovalX, ovalY);
if (penStrokes != null) {
for (Integer xCoor : penStrokes.keySet()) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((xCoor - (brushSize/2)), (penStrokes.get(xCoor) - (brushSize/2)), brushSize, brushSize);
//System.out.println("SIZE OF HASHTABLE: " + penStrokes.size());
}
}
System.out.println("Filling an oval!" + ovalX + ", " + ovalY);
}
} else if (textMode) {
g2.setPaint(Color.YELLOW);
if (drawRect) {
rectDimensions.add(rectX);
rectDimensions.add(rectY);
rectDimensions.add(rectWidth);
rectDimensions.add(rectHeight);
for (int i = 0; i < rectDimensions.size(); i+=4) {
g2.fillRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
g2.drawRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
}
}
}
System.out.println("This is being called again!");
}
}
}
public void setRectangle(int x, int y, int width, int height) {
drawRect = true;
rectX = x;
rectY = y;
rectWidth = width;
rectHeight = height;
}
public void removeRectangle() {
drawRect = false;
}
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX =
주를 DRAWLINE() 메소드 위. 주어진 시점에서 그리기, 다시 그리기, 그리고 이전 변수를 현재 변수로 설정하고 있습니다.
Main 클래스하십시오 mousedrag가 발생했을 때
private static PhotoComponent img;
private static JFrame frame;
private static JPanel contentArea;
//Mouse Coordinates for PenStrokes
private static int oldX, oldY;
//Mouse Coordinates for Sticky Notes
private static Point clickPoint;
public static void main (String[] args) {
frame = new JFrame("PhotoManip");
img = null;
contentArea = null;
oldX = 0;
oldY = 0;
setupMenubar(frame);
setupJFrame(frame);
}
private static void addPhotoComponent(File file) {
}
if (img.getTextMode()) {
img.removeRectangle();
clickPoint = null;
}
}
});
img.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (img.getDrawingMode()) {
if (withinRange(e.getX(), e.getY())) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
img.repaint();
}
}
if (img.getTextMode()) {
if (withinRange(e.getX(), e.getY())) {
Point dragPoint = e.getPoint();
h, height);
img.repaint();
}
}
}
});
if (img!=null) {
contentArea.add(img);
}
}
private static boolean withinRange(int x, int y) {
if (x > img.getX() && x < img.getX() + img.getWidth()) {
if (y > img.getY() && y < img.getY() + img.getHeight()) {
return true;
}
}
return false;
}
private static void flipImage() {
if (!img.isFlipped()) {
img.setFlipped(true);
} else if (img.isFlipped()) {
img.setFlipped(false);
}
}
drawLine()는,이 메인 클래스에 위라고합니다. 문제는 스트로크가 표시되지 않는 것입니다.
나중에 확인 문을 인쇄하기 때문에 프로그램에서 g2.fillOval()을 호출한다는 것을 알고 있습니다.
또한 마우스를 누르고 드래그하여 올바른 좌표를 얻었을 때 print 문을 만들었습니까?
왜 빨간색 선이 표시되지 않습니까? 나는 혼란스러워. 내 코드가 구조화 된 방식입니까?
을가 공동 ords 변수의 일종으로 선을 저장할 수있다 그것을 'paintComponent' 내에서 참조하십시오. 그래픽 객체를 저장하려고하지 말고'paintComponent' 밖에서 그릴 수 있습니다. 'paintComponent'가 다시 호출되면 곧바로 덮어 쓰게 될 것입니다. – nhouser9
문제에 대한 추가 컨텍스트를 추가했습니다. 따라서 paintComponent() 메서드 내에서 페인트 된 선을 모두 움직여야한다고 말하고 있습니까? – dan139
아래 답변을 게시했습니다. 전체 코드가 없기 때문에 실제로 컴파일하고 실행할 수는 없지만 제대로 작동해야한다고 생각합니다. 그렇지 않은 경우 알려 주시면 변경을 위해 변경해야 할 사항을 파악하는 데 도움을 드리겠습니다. – nhouser9